In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the differences between MVC, MVP and MVVM in ASP.NET". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Read the catalogue:
four。 MVP mode
4.1 the thought of MVP
4.2 UI interface interface
4.3 Presenter-the bridge between Model and View
4.4Code structure and timing diagram of MVP
4.5 MVP model summary
five。 MVVM mode
Design idea of 5.1 MVVM pattern
5.2 MVVM schema structure diagram
six。 Summary of usage scenarios for MVC, MVP and MVVM modes
Fourth, MVP mode
MVP mode is also a classic interface mode. M in MVP stands for Model, V is View, and P is Presenter.
The complete code in the following example can be downloaded here: WinformMVP source code
You can also compare this article to analyze the problem of Vmurp interaction and case sharing in MVP mode.
4.1 the thought of MVP
The MVP pattern, in my opinion, is a true pattern that isolates the details and complexities of View. Why do you say so:
Because in other modes V represents the UI interface, which is a html page, XAML file or winform interface. But V in MVP mode represents an interface, an interface that abstracts the UI interface. The interface means that any interface that implements the interface can reuse existing Presenter and Model code.
4.2 UI interface interface
To have a good understanding of MVP, you need the ability to interface the UI interface. Look at the interface below. By abstracting the User Control marked in red, you can get the following interface
The copy code is as follows:
Public interface IUserAdd
{
Event EventHandler UserAddEvent
String UserName {get; set;}
String UserAge {get; set;}
}
The two input boxes in the interface are abstracted into UserName and UserAge attributes. The click event of the Save button is abstracted into the event UserAddEvent. The code that implements this interface in winform is as follows:
The copy code is as follows:
Public partial class UserAdd: UserControl, IUserAdd
{
Public event EventHandler UserAddEvent
Public string UserName
{
Set {this.txbName.Text = value;}
Get {return this.txbName.Text;}
}
Public string UserAge
{
Set {this.txbAge.Text = value;}
Get {return this.txbAge.Text;}
}
Public UserAdd ()
{
InitializeComponent ()
}
Private void btnAdd_Click (object sender, EventArgs e)
{
If (UserAddEvent! = null) UserAddEvent (this, e)
}
}
Let's use the UserAge attribute to explain the magic of UI interface interfacing. When the back-end code wants to get the age value on the interface, it only needs the get attribute, and when it wants to update the interface display, it only needs the set attribute.
At this time, the operation of the back-end code on the interface is abstracted into the operation of the UserAge attribute, which has nothing to do with the specific display of the interface.
4.3 Presenter-the bridge between Model and View
The back-end code mentioned above includes P and M. M, as in MVC, refers to logical code. P is the bridge between Model and View, which is responsible for combining the corresponding Model and View.
For the above IUserAdd, the corresponding Presenter code is:
The copy code is as follows:
Public class UserAddPresenter:IPresenter
{
Private readonly IUser _ model
Private readonly IUserAdd _ view
Private readonly ApplicationFacade _ facade = ApplicationFacade.Instance; / / the facade here is for communication between Presenter. For more information, please see the complete code.
/ / in the Presenter constructor, pass view and model as parameters
Public UserAddPresenter (IUser model, IUserAdd view)
{
_ model = model
_ view = view
WireUpViewEvents ()
}
Private void WireUpViewEvents ()
{
_ view.UserAddEvent + = _ view_UserAdd
}
/ when the UserAdd event of view is triggered, get the data in UI, call model logic processing, and add new users.
/ / send User_ADDED messages to the system at the same time (other UI parts of the system receive messages, such as DataGrid here, which will be refreshed after receiving User_ADDED)
Private void _ view_UserAdd (object sender, EventArgs e)
{
Var user = new User
{
Name = _ view.UserName
Age = Convert.ToInt32 (_ view.UserAge)
}
_ model.AddItem (user)
_ facade.SendNotification (ApplicationFacade.USER_ADDED)
}
}
4.4Code structure and timing diagram of MVP
The code structure diagram and sequence diagram in MVP here can better help to understand the MVP pattern.
4.5 MVP model summary
In MVP, Presenter completely separates Model from View, and the main program logic is implemented in Presenter. Moreover, the Presenter is not directly related to the specific View, but interacts through a defined interface, so that the Presenter can be kept unchanged when changing the View, that is, reuse! Not only that, we can also write View for testing to simulate the various actions of users, thus realizing the testing of Presenter-- without using automated testing tools. We can even test the logic of Model by writing Mock Object (that is, implementing the interface between Model and View, but with no specific content) when neither Presenter nor Presenter is finished.
Advantages of MVP
1. The model is completely separated from the view, and we can modify the view without affecting the model
2. The model can be used more efficiently, because all the interactions take place in one place-within the Presenter
3. We can use a Presener for multiple views without changing the logic of the Presenter. This feature is very useful because views always change more frequently than models.
4. If we put the logic in Presenter, then we can test the logic out of the user interface (unit tests)
Fifth, MVVM model
Design idea of 5.1 MVVM pattern
In MVVM mode, a ViewModel matches a View. It does not have the IView interface in MVP, but is completely bound to View. All changes in View are automatically updated to ViewModel, and any changes in ViewModel are automatically synchronized to the View.
The reason for this automatic synchronization is that the properties in ViewModel implement interfaces such as observable, that is, when the set method of the property is used, the event of property modification will be triggered at the same time, and the bound UI will be refreshed automatically. (in WPF, this observable interface is implemented by INotifyPropertyChanged; in knockoutjs through the functions ko.observable () and ko.observrableCollection ().)
Therefore, MVVM is one step more upgraded than MVP. In MVP, V is the interface IView to solve the coupling of interface UI, while MVVM simply uses the seamless combination of ViewModel and UI, and ViewModel can directly represent UI. But MVVM does this by relying on specific platforms and technologies, such as WPF and knockoutjs, which is why ViewModel does not need to implement interfaces, because depending on specific platforms and technologies, using MVVM pattern can not replace the UI platform.
5.2 MVVM schema structure diagram
Here is the structure diagram of the MVVM pattern, which can help you understand the MVVM pattern more easily:
Summary of the usage scenarios of MVC, MVP and MVVM modes
Because it is impossible to support bi-directional binding of data and interface and event monitoring like WPF in winform, MVP is the best choice in winform.
Knockout is used in the interface of WPF and html, and observable is implemented, so MVVM. (it should be said that WPF is designed to use MVVM.)
In web applications, because http works together based on request and response, it can not keep the connection state all the time, so it is impossible to achieve the message delivery between Presenter in MVP and the binding between ViewModel and interface in MVVM, so MVC is the best choice.
This is the end of the content of "what are the differences between MVC, MVP and MVVM in ASP.NET". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.