What is MVVM?

There has been a lot of confusion lately over just what MVVM is and what it isn’t. I think this confusion has led many to throw their hands up in disgust and/or frustration and swear off MVVM altogether. I think the confusion comes because people aren’t clear about the delineation between the pattern itself and the details of implementing the pattern. So here is my definition of MVVM.

Model-View-ViewModel (aka Presentation Model)

Problem: You have a UI with complex, interdependent interactions.
Solution: Separate the UI from the UI logic using a ViewModel that encapsulates the interactions and provides properties for a View to retrieve its state (and the state of its elements).

This is the long and short of MVVM Nothing more nothing less. Everything else is an implementation detail. If you use INPC or not, if you use Code-Behind in ASP.NET or Data binding in WPF/Silverlight. If you use DelegateCommands, RelayCommands, or RoutedCommands. If you use attached behaviors or Blend behaviors, VSM, or data triggers is not a concern of the pattern. The pattern is overkill for "Calculator Examples" (unless of course your calculator is storing a stack of historical operations for undo/redo). The pattern is not mutually exclusive of using a Front Controller for navigation.

I think the majority of frustration regarding MVVM comes from a lack of this fundamental knowledge of what it is and what it isn't. So there, I've spelled it out. HOW the pattern is implemented is up to the individual.

Published Tuesday, May 04, 2010 11:22 AM by Mike Brown

Comments

# Twitter Trackbacks for Just gave my definition of MVVM [azurecoding.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Just gave my definition of MVVM         [azurecoding.net]        on Topsy.com

# re: What is MVVM?

I agree with you. However, I would add one more constraint to your definition in order to differentiate it from Supervising Controller. A ViewModel relies wholly on some sort of observer synchronization mechanism, however it's implemented (you've already listed a lot of examples), to connect the view and the view model. I've had a lot of questions about and seen a lot of developers confused about the difference between a Supervising Controller and a ViewModel because a SC uses a combination of observer sync and manual sync (or flow sync). I tend to view several of these patterns as a spectrum. PassiveView is on the left side, using wholly manual sync mechanisms; ViewModel is on the right, using wholly observer sync mechanisms; Supervising Controller is somewhere in the middle, using a combination of both (which is why people get confused and often misrepresent one as another).

I think one problem we have right now is that the Silverlight/WPF community, with a few exceptions, is *only* talking about MVVM. As a result, developers don't really understand the broader range of UI patterns, their differences (which are sometimes quite nuanced) and when one pattern is a more appropriate solution than another.

Tuesday, May 04, 2010 8:47 AM by Rob

# re: What is MVVM?

What better way to clear up confusion than an extremely vague definition.

Tuesday, May 04, 2010 8:49 AM by Corey

# re: What is MVVM?

What's vague about it? It's a pattern. Patterns don't dictate how they're implemented, they just provide a solution to a problem.

As I mentioned a lot of the confusion comes from people mistaking the implementation for the pattern itself. I wanted to take a step back and spell out just what the pattern is.

Tuesday, May 04, 2010 10:52 AM by Mike Brown

# re: What is MVVM?

"Here here" on the distinction between *pattern* and *implementation*, Mike.

You aptly captured the fundamental intent of MVVM:  separate view (visuals) from presentation logic

Rob's comment helps differentiate MVVM from other Presentation Patterns that share this intent.

The vagueness complaint has some merit. Yes people tend to confuse pattern and implementation ... and we don't want that.

But even a brief definition of MVVM would identify the distinguishing characteristics of the pattern, the collaborators, and how it works in the abstract.

You could do worse than the first screenful of Fowler's "Presentation Model": martinfowler.com/.../PresentationModel.html

A couple of pithy quotes therefrom:

"The essence of a Presentation Model is of a fully self-contained class that represents all the data and behavior of the UI window, but without any of the controls used to render that UI on the screen. A view then simply projects the state of the presentation model onto the glass"

"Compared to Passive View and Supervising Controller, Presentation Model allows you to write logic that is completely independent of the views used for display. You also do not need to rely on the view to store state. The downside is that you need a synchronization mechanism between the presentation model and the view. This synchronization can be very simple, but it is required. Separated Presentation requires much less synchronization and Passive View doesn't need any at all."

"Probably the most annoying part of Presentation Model is the synchronization between Presentation Model and view. ... Ideally some kind of framework could handle this, which I'm hoping will happen some day with technologies like .NET's data binding." [written pre-WPF/SL]

To my eye, this is sufficiently precise - I can see what it's about and compare it to alternatives - without succumbing to the implementation specificity trap that you justly decry.

Wednesday, May 05, 2010 11:41 AM by Ward Bell

# re: What is MVVM?

I'd prefer to see this in the same format as was used by GoF, but you're spot on here. Way too much of the confusion lies in people mistaking implementation for pattern, and then other's not seeing the real pattern in the details of the implementation and thus writing about the pattern with a flawed understanding. Rinse, repeat, and cry.

Wednesday, May 05, 2010 11:48 AM by wekempf

# re: What is MVVM?

For comparison:

In Passive View, the role of the controller is to synchronize state.

In MVVM, the role of the View Model is to represent state.

In Supervising Controller, the role of the controller is to represent simple state and synchronize complex/difficult to represent state.

Implicit in MVVM and SC is some form of observer synchronization which can be handled in a variety of ways.

Wednesday, May 05, 2010 11:54 AM by Rob

# re: What is MVVM?

Thanks for the feedback everyone...I agree that I may have been too spartan in my definition. I'll update with some more info.

--Mike

Wednesday, May 05, 2010 5:25 PM by Mike Brown

Leave a Comment

(required) 
(required) 
(optional)
(required)