Before you create your first view model, I want to revisit the internals of the Model-View-ViewModel pattern. We need to keep the following in mind.

The model is owned by the view model. The controller doesn't know about and cannot access the model. The controller owns the view model and the view model doesn't know about the controller it is owned by.

MVVM Architecture

These are the four key elements you need to remember for this and the next episodes. Let me show you what happens when the controller needs to display a piece data in the view it manages.

An Example

The controller asks its view model for a piece of data. The view model asks the model it manages for the raw value, a timestamp, for example. The view model transforms and formats the raw value and returns a value the controller can immediately display to the user in its view.

That is the flow we will see over and over in the next episodes. The controller is no longer responsible for transforming the raw values of the model. In fact, it doesn't even know about the model. That is an important difference that sets the Model-View-ViewModel pattern apart from the Model-View-Controller pattern.

Naming the View Model

What name should you give the view model? You have several options. I recommend choosing a name that feels right and makes sense. Make sure you understand the code you write today a year from now.

The view models I create are very often focused on a specific view controller. For example, we will create a view model for the day view controller and a view model for the week view controller. Because the view model is linked to a view controller, I name the view model after the controller it is tied to. I simply replace Controller with ViewModel.

For example, we will name the view model for the day view controller DayViewModel and the view model for the week view controller WeekViewModel. You are free to choose the name of your view models, but I recommend that you use the ViewModel suffix to clearly indicate that you are dealing with a view model.