In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand MVC, MVP, MVVM in ASP.NET". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Read the catalogue:
one。 What are the requirements for the birth of MVC, MVP, MVVM?
two。 A typical piece of coupling code
three。 MVC mode
3.1 active MVC
3.2 passive MVC
3.3 MVC framework in Web applications
3.4 MVC Summary
First, the demand for the birth of MVC, MVP, MVVM?
What is the core and basic thing in the software? Yes, it's data. All the code we write revolves around data.
Around the data generation, modification and other changes, the emergence of business logic.
Around the display of data, there are different interface technologies.
Without well-designed code, there is often a coupling between the data layer (persistence layer), the business logic layer and the interface code.
Frameworks such as ORM decouple the coupling between business logic and data, and business logic no longer cares about how the underlying data is stored and read. All the data presented to the business logic layer are individual objects.
MVC, MVP, MMVM are used to solve the coupling between business logic and view.
Second, a typical coupling code
The copy code is 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 page presentation. When the project is more complex, the code becomes very difficult to maintain and the hierarchy is not clear.
Third, MVC mode
The full name of MVC is Model View Controller, which is an acronym for model-view-controller. MVC initially exists in desktop programs. M refers to the business model, V refers to the user interface, and C is 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 expression.
3.1 active MVC
The theory of MVC corresponds to active MVC, which means that Model actively notifies View of updates. While we use the MVC framework, Struts, asp.net mvc, etc., are not active MVC (view updates are done through Controller)
Model
It is used to encapsulate the data related to the business logic of the application and the method of processing the data.
Changes in the data in the model are generally published through a refresh mechanism. In order to implement this mechanism, the views used to monitor the model must be registered on the model in advance so that the view can understand the changes that have taken place on the data model.
View
The view layer is responsible for the display of data.
There is generally no procedural logic in the view. In order to refresh the view, the view needs to access the data model it monitors (Model), so you should subscribe to Model events from the data it monitors in advance.
Controller
The controller is the connector between M and V that controls the flow of the application. It processes events and responds. "events" include changes in the user's behavior and data model.
3.2 passive MVC
The following figure shows the process in passive MVC. Unlike active MVC, View does not subscribe to events of Model data changes and waits for Model to notify that View needs to be updated according to the new data. In passive MVC, Controller is responsible for notifying View that there is a data change and that the view needs to be updated.
In passive MVC, it differs from active MVC in:
1. The model knows nothing about views and controllers, it is just used by them
2. The controller uses the view and tells it to update the data display.
3. The view does this only when the controller tells it to fetch data from the model (the view does not subscribe to or monitor model updates)
3.3. MVC Framework in Web Application
The MVC framework in Web is passive MVC mode, because in web applications, because http works together based on request and response mode, when the model (data) of the server changes, it will not update the view of the client immediately, but only when the client requests or refreshes the page.
The following figure shows a request flow of MVC 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
The development speed is fast
High maintainability
No concept of control, no encapsulation of html, easy to understand
More similar to other platforms (java, php), etc. To facilitate the acquisition of talents
Misunderstandings in the use of MVC
1. Understand Model as an entity class (Entity). In MVC, Model should contain two parts of functions, one is to process business logic, and the other is to provide data displayed by View.
two。 Put all the business logic on the controller side
These two misunderstandings are essentially caused by the unclear role of Model.
Model plays a very important role in MVC architecture, and it should be the true implementation layer of business logic. So Model is actually Business Model (business model). On the other hand, Controller only acts as a "bridge". It is responsible for forwarding View's request to Model, and then notifying View of the end of Model processing. Controller is used to decouple View and Model, specifically, to separate UI from logic (interface from code).
The copy code is as follows:
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.
Shortcomings of MVC
The perfect MVC application scenario would look like this:
There is a Student Model associated with StudentListView, StudentEditView.
For StudentListView, Student Model provides the collection data of Student to display StudentListView
For StudentEditView, Student Model provides a single Student data to display the StudentEditView and respond to the save operation of StudentEditView.
But this is only a perfect situation, in practical application, in ListView, not only the information of Student is displayed, but also the historical score, family situation and teacher information of this Student may be required. These are things that Student Model cannot provide.
Maybe we can extend Student Model, expand the information that Student Model can provide, including score information, etc., which itself can also. However, if Student shows View, this need only requires additional score information, another View just needs additional family information, is Student Model a little tired, can you know how many more differentiated View needs? And let the logical side code be constantly modified to adapt to the View side, okay?
Because the design idea of MVC is based on Model without taking into account the complexity of the View side, the problem is that it is difficult for Model to meet the complex and changeable changes of the View side.
MVP and MVVM are much better at this point. They are all independent of Presenter and ViewModel to correspond to each View.
This is the end of the introduction of "how to understand MVC, MVP, MVVM in ASP.NET". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.