Having spent some time looking at several presentation technologies recently I thought I should take stock of a number of design patterns that are frequently referenced.
Silverlight and WPF are often associated with the Model-View-ViewModel (MVVM) pattern (see frameworks such as Prism, MVVM Light, Caliburn etc). ASP.Net MVC of course utilises Model-View-Controller. In the past I have tried to overcome some of the short-comings of ASP.Net Web Forms using variations of the Model-View-Presenter (MVP) pattern.
Each of these patterns tries to address a number of problems:
- Where and how do we maintain state in the UI?
- Where does business logic live in the application and how is it invoked?
- How do we keep the UI synchronised with changes to the data and between UI elements?
- How can we ensure good separation of concerns and create loosely coupled testable code?
So the question is, how do they compare?
Note: There are variations on the theme here so there may be alternatives especially with MVP (e.g. Passive View, Supervising Controller, Front Controller).
MVC | MVP | MVVM |
Input is routed to the Controller. The Controller decides which View to render and builds the Model which is bound to the View. A Controller can choose to render one of many Views. The View has no knowledge of its Controller. Business logic exists in the Controller. MVC is useful when state can’t be maintained between user input requests (e.g. over HTTP – a stateless protocol). | Input is routed to the View. The Presenter updates the View usually in response to events raised by the View. State is effectively stored in the View. Business logic exists in the Presenter. | Input is routed to the View. The View knows about nothing but the ViewModel. The ViewModel knows about nothing but the Model. The View gets its data from the ViewModel, not the Model itself. This is generally accomplished by data binding. State and business logic exist in the ViewModel. The ViewModel can be thought of as an abstract representation of the UI. MVVM is useful when state can be maintained between UI requests (e.g. Silverlight, WPF). |