views:

820

answers:

2

ViewModel is a term that is used in both MVVM (Model-View-ViewModel) and the recommended implementation for ASP.NET MVC. Researching "ViewModel" can be confusing given that each pattern uses the same term.

What are the main differences between the MVC ViewModel and MVVM ViewModel? For example, I believe the MVVM ViewModel is more rich, given the lack of a Controller. Is this true?

+3  A: 

It's been a while since I played UI Design Patterns Bingo.. however let me take a stab at this..

MVVM is just something that MS has come up with... because it helps you to get the most out of WPF. You combine the state and behavior of the view into a class (a presentation model) that is easily testable + then you use data-binding to get the data into any view.

This link has a brief of the evolution of MVVM. Combine this with Fowler's "GUI Architectures" series, and you should be on your way.

Update: Didn't know there was something called MVC-VM. Apparently a brainchild of the ASP.NET MVC crowd. Looks and sounds similar to MVVM (except tuned for ASP.NET MVC); the only difference is that it places a restriction that there is 1:1 mapping between VM and View. I'd have guessed 1:N, but everything else matches.

Gishu
+8  A: 

A rather challenging question to answer succinctly, but I'll attempt it. (Bear in mind these answer to these kinds of questions are still the subject of debate amongst developers.)

In MVC, the ViewModel provides all the information necessary for a View to be rendered. The data it contains is created using data defined in the Model. The View reads the ViewModel and renders the output. Input from the View is passed to the Controller, which manipulates the Model, constructs an appropriate ViewModel, and passes this to the View for rendering.

In MVVM, the ViewModel serves the same function as it does in MVC, but it also replaces part of the MVC Controller by providing commands which allow the View to manipulate the Model. WPF databinding manages the updating of the View according to changes in the ViewModel (and this effectively replaces the remaining function of the MVC Controller).

AdamRalph
In practice; the ViewModel in ASP.NET MVC allows for no 'Magic Strings' in the View; and provides a strongly typed model for the view to display (Intellisense included, and all that jazz). Otherwise, excellent answer.
George Stocker
Good answer. @George: You can still use Magic Strings in MVC though... this isn't a concern of MVC... it doesn't do anything to prevent this practice.
Anderson Imes