MVC, MVP and MVVM are all used to solve the pattern of interface rendering and logical code separation. In the past, there is only part of the understanding of them, not in-depth research, for some of the concepts and differences are also a smattering. Now look at the information, and combine their own understanding, to talk about the three models of the idea of understanding, and their differences. Welcome to all of you master shot bricks.
Read the Table of contents:
Copy Code code as follows:
A. MVC, MVP, mvvm the birth of the demand?
Two. A typical coupling code
Three. MVC pattern
3.1 Active MVC
3.2 Passive MVC
MVC framework in 3.3 Web applications
3.4 MVC Summary
One, MVC, MVP, mvvm the birth of the demand?
What is the most important and basic thing in software? Yes, it's data. All the code we write is around the data.
Around the data generation, change, and other changes, the business logic emerged.
Around the display of data, there are different interface technologies.
Without well designed code, there is often a case where the data layer (persistence layer) and the business logic layer are coupled with the interface code.
ORM Frameworks, which decouple the coupling between business logic and data, business logic no longer cares about how the underlying data is stored and read. All the data presented to the business logic layer is one object.
While MVC, MVP, MMVM is used to solve the coupling between business logic and views.
Two, a typical coupling code
Copy Code code as follows:
{
SqlDataAdapter adapter = new SqlDataAdapter ("select * from Table1", "server=.; Database=db;uid=sa;pwd=password ");
DataSet ds = new DataSet ("DS1");
Adapter. Fill (DS);
This. Gridview1.datasource = ds;
This. Gridview1.databind ();
}
The above code contains both data access and a page display. When the project is more complex, the code becomes very difficult to maintain and the hierarchy is not clear.
Third, the MVC pattern
The MVC full name is Model View Controller, which is the abbreviation for Models-View-Controller (Controller). MVC begins in the desktop program, M refers to the business model, v refers to the user interface, C is the controller, the purpose of MVC is to separate the implementation code of M and v so that the same program can use different representations
3.1 Active MVC
The theory of MVC corresponds to active MVC, where the initiative means that model will actively notify the view update. And we use the MVC framework, Struts, asp.net mvc, etc. are not active MVC (the view update is done through controller)
Model
Used to encapsulate data related to the business logic of the application and how to handle the data.
Changes in the data in the model are generally disclosed through a refresh mechanism. To implement this mechanism, the views that are used to monitor this model must be registered in advance on this model, so that the view can understand the changes that occur on the data model.
View
The view layer is responsible for displaying the data.
There is generally no procedural logic in the view. To implement the Refresh feature on the view, the view needs to access the data model it watches, so you should subscribe to the model event in advance of the data that it watches.
Controller
The controller is a connector between M and V that controls the flow of the application. It handles events and responds. "Events" includes user behavior and changes on the data model.
3.2 Passive MVC
The following figure is the process in passive MVC, unlike active MVC, where view does not subscribe to the model data changes, waiting for model to notify you of the need to update the view based on the new data. In passive MVC, controller is responsible for informing view, data changes, and the need to update the view.
In passive MVC, the difference from active MVC is:
1, the model of the view and the controller is ignorant, it is only used by them
2, the controller uses the view, and informs it to update the data display
3, the view is only when the controller informs it to the model to fetch data when it does so (the view does not subscribe or monitor the update of the model)
3.3. MVC framework in Web applications
The MVC framework in the web is a passive MVC pattern because in Web applications, because HTTP works together based on request and response, when the server-side model (data) changes, it does not immediately update the client's view, only when the client requests or refreshes the page.
The following figure is an MVC request process in a typical MVC framework.
3.4 MVC Summary
MVC Benefits
• Because MVC separates the view and business layers Well, it has the following advantages
• Low coupling
• Fast development speed
• High maintainability
• No concept of controls, no encapsulation of HTML, easy to understand
• More similar to other platforms (Java, PHP). Easy access to Talent
The pitfalls of using MVC
1. Understanding model as entity Class (Entity), in MVC model should contain 2 parts function, part is to handle business logic, part is to provide view display data
2. Put the business logic all on the controller side
These two misunderstandings are essentially the role of model of the unknown cause.
Model plays an important role in the MVC architecture, and it should be the real implementation layer of the business logic. So the model is actually business model (business models). and controller only a "bridge" role, it is responsible for the view request to model, and then responsible for the model processing end of the message to inform view. Controller is used to decouple view and model, specifically in order to separate the UI from the logic (the interface is separated from the code).
Quote from Http://www.techopedia.com/definition/27454/model-mvc-aspnet
Copy Code code as follows:
Techopedia explains Model (MVC)
The Model is the part of the MVC which implements the domain logic. In simple terms, this logic are used to handle the data passed between the database and the user interface (UI).
The Model is known as domain object or domain entity.
The domain objects are stored under the Models folder in asp.net. The domain model represents the application perspective for the "data to be handled whereas a view model are required to pro Duce the engine that generates the View.
This definition is written in the context of asp.net.
The drawbacks of MVC
The perfect MVC scenario should be this:
There is a student Model, associated Studentlistview, Studenteditview.
For Studentlistview, Student model provides Student collection data to display Studentlistview
For Studenteditview, Student model provides a single Student data to show Studenteditview and respond to studenteditview save operations.
But this is only the perfect situation, the actual application, in the ListView, not only displays the student information, may also need this student historical achievement, the family situation, the teacher information. And these are student model can not provide.
Perhaps we can expand student model, the student model can provide information expansion, including performance information, which itself can also. However, if Student display view, this need only need additional information, another view just need additional family information, Student model is not a bit tired, you can know how many different view needs? And let the logic-side code keep changing to fit the view, okay?
Since the design of MVC is based on model, without considering the complexity of the view side, the problem is that model is difficult to meet the complex and changeable view-end changes.
In contrast, MVP and MVVM are much better off. They are all independent of presenter and ViewModel to correspond to each view.