Maintainability, Scalability and extensibility are 3 most important things when you develop an application. Software development team usually composed of at least 2-3 people and most of the time the code has been written exceeds couple of thousands of lines. Because of that, each team member has to agree upon some common rules and restrictions over the project. So, developers will provide a common solution if they face any problem during the development instead of providing a specific solution for his/her part. In order to make it easier, there are some software design patterns that have been proved as best solution can be used. MVVM model is one of those software design patterrns which provides the 3 most important things that we have mentioned above specifically in WPF and Silverlight applications. It was created by John Gossman and it can be thought as a variation of MVC pattern.
MVVM model aims to provide seperating UI elements (UserControl, Page, Control, Window etc.) and related code in code-behind file from each other. In traditional UI programming, the logic belongs to the UI element usually resides in code-behind file.On the other hand,in MVVM model, this will not be the case. The goal is to clear the logic from code-behind file.
View: UI Element in the program is called View in this model and View can be Silverlight Page, User Control, Window, Data Template etc. Why it is called View? because it always displays something when the program runs.
ViewModel: This is the type that contains all the business logic for the View. Shortly, it provides a connection between view and model and keeps the states. View Model Philosophy aims "In this class, just code the business logic without thinking about the controllers"
Model: Model is data that we will use in the application. Data can be an entity returned from WCF RIA services or POCO objects.
View knows about ViewModel but ViewModel does not know about View.
We can say that view knows everything about ViewModel Because elements in UI will be bind to member in view model but ViewModel cannot reach View at all.
ViewModel knows about Model but Model does not know about ViewModel
View Model is able to reach a member of a class and use it but there is no code in model related with view model.
Advantages of MVVM
- Developer gains an ability and discipline to focus on business logic
- There is no need to change anything in ViewModel unless the business logic is changed. It does not matter that any input-output element in view has been changed.
- Blendability of View is increased so that Expression Blend ability is better.
- Binding in XML is easier to controllers. Data Binding in code file will be easier without repating the same code in different places.
- Because the business logic is independent from UI, testing the software is much easier.
- MVVM makes the designer and developer working synchronously and efficiently in the same project.