Dare to differentiate between MVC, MVVM, and MVP, dare to mvcmvvmmvp
MVC, MVP, and MVVM are both modes used to solve the problem of interface rendering and logical code separation. In the past, I only had a partial understanding of them, but I have not studied them in depth. I also have some knowledge about the concepts and differences in them. Now let's look at the information and understand the differences between the three models. You are welcome to shoot bricks.
Reading directory:
I. What are the requirements of MVC, MVP, and MVVM?
2. A typical coupling code
Iii. MVC Mode
3.1 active MVC
3.2 passive MVC
3.3 MVC Framework in Web Applications
3.4 MVC Summary
I. What are the requirements of MVC, MVP, and MVVM?
What is the most important and basic thing in software? Yes, it is data. All the code we write is centered on data.
There is a business logic around data generation, modification, and other changes.
There are different interface technologies around the data display.
Without well-designed code, the data layer (persistent layer) and the business logic layer often have interface code coupling.
ORM and other frameworks, coupled with the coupling between business logic and data, business logic no longer cares about how the underlying data is stored and read. All data is presented to the business logic layer as objects.
MVC, MVP, and MMVM are used to solve the coupling between business logic and views.
2. A typical coupling code
{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 code above contains both data access and page display. When the project is more complex, the Code becomes very difficult to maintain and the layers are not clear.
Iii. MVC Mode
The full name of MVC is Model View Controller, short for model-view-controller. MVC first exists in the desktop program, M refers to the business model, V refers to the user interface, and C refers to the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different forms of representation
3.1 active MVC
The MVC theory corresponds to the active MVC.ActiveThe Model automatically notifies the View to update. But we use the MVC Framework, Struts, asp. NET mvc, and so on, which are not active MVC (view updates are all done through the Controller)
Model
It is used to encapsulate data related to the application's business logic and to process the data.
Data changes in the model are generally published through a refresh mechanism. To implement this mechanism, the views used to monitor this model must be registered on this model in advance, so that the views can understand the changes in the data model.
View
The view layer displays data.
Generally, there is no program logic in the view. To refresh a view, the view needs to access the data Model it monitors. Therefore, you should subscribe to Model events in the data monitored by the view in advance.
Controller
A controller is a connector between M and V used to control the process of an application. It processes events and responds. "Events" include user behaviors and changes in data models.
3.2 passive MVC
It is a passive MVC process. Unlike the active MVC process, the View does not subscribe to the event of Model data changes. The View needs to be updated based on new data when the Model is notified. in passive MVC, the Controller notifies the View that the View needs to be updated due to data changes.
In passive MVC, the difference from active MVC lies in:
1. The model knows nothing about views and controllers. It is only used by them.
2. The controller uses the view and notifies it to update the data display.
3. The view does this only when the Controller notifies it to fetch data from the model (the view does not subscribe to or monitor model updates)
3.3. MVC Framework in Web Applications
The MVC Framework in the Web is passive MVC mode, because in web applications, http works collaboratively based on request and response methods, so when the model (data) on the server changes, it does not update the view of the client immediately. It is updated only when the client re-requests or refreshes the page.
It is a request process in a typical MVC framework.
3.4 MVC Summary
Advantages of MVC
- Because MVC separates the view layer from the business layer, it has the following advantages:
- Low Coupling
- Fast Development
- High maintainability
- No control concept, no html encapsulation, easy to understand
- It is more similar to other platforms such as java and php. Easy talent acquisition
Mistakes in using MVC
1. Understand the Model as an Entity class. In MVC, the Model should contain two functions, one is to process the business logic, and the other is to provide the data displayed by the View.
2. Put all the business logic on the Controller side
These two misunderstandings are essentially caused by unclear roles on the Model.
Model plays an important role in the MVC Architecture. It should be the real Implementation Layer of business logic. Therefore, the Model is actually a Business Model ). The Controller serves only as a "bridge". It is responsible for forwarding the View request to the Model, and then for notifying the View of the message processing end of the Model. Controller is used to decouple views and models. Specifically, it is used to separate the UI from the logic (interface and code ).
Self-built 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 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 is required to produce the engine that generates the View.
This definition was written in the context of ASP.NET.
Disadvantages of MVC
The perfect MVC application scenario should be like this:
There is a Student Model associated with StudentListView, StudentEditView.
For StudentListView, Student Model provides Student set data to display StudentListView
For StudentEditView, the Student Model provides a single Student data to display the StudentEditView and responds to the storage operation of the StudentEditView.
However, this is only a perfect situation. In actual application, the history score, family status, and teacher information of Student may not only be displayed on ListView. These are not provided by Student Model.
Maybe we can extend the Student Model to extend the information that Student Model can provide, including the score information. However, if Student shows a View, it only requires additional score information, and another View only requires additional family information. Is the Student Model a little exhausted, how many different View requirements do you have? And let the logic end code be constantly modified to adapt to the View end, OK?
Because the MVC design idea is based on the Model without considering the complexity of the View end, the problem is that the Model is difficult to adapt to the complex and changing View end changes.
MVP and MVVM are much better than above. They all have their own Presenter and ViewModel to correspond to each View.