Original: MVC, MVP, MVVM comparison and difference (UP)
MVC, MVP, and MVVM are all patterns used to solve the problem of interface rendering and logical code separation. Previously only a partial understanding of them, no in-depth research, for some of the concepts and differences are also smattering. Now, while looking at the information, and in combination with their own understanding, to talk about the three models of the idea of understanding, and their differences. Welcome you to shoot bricks.
Read the catalogue:
A. MVC, MVP, the need for the birth of MVVM?
Two. A typical coupling code
Three. Mvc mode
3.1 Active MVC
3.2 Passive MVC
3.3 The MVC framework in Web applications
3.4 MVC Summary
One, MVC, MVP, the need for the birth of MVVM?
What is the most basic thing in software? Yes, it's data. All the code we write is around the data.
The business logic emerges around the change of data generation and modification.
Around the display of data, there are different interface technologies.
Without well-designed code, it is often the case that the data layer (persistence layer) and the business Logic layer and interface code coupling are present.
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 a one-to-one object.
and MVC, MVP, MMVM is used to solve the coupling between business logic and views.
Two, a typical coupling code
{SqlDataAdapter adapter=NewSqlDataAdapter ("SELECT * from Table1","server=.; Database=db;uid=sa;pwd=password");D ataset ds=NewDataSet ("DS1"); adapter. Fill (DS); This. Gridview1.datasource =ds; This. Gridview1.databind ();}
The above code contains both the data access and the page presentation. When the project is more complex, the code becomes very difficult to maintain and the hierarchy is not clear.
Third, the MVC pattern
The full name of MVC is the Model View controller, which is the abbreviation for the models-view-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 using MVC is to separate m and v implementation code, so that the same program can use different representations
3.1 Active MVC
MVC's theoretical thinking corresponds to active MVC, where the initiative means that the model will proactively notify the view update. And we use the MVC framework, Struts, ASP. NET MVC, etc. are not active MVC (view updates are all done by the Controller)
Model
Used to encapsulate data related to the business logic of the application and how to handle the data.
Changes to the data in the model are generally advertised 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 have occurred on the data model.
View
The view layer is responsible for the presentation of the data.
There is generally no logic on the program in the view. To implement the Refresh feature on the view, the view needs to access the data model it monitors, so you should subscribe to the model's events beforehand in the data it is monitoring.
Controller
The controller is a connector between M and V that controls the flow of the application. It handles the event and responds. An "event" includes the user's behavior and changes on the data model.
3.2 Passive MVC
is a process in passive MVC , unlike active MVC , where view does not subscribe to the event of a model data change, waits for model to notify that the view needs to be updated based on the new data. In passive MVC , the controller is responsible for notifying the view that there are data changes that need to be updated.
in passive MVC, the difference from active MVC is that:
1. The model is ignorant of the view and controller, it is only used by them
2, the controller uses the view, and notifies it to update the data display
3. The view only does this when the controller notifies it that the model is fetching data (the view does not subscribe to or monitor the update of the model)
3.3. The 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, it does not immediately update the client view when the server-side model (data) changes. Updates only if the client requests or refreshes the page again.
is a typical MVC framework in which MVC is a request process.
3.4 MVC Summary
MVC Benefits
- Because MVC separates the view layer and the business layer nicely, it has the following advantages
- Low coupling resistance
- Fast development speed
- High maintainability
- No control concept, no encapsulation of HTML, easy to understand
- More similar to other platforms (Java, PHP). Easy access to Talent
The pitfalls of MVC use
1. The model is understood as a physical class (entity), and in MVC the model should contain 2 parts, part of the business logic, and part of the data that provides the view display
2. Put all the business logic on the controller side
These two misunderstandings are essentially the role of the model of unknown cause.
The role of model in the MVC architecture is very important, and it should be the real implementation layer of the business logic. So the model is actually business model. The controller only plays a "bridge" role, it is responsible for the view request to the model, and then responsible for the model processing end of the message to notify the view. The controller is used to decouple the view and model, specifically to separate the UI from the logic (the interface is separated from the code).
Quoted Http://www.techopedia.com/definition/27454/model-mvc-aspnet
Techopedia explains Model (MVC)
The Model is the part of MVC which implements the domain logic. In simple terms, this logic is 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 is stored under the Models folder in ASP. 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.
The drawbacks of MVC
The perfect MVC application scenario should look like this:
There is a student Model, associated Studentlistview, Studenteditview.
For Studentlistview, the Student model provides Student collection data to display Studentlistview
For Studenteditview, the Student model provides a single Student of 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 show student information, may also need this student historical results, family situation, teacher information. And these are not available in the student model.
Perhaps we can expand the student model to extend the information that student model can provide, including performance information, which itself can be. However, if Student shows the view, this need only need additional information, another view just need additional family information, Student model is not a bit busy, you can know how many different view needs? And let the logic-side code change so constantly to fit the view side, okay?
Since the design of MVC is based on the model, and does not take into account the complexity of the view side, the problem is that model is difficult to meet the complex and changeable view-side changes.
In contrast, MVP and MVVM are much better. They are all independent of presenter and ViewModel to correspond to each view.