Differences Between MVC, MVVM, and MVP

The three patterns—Model-View-Controller (MVC), Model-View-Presenter (MVP), and Model-View-ViewModel (MVVM)—have many similarities, but also are very different. All three patterns have an underlying goal, which is to separate the view from the model. All three patterns contain the concepts of the model and the view.

The main difference between the patterns is the way changes are propagated between the view and the model. The view model, the presenter, and the controller all share the responsibility of communicating state changes between the view and the model, but they employ a different mechanism to do it.

In MVC, events fired in the view result in actions being called on the controller. In the MVC Framework, this is implemented by HTTP requests routed to the appropriate controller by the ASP.NET request routing subsystem. Each unique URL is mapped to a special method in the controller, known as an action. Inside the action method, the view data is processed and the model is updated. MVC controllers also have the additional responsibility of determining which view should be displayed.

In the MVP pattern, the controller has been replaced by the presenter. The presenter is similar to the controller in that it is the only entity that should manipulate the model. Presenters differ from the controllers in three ways:

  • They do not play the role of the traffic cop as controllers do, but instead are instantiated by a view.
  • The view and the presenter are completely decoupled and communicate by way of an interface.
  • The presenter handles all UI events on behalf of the view.

The MVP pattern is commonly used by Enterprise ASP.NET Web Forms developers who need to create automated unit tests for their code-behind pages, but do not want to run the tests inside of a web server process. By modeling the properties and events defined in the Web Forms page into an interface, a mock implementation of the page can be used when running unit tests. Figure 2-3 shows a conceptual diagram of the MVP pattern implemented in an ASP.NET Web Forms application.

In the MVVM pattern, two-way data binding is used to communicate state changes in the view to the view model. In application frameworks such as WPF and Silverlight, this is done by setting the view model as the data context on the view. Because the data binding is bidirectional, when the view model’s data is changed, the updates value is automatically propagated to the view, and changes to the view are automatically propagated to the view model.

The view model typically uses the observer pattern, in which an event is fired every time an exposed property is modified—notifying subscribers that a change has occurred.

The main advantage of this pattern is that it eliminates the need to explicitly write code for data binding. in order to update the user interface with data from the view model. It also removes the necessity of writing code  to update the model with changes made by the end user in the view.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.