Model View Controller Model

A convenience model for view controllers

Rob Nash
2 min readOct 18, 2017

MVVM

The useful design concept ‘model view view model’ MVVM is widely accepted and simply put acts as a convenience model for a view. Such a model may, for instance, format the data that is about to be presented by the view.

A convenience model is exactly what I propose for view controllers.

Why?

First, let’s understand what view controllers are typically responsible for. View controllers are responsible for co-ordinating views, which typically involves passing data them. But view controllers often do the same in reverse by co-ordinating the collection of data from views (and controls etc).

For example

Let’s consider a ‘calendar entry’ scene which presents UI to the user for the collection of data regarding a calendar entry. An instance of a calendar entry, may look like the following.

This structure is great for immutability and is very clear. However, we sometimes want to temporarily store incomplete data. Or we may want to validate data and immediately feedback any issues to the user, via the UI. If the data was incomplete, we would need to discard the above struct instance and create another, which seems wasteful.

If we changed the above struct so that each property was optional var, then we could store values on each property of the struct and easily re-assign each property as the user re-attempts to input valid data.

But this doesn’t solve our problem of unecessary copies. By re-assigning a var property on a struct, we essentially instruct the compiler to instantiate a new struct using that new property value.

A struct is a value type and once it is instantiated, it does not change. See assignment and copy behaviour and copy-on-write for more details.

Another point to mention about var is that we don’t want our data to be mutable once the user has completed data input. We want to transmit immutable data.

MVCM

Let’s consider a convenience model, local to the view controller, designed for data collection and temporary storage.

Once the user has completed data input, we may then validate the data and move on.

An immutable version of the data is transmitted.

--

--