MVC vs MVP vs MVVM : Difference
MVC, MVP and MVVM; among the most prominent questions in an interview. It seriously ate my head and time in the beginning. Links were available, but again the terminologies and examples used were no lehman terms. That is why I thought just to share the crux in the simplest possible way. The article is totally as per my understanding on the topic. Please correct me in case I appear confusing or you have a contradicting thought.
MVC, MVP and MVVM are design patterns prevalent in most of the Service Oriented Architecture (SOA) applications.
MVC stands for Model View Controller.
MVP stands for Model View Presenter.
MVVM stands for Model View View Model.
As can be made out from the expanded form of the three design patterns above, the difference lies in the third term. Yes it is the third aspect or layer of these patterns that puts them different from the another.
To begin with, an SOA (Service Oriented Architecture) web application or desktop/windows application will have the following building blocks :
i) Layer that connects to Database ( may be using ADO.Net, Enterprise Library or any third party product available )
ii) Layer that contains the aspx or UI pages.
iii) Layer in between the UI or aspx and the Database connection details; this layer contains the Logic that processes the data provided by User in the UI, and passes it to the database layer that saves it to the database. It also reads from the Database and processes it to the UI layer.
The term Model(in MVC or MVP or MVVM) is what (i) above is all about. The Model contains :
a) Required connection settings to Database
b) Required logic to convert the processed data to Database specific objects
c) Required logic to change the result set from the Database, to an entity that is understood and can be processed by the other layers of the application.
View (in MVC or MVP or MVVM) refers to the User Interface pages in the application, through which the Users interact or provide data.(Point (ii) detailed above). The View contains :
a) ASPX pages in MVC/ MVP; XAML pages in MVVM.
b) User controls, Stylesheets(Resource dictionary), Resource files
c) UI validations
d) Required logic to process or map the User provided data in the UI, to entities that can be well understood and processed by other layers.
View and Model are same for all the three patterns; whether it be MVC or MVP or MVVM. The difference lies in the the third component (point (iii) detailed above), that lies between the View and Model. This intermediate layer details the communication between the View and Model, and known as Controller in MVC, Presenter in MVP and View Model in MVVM.
The difference between the MVC, MVP and MVVP lies in the mode how the layers communicate. Communication is when data from Database is updated to UI (Model to View), or when User's response from UI is submitted to Database (View to Model), through the layers(Controller/ Presenter/ View Model) in between.
To put in plain words, in MVC, Controller controls the View to Model interaction i.e UI to Database, where as the communication from Model to View may happen without invoking the Controller.
In MVP, Presenter acts as a medium in both the modes of communication, ie View to Model and Model to View. So MVP is considered more modular and more of loosely coupled compared to MVC. This aspect of MVP makes is a reasonable choice over MVC, when the application has to capture User response as well as display information on UI.
In MVVM, View Model is more of Silver light or WPF specific or can say UI related. In case of MVP, Presenter is aware of View and View is also aware of Presenter, ie. both have each others instance, but in case of MVVM, it is only the View which is aware of the View Model, the View Model is not aware of the View it is associated to. That is, in a way View Model is more loosely coupled compared to MVP. The sole idea behind View Model is about Binding and Dependency.
MVVM again is altogether a different concept, where we again have .Net libraries available (Prism with MEF) to implement. Detailed implementation of MVVM will be dealt in later posts. :)