Windows Phone

MVVM basics for Windows Phone 7 developers

Justin James provides a high-level overview of what Windows Phone 7 developers need to know about the Model, View, View Model (MVVM) pattern.

One concept I keep bumping into with Windows Phone 7 development is the Model, View, View Model (MVVM) pattern. It has been a rather difficult to find good material that explains why, where, and when to employ the pattern, so I spoke with Ginny Caughey, a Windows Mobile MVP, to find out more about it. Here's what I learned.

Why MVVM is useful

Like many of the patterns to gain recent popularity, MVVM separates concerns and makes unit testing easier. In the context of a Silverlight or Windows Phone 7 application, it removes all application logic from the code behind, relegating it to doing little more than tying the XAML (which is the "View") to the View Model located in a separate file. Because it uses data binding, you do not need to write heaps of code to populate UI content, react to user input, or perform any other kind of IO work. Also, it is a lot easier to write code to restore the application state after the application was tombstoned, since you can serialize the data context.

Cautionary note: Based on my research, I learned that MVVM is too much work for the results for simple applications. Not that it is hard to implement per se, but there is little payoff in creating all of the files and going through the effort of separating concerns when there are few concerns to separate.

The three parts of MVVM

  • The Model holds the business logic. If you have a data driven application, your data connectivity happens here, for example. In more algorithmic applications, this would be where your calculations appear.
  • The View Model takes the information from the Model and transforms it into something usable by the View. It also takes the changes from the View and calls the appropriate functionality in the Model. It is a gateway, or a translation layer between the Model and the View. The INotifyPropertyChanged interface is the solution for handling the binding with the View.
  • The View represents the user interface. In a Windows Phone 7 application, it is implemented with the XAML of a screen.

MVVM Light Toolkit and Prism

Ginny told me that two excellent and popular choices for working with MVVM are MVVM Light Toolkit and Prism. MVVM Light has the advantage of being minimalistic, which reduces resource consumption. While that may be less of a concern in a Windows Presentation Foundation (WPF) or a Silverlight desktop application, it is a very big factor when writing Windows Phone 7 applications. She also explained to me that in the applications she has used MVVM Light, she has been able to update the code with little effort.

The debugging aspect

One of my biggest concerns with this style of programming has been the debugging aspect. MVVM relies upon event handling, which means the code is more abstract. There's no obvious path from the UI to the Model. The developers that I have talked with about this are in agreement that it takes some adjustment to get used to the different debugging patterns, but there are no issues with it. Since my Lifting Calculator application has not had much work done in it, I am going to convert one of the screens to MVVM and see how well it works out for me.

MVVM resources

Here are some additional resources to help you learn about MVVM:

J.Ja

About

Justin James is the Lead Architect for Conigent.

1 comments
Justin James
Justin James

If so, how did you go about it? What did you think of it? J.Ja