Differences between Model-View-viewmodel and MVC and MVP design modes
The Model-View-viewmodel design mode is the product of combining the MVP design mode with the WPF technology. It is common for Model-View-controller (MVC) design Patterns and Model-View-Presenter (MVP) design patterns have a major drawback when used in WPF: that is, they did not take into account the powerful data binding technology of WPF at all. In these modes, the Controller or presenter is responsible for view behavior, such as filling textbox text content, loading ListBox, and filling table content. These can be easily solved in WPF, because one of the biggest advantages of WPF is its rich data binding capabilities. If you use the MVC or MVP design mode in WPF, you may not be able to use the convenience of Data Binding in WPF, but the Model-View-viewmodel is different.
Definition of Model-View-viewmodel
Model-View-viewmodel was first introduced by John gossman. If you are interested, please refer to "http://blogs.msdn.com/johngossman/archive/2005/10/08/478683.aspx.pdf ".Article. This model provides a pure model, creates an abstract view to hold the State, and binds data from a view created by the visual designer to an abstract view. The visualization designer is mainly a Microsoft Expression blend tool, and the abstract view is a viewmodel. The key point is to bind data to a two-way connection between the view and viewmodel models. When this design pattern is correctly used, each view except the pure XAML and a very small number of backendCodeIt does not contain anything, and completely achieves the separation of interface display and business logic, so thatProgramMembers are more focused on coding.
Viewmodel
The role of viewmodel is to make mdoel more suitable for view. For example, if you have a method that returns a generic set of ilist <tentity>, but you may actually convert it to a more friendly class for WPF to facilitate data binding. Through viewmodel, you can convert the ilist <tentity> type model set into something like collectionview to facilitate data binding of UI interface elements in WPF, the key to completing this task is to expose a public attribute in viewmodel. Like controlller, viewmodel can also be used to hold the view status and execute any commands required by the view.
Since WPF has a built-in command mode, all UI elements such as button controls have a command attribute, which is the icommand type defined by WPF. You can put these commands in viewmodel and expose them in the form of public attributes, so that view can be bound to them. This is extremely powerful because it can bind executable code in modelview to the button of the form.
Demo
The following is a simple WPF application example using the Model-View-viewmodel design mode. If you are interested, you can download the code. The background uses common northwnd databases:
The related class relationship diagram is as follows:
If you are interested, you can download the relevant source code: demo.