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.
Processing flow
For the difference between the two aspects of the processing process, the following graphs are used to illustrate everything:
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, selects 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.
View Difference
View in the ASP. NET MVC framework can be an ASP. NET page, user control, or master page. It is required to inherit s from ViewPage, Viewusercontrol, and ViewMasterPage respectively. Example code:
public partial class views_blog_new:viewpage{ }
Using inline code for data rendering, of course, you can also use server controls, sample code:
In MVP, the WebForm model is still used, where view is divided into the view interface and the view implementation, which can be either an ASP. NET page, a user control, or a master page:
Public interface iproductdetail{ string Name {set;} String Brand {set;}}
Public partial class Products_productdetail:page, iproductdetail{}
Use server controls for rendering (or HTML controls):
<asp:content id= "Content" contentplaceholderid= "defaultcontent" runat= "Server" > public class postcontroller:controller{ [controlleraction] public void New () { Renderview ("New"); } }public class blogcontroller:controller{ [controlleraction] public void New () { Renderview ("new"); }}
A view in the MVP only corresponds to a specific presenter in the entire application:
Public partial class Products_productdetail:page, iproductdetail{ private productdetailpresenter _presenter; protected void Page_Load (object sender, EventArgs e) { if (!this. IsPostBack) { this._presenter. Onviewinitialized (); } This._presenter. Onviewloaded (); } [CreateNew] Public Productdetailpresenter Presenter { set { this._presenter = value; This._presenter. View = This;}} }
Impact on the development processIn 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:
Support for Unit testsWith support for unit testing, the ASP. NET MVC framework, when present, facilitates clear separation of concerns, testability, and TDD. So the core contract in MVC Framewrok is interface-based and can be easily simulated by mock. You can do unit testing without running the controller in an ASP. You can also use any unit test framework you want to use for unit testing, including NUnit, Mbunit, MS test, and more.
The MVP pattern appears in part to facilitate unit testing of the UI. Because all the processing is in the presenter, the code in view becomes clean and simple, so it is easy to unit test, Web Client Software Factory, provides the Automation project guidance package, can create the test project directly.
ConclusionFor the small comparison of the MVP model in the ASP. NET MVC framework and WCSF, it's over here, whether it's the ASP or the MVP model in the WCSF, it's a great model and deserves a bit more research.
MVC and MVP (GO)