It won't take long in your quest to find more information on ASP.NET MVC through internet searches, with bing.com of course, before you'll stumble across the notion of a ViewModel. Mixed within those search results are links to articles on ViewModels in Silverlight/WPF, or other technologies that also use the MVC or MVVM patterns or ViewModels, which can be quite confusing. Having said that, it's no wonder in every presentation I give concerning MVC or ViewModels, a few folks frequently ask these two, quite reasonable, questions:
- "What's the difference between MVC and MVVM ?" (in general)
- "What's the difference between ViewModels in MVC and ViewModels in MVVM?"
The elevator pitch: MVC is a web UI pattern, and ASP.NET MVC is Microsoft's implementation of it. MVVM is Microsoft's implementation of a desktop UI pattern in WinForms, SilverLight or WPF.
MVC is an old pattern, in use for decades in non-Microsoft platforms, and MVVM is a pattern similar to Martin Fowler's Presentation Model (PM) pattern, as it has taken many of its concepts from PM.
It's all about the patterns
Both MVC and MVVM are patterns, meaning that they are solutions to recurring design problems in software development. Patterns can show interactions between objects in various ways, as the few listed below will demonstrate:
- General interactions
- Interaction through inheritance and OOP principles
- UI interactions
- Interaction between models, views, and controllers or models, views, and ViewModels
- Data access patterns
- ORM mappings, Active Record pattern, POCOs in DALS
Once you're aware of patterns, you can then apply them in software to make your code more stable, consistent, and maintainable. A variety of tools and frameworks exist that can easily help find the best pattern and/or implementation for your specific business problem/domain. In particular, the MVC and MVVM patterns are both UI interaction patterns with MVC being the web UI pattern and MVVM being a desktop UI pattern. You can implement either pattern in Visual Studio, and the ASP.NET MVC project template in particular guides developers to use the MVC pattern.
In addition to MVC & MVVM there are many patterns that are worth reading up on.
Comparing MVC and MVVM: the patterns.
Since MVC and MVVM are geared towards different application paradigms altogether, i.e., ASP.NET MVC for web and MVVM desktop, they need to behave in distinctly different ways, with the most noticeable distinction being the controller from MVC...
...and the ViewModel from MVVM...
The controller in MVC accepts HTTP requests, fetches data from the model, and routes that model to the view for output. In a desktop app, there is no routing or URLs; but desktop apps still feature navigation, which is part of the UI and therefore needs to be part of a good UI pattern. ViewModels are the piece that accomplishes this task, as the ViewModel in MVVM takes the responsibility of performing, or exposing the command that house all the UI logic in addition to fetching the data and data binding.
Models behave the same way in either pattern - they're full of data (and sometimes logic). You may want to use other patterns at the model level for better code organization, maintenance, and a finer separation of concerns. The repository pattern with Entity Framework is a popular pattern, and Julie Lerman has a great explanation within series of posts on it.
Within both MVC and MVVM exists the ViewModel. Despite the same name, there are marked differences within how ViewModels in either pattern work.
Comparing MVC & MVVM: the ViewModels.
There are ViewModels in MVC, but they have different responsibilities than an MVVM ViewModel.
- An MVC ViewModel is two or more models combined (smashed together), or a customized subset of a model or models that provides all the information necessary to its corresponding view. It's basically a hybrid model, and the best part - the views don't know the difference.
- In MVVM, the ViewModel serves the same function as it does in MVC, but it also takes on the responsibility of a controller.
This is because although they're both user interface patterns, a web application and a desktop application behave very differently, with a few key reasons below:
- As previously mentioned, navigation is completely different in both type of apps, and SilverLight/WPF applications do not use routing or URLs.
- Desktop applications connecting to LAN resources can happily save data locally, with security restrictions, of course. One reason is because browser based applications have no access at all to resources on the user's machine, and rightfully so, or any web site could add what they'd like to the users machine (not acceptable!). Users specifically choose to install desktop apps. When the app runs under the "Standard Users" group, it enjoys a wider range of UI activities than a web application will.
- Model/Data Binding
- Because of the stateless environment of web apps vs. the stateful environment of desktop apps, binding happens in different ways. Controllers and/or models in MVC must parse and match HTTP form elements to model properties for model binding to occur. This happens for each HTTP request, keeping in mind that the web app has no knowledge of any previous requests, even by the same user agent (browser). On the other hand, while ViewModels in MVVM apps match up the model with the view via data binding, it has no need to deal with HTTP request cycles. Networked desktop apps have built-in capacity to know what's going on between requests for data from the database.
- ViewModels in both MVC and MVVM both can perform validation; however MVC 3 validation with Razor exploits unobtrusive jQuery on the client side in tandem with data annotations on the server. Alternatively, MVVM depends on data binding properties. MVC 3 also offers remote validation for dealing with the stateless nature of the web, whereas MVVM on desktop apps have immediate access to data through data binding to validate.
As you can see, there's both similarities and differences between the patterns; mostly dependent on the environments - browser or OS.
Since they're all in the same category, UI presentation patterns, you can use MVC on WPF, SilverLight or Winforms apps & MVVP in web applications. A note of caution: before running off swapping out patterns, be sure that what you're going to use will actually work the best for your project.
By now you may be wondering where MVP pattern for ASP.NET, Winforms or WPF/SL fits in. MVP, or Model-View-Presenter, is yet another UI pattern for web or desktop development. Experiment with other patterns such as MVP to see what helps your coding efforts the most.
Phil Japikse writes, talks, and mentors on patterns and testing with MVVM, WPF, & SL. Phil was also kind enough to take some time to compare the finer points of the patterns with me before posting this.
Karl Shifflett's Blog. Karl is a Program Manager at Patterns & Practices at Microsoft.
 The "Standard Users" group contains users with very basic privileges in the Windows OS, but has more privilege than the anonymous web user (IUSR_XX or ASPNET).