Note: this article is published on 04/26/2021.
These will be a series of articles about Design Patterns. We start from MVC Pattern:
- Design Pattern (0), Overview
- Design Pattern (1), MVC
- Design Pattern (2), MVVM --- this article
- Design Pattern (3), Singleton
- Design Pattern (4), Desposal
- Design Pattern (5), Dependency Injection
Introduction
After writing Design Pattern, MVC, my next design pattern article had to be MVVM for sure. Especially, I used this pattern and tried to train my team to use this pattern several years ago. However, I wonder, for this important, popular, and valuable pattern, why Microsoft does not make a physical pattern like MVC (for MVC, we have MS Visual Studio Web MVC module for more than 10 years, actually from 2008). On the other hand, if we search online, we can get a lot of papers to introduce and discuss MVVM pattern.
I realize the MVVM pattern still lives while Microsoft does not have a formal physical pattern (although there was one call MVVM Light ToolKit for WPF and Silverlight for a while) must be because this pattern is quite complex with more variations, therefore it is hard to make a formal pattern.
That is true, when I tried to discuss MVVM pattern, it seems to me that the first thing I need to do is to define the pattern, ..., so, this article will be starting by discussing the definition of MVVM, and then giving a simplest sample to demo MVVM.
The discussions in this article will be as following,
- A: History
- B: What is MVVM Pattern:
- C: Make a Project Demo
- C-0: Make a WPF app
- C-1: Make a Separation between Model and View
- C-2: Make Synchronization between ViewModel and View
- C-3: Use ICommand from View to Call back
- D: What is MVVM Pattern again:
- Physical module to Conceptual module
A: History
Model–view–viewmodel (MVVM) is a software architectural pattern that facilitates the separation of the development of the graphical user interface (the view) from the development of the business logic or back-end logic (the model) so that the view is not dependent on any specific model platform.
The view model of MVVM is a value converter, meaning the view model is responsible for exposing (converting) the data objects from the model in such a way that objects are easily managed and presented. In this respect, the view model is more model than view, and handles most if not all of the view's display logic. The view model may implement a mediator pattern, organizing access to the back-end logic around the set of use cases supported by the view.
MVVM was invented by Microsoft architects Ken Cooper and Ted Peters specifically to simplify event-driven programming of user interfaces. The pattern was incorporated into WPF and Silverlight. John Gossman, one of Microsoft's WPF and Silverlight architects, announced MVVM on his blog in 2005.[4]
Model–view–viewmodel is also referred to as model–view–binder, especially in implementations not involving the .NET platform.
B: What is MVVM
MVVM is a variation of MVC module, we can say MVVM module is derived from MVC in concept. The difference is that MVVM used most likely for WPF or Silverlight apps without an endpoint for controller access, but with strong data binding between the View and middle tier, the "Controller". So, this introduces the ViewModel to replace the position of Controller of MVC module.
MVVM stands for Model, View, and ViewModel. MVVM separates an application into three components - Model, View, and ViewModel, associated with One Operation: Data Binding that have two features: Synchronization and Command Calling Back.
Three Components of MVVM: the following understanding it from my previous article: Design Pattern (1), MVC
- Model: What --- Data
- View: What Look like --- Format (with data)
- ViewModel: How --- operation through Data Binding
One Operation of MVVM with Two Features,
- Data Binding: Between View and ViewModel with two features:
- Synchronization: View and ViewModel (ViewModel and Model are always synchronized)
- Command: Calling back from View to ViewModel
Conceptual Module
Note:
For the conceptual model above, someone argued that the MVVM model is a front end model, it should not directly talk to Database as the graph shown above. That is true, the MVVM model and the MVC model are all front end model for sure. MVC is a new replacement of the old ASP.NET web application, while MVVM is a new version of Windows Forms, and on the top of WPF. At this point, if we say the MVVM model is a data holder in this model, and it links to Data Source, provided such as by Web API or some other middle tier. I have modified the photo a little bit below to make the point clear:










Mariusz PostolPosted Jul 1, 2024, 8:11 PM
I am confused about how the comments work, but my first comments are dated June 10, 2024 - a few weeks ago. You mentioned that you are going to introduce some modifications. Before submitting try to check out my article - it is not published on CSharpCorner jet (only submitted) but a similar one is available in the GitHub repository at <https://github.com/mpostol/TP/blob/ExDMUdemy/ExDataManagement/GraphicalData/README.MVVM.md#mvvm-program-design-pattern->. It will be subject to change because I am still working on the GUI subject.
Mariusz PostolPosted Jul 1, 2024, 8:00 PM
@George Thanks for your responses. You did ask me for advice. My advice is that "Programming in Practice - GUI - MVVM Program Design Pattern" - my new article submitted, not yet published.
Mariusz PostolPosted Jul 1, 2024, 1:46 PM
What is the "formal physical pattern" you mentioned?
Mariusz PostolPosted Jun 10, 2024, 7:39 PM
If you are talking about the program architecture are you talking about design time or run-time alternatively? The same relates to communication is it design time or run-time activity? We must distinguish both although closely related, namely, the program describes behavior.
Mariusz PostolPosted Jun 10, 2024, 7:35 PM
You have connected the Model Layer to the Database in the conceptual model. You also mentioned that MVVM is part of WPF - Windows Presentation Foundation - as the name says the presentation layer. What about Data and Logic (Business Logic or Service ) layers? It looks like inconsistency. My point is that the MVVM model is three sublayers of the presentation layer and must not be used to gain access to a database.