Model-view-presenter (MVP) Overview
The MVC pattern has been around for decades and has been widely used in the GUI domain, and since the advent of the Microsoft ASP. NET MVC Framework, MVC has become a hot topic for the. The variant MVP model of MVC has been around for years, and in the Web Client software factory provided by the Microsoft Model and Practice group, application best practices for implementing the MVP pattern are given, and this article will try to compare the two implementations by one or two.
MVC (Model-view-controller, model-View-Controller) mode is a kind of software design pattern appearing in the 80 's Smalltalk-80, which has been widely used, and its main purpose is to promote the clear separation of the concerns between models, views and controllers in the application. MVP (Model-view-presenter, model-view-representation) mode is an IBM-developed programming model for C + + and Java, probably in the 2000, a variant of the MVC pattern that is used to isolate UI, UI logic, and business logic, data. In the following text, MVC refers to the ASP. NET MVC Framework, if not specifically described.
Model-view-presenter (MVP) pros and cons
In this example, the MVP model uses the castle framework and the underlying data map NHibernate framework for the ASP. NET MVP sample instance, in the development process should pay attention to the NHibernate version of the different, some support SQL Server 2000, and some support SQL Server 2005 databases. The implementation of the IHttpModule interface. In fact, when using the Castle framework, the Icontaineraccessor interface has encapsulated IOC mode. There is also generic programming. The transaction rollback operation. During the programming process, you can keep its existing pattern, or you can add or change its pattern.
The primary goal of Model-view-presenter is to decouple the display logic from the business logic, as we design object-oriented programs to create loosely coupled and reusable objects, designed for application layering and improved testing efficiency.
Another goal of MVP is to improve the efficiency of testing for view. Writing unit test classes that rely on sessions, ViewState, AJAX, HTML or Web controls and business entities is more complex, so we keep the display logic of each view in the Aspx/ascx file class and separate the business logic from the corresponding classes. In MVP, presenter acts as a buffer layer for views and business logic.
MVP and MVC The Difference
Mvp--model-view-presenter It is a variant of the MVC pattern. The UI is easy to change and diverse, and the same data is displayed in n ways, and business logic is relatively easy to change. To make application more resilient, we want to isolate the UI, logic (UI logic and business logic) and data, and MVP is a good choice.
Presenter replaced the controller, and it was more complicated than the controller's task. The presenter handles the events and executes the corresponding logic, which maps to the model's command to manipulate the model. The code that handles how the UI works is basically in presenter. Presenter acts as a conductor of a band, performing and coordinating the entire application, which is responsible for creating and coordinating other objects.
Model and view communicate using observer mode, while presenter and view communicate using mediator mode, and the presenter operation model is performed using command mode. The basic design is the same as MVC: Model stores data, view represents model performance, and presenter coordinates communication between the two. In MVP, the View receives events and then passes them to the Presenter, and how to handle these events is done by Presenter.
Figure 1:model-view-controller
Figure 2:model-view-presenter
In the process, in MVC, the user's request first arrives at the controller, the controller obtains the data from the model, chooses the appropriate view, renders the processing result to the view, in the MVP, the user's request first arrives at the view, The view passes the request to a specific presenter,presenter after retrieving the data from the model, and then passes the processing result through the interface to the view.
With the MVP, we can improve the reuse of model and presenter, such as the model and the presenter without modification, but can provide ASP. NET Web form and Windows form.
In the ASP. NET MVC framework, the data is presented in inline code, and the logic is centered in the controller, but view cannot be fully delivered to the UI designer. In MVP mode, all the business logic is given to presenter, so that the code in view becomes simple and easy to separate the developer from the UI designer, as shown in:
MVP Example Explanation
Let's look at a simple example:
This method creates presenter, passes the view and model, and calls the function of the "Initview" method to the Ascx user control (View) processing. View application The corresponding Presenter,presenter only know the interface of the view. ASPX pages are used only to add user controls, so you can easily reuse them by simply dragging them onto the page.
public class Presenter
{
Public Presenter (IView view, IModel model)
{
This.view = view;
This.model = model;
}
public void Initview (bool isPostBack)
{
if (!ispostback)
{
View. Setproducts (model. GetProducts ());
}
}
public void Saveproducts (ilist<iproduct> products)
{
Model. Saveproducts (products);
}
}
Page or user control CS code
protected override void OnInit (EventArgs e)
{
Base. OnInit (e);
Presenter = new presenter (This,model);
Presenter. Initview (Page.IsPostBack);
}
public void Setproducts (ilist<iproduct> products)
{
Bind products to view
}
View interface
public interface IView
{
void Setproducts (ilist<iproduct> products);
}
With the code above you can see what the structure of the MVP is, and you can develop your project based on this pattern. Of course you can also download a demo from the CodePlex website for further understanding. I hope this article will be useful to everyone.
Enterprise-Class MVP architecture application
using MVP in Enterprise-Class ASP.
1. Use user controls to encapsulate Views: This topic discusses user controls as a view in MVP.
2. MVP Event Handling: This topic discusses the passing of events to Presenter,ispostback and the delivery of messages to view, along with page validation.
3, MVP and Pagemethods page redirection: This topic discusses using a user control as a view and how to use Pagemethods to handle page redirection.
4. MVP's Presentation Security Control: This topic discusses how to show/hide segments in a view based on basic security restrictions.
5. Architecture of application using MVP (Advanced): This is a key point, this topic shows a MVP application using NHibernate as the data access layer.
CodePlex The example on the website, contains a lot of content, we can download the analysis.
MVP Work Testimonials
This article, mainly to read the MVP framework, for an example of Microsoft MVP to explain some of the application patterns involved. Recently, the company project using the MVP framework to develop, for me a lot of harvest and feeling. For the MVP model to be developed, it should be considered a new architecture, since only the Microsoft MVP (Microsoft Most valuable Professional) was known, and the MVP (Model-view-presenter) was not known. Since came to the blog Park constantly learning, and constantly learn from, enrich their knowledge. Here to thank drummery and ξ ξ Two teachers of the article, but also borrowed from the UML Software engineering Organization website articles. MVP Mode Development project, I think the next few years will be more and more people use development projects. The MVP here, I think, should also be used to develop Windows software projects. This article is written in a relatively hasty, inevitably wrong place, at the same time I am also constantly digging the MVP of the deeper application. Here is my personal understanding, I hope the expert comments, if you have other understanding, you can discuss with me. I hope we all learn together and make progress together.
MVP architecture ....