What is the MVC mode?

Source: Internet
Author: User
Tags http request classic asp

The MVC mode is short for "Model-View-Controller". The Chinese translation is "mode-View-Controller ". The MVC application is always composed of these three parts. Event causes the Controller to change the Model or View, or change both. As long as the Controller changes the data or attributes of Models, all dependent views are automatically updated. Similarly, as long as the Controller changes the View, the View will retrieve data from the potential Model to refresh itself. The MVC pattern was first proposed by the smalltalk language research group and applied to user interaction applications. Smalltalk and java have many similarities. They are all object-oriented languages. Naturally, SUN recommends the MVC pattern as the architecture pattern for developing Web applications in the petstore case applications. The MVC mode is an architecture mode that requires collaboration between other modes. In the J2EE mode directory, the service to worker mode is usually used, while the service to worker mode can be composed of the centralized controller mode, the dispatcher mode, and the Page Helper mode. Struts only implements the View and Controller of MVC. The Model must be implemented by the developer. Struts provides the abstract class Action to enable the developer to apply the Model to the Struts framework. The MVC pattern is a complex architecture pattern, and its implementation is also very complicated. However, we have come to an end with a lot of reliable design patterns. The combination of multiple design patterns makes the implementation of the MVC pattern relatively simple. Views can be seen as a tree, which can be implemented by Composite Pattern. The relationship between Views and Models can be reflected by Observer Pattern. The Controller controls the display of Views, which can be implemented using Strategy Pattern. Model is usually a Mediator and can be implemented using Mediator Pattern.
Now let's take a look at the position of the three parts of MVC in The J2EE architecture, which helps us understand the implementation of the MVC model. The relationship between MVC and J2EE architecture is: View is in the Web Tier or Client Tier, usually JSP/Servlet, that is, the page display part. The Controller is also in the Web Tier, which is usually implemented using Servlet, that is, the logic part of page display. The Model is in the Middle Tier. It is usually implemented using the server's javaBean or EJB, that is, the implementation of the business logic.
I. MVC design ideas
Model-View-Controller (MVC) separates the input, processing, and output processes of an application in the form of Model, View, and Controller, such an application is divided into three layers: Model layer, view layer, and control layer.
A View represents a user interaction interface. For Web applications, it can be summarized as an HTML interface, but may be XHTML, XML, or Applet. With the complexity and scale of applications, interface processing becomes challenging. An application may have many different views. The MVC design mode can only process View data collection and processing and user requests, it does not include the processing of business processes in the view. The processing of business processes is handed over to the Model. For example, an order View only accepts and displays data from the model to the user, and transmits input data and requests from the user interface to the control and model.
Model: Processes business processes/states and formulates business rules. The processing process of a business flow is black box for other layers. The model accepts the data requested by the view and returns the final processing result. The design of business models is the core of MVC. Currently, the popular EJB model is a typical application example. It further divides the model from the perspective of application technology implementation to make full use of the existing components, but it cannot be used as the framework of the application design model. It only tells you that certain technical components can be used based on this model design, reducing technical difficulties. A developer can focus on the design of business models. The MVC design pattern tells us that the application model is extracted according to certain rules, and the extraction level is very important, which is also the basis for determining whether the developer is an excellent design. Abstraction cannot be separated from the specifics too far or too close. MVC does not provide a model design method. Instead, it only tells you to organize and manage these models to facilitate model reconstruction and improve reusability. We can use object programming as a metaphor. MVC defines a top-level class and tells its sub-classes that you can only do this, but you cannot limit your ability to do this. This is very important for programming developers.
Another important model of the business model is the data model. The data model mainly refers to the data storage (continuous) of object objects ). For example, save an order to the database and obtain the order from the database. We can list this model separately. All database operations are limited to this model.
The Controller can be understood as receiving requests from users, matching the model with the view to complete user requests. The division control layer is also very effective. It clearly tells you that it is a distributor, the model you choose, the view you choose, and the user requests you can complete. The control layer does not process any data. For example, when a user clicks a connection and the control layer accepts the request, it does not process the business information. It only transmits the user information to the model and tells the model what to do, select a view that meets the requirements and return it to the user. Therefore, a model may correspond to multiple views, and a view may correspond to multiple models.
The separation of models, views, and controllers allows a model to have multiple display views. If you change the model data through the controller of a view, all other views dependent on the data should reflect the changes. Therefore, the controller will notify all views of the changes at any time, resulting in display updates. This is actually a model change-propagation mechanism. The relationship between models, views, and controllers and their main functions are shown in Figure 1.


II. Implementation of the MVC design model
ASP. NET provides a similar environment for implementing this classic design mode. Developers develop user interfaces on the ASPX page to implement the view. The controller function is implemented in the logic function code (. cs). The model usually corresponds to the business part of the application system. A multi-layer system provided by implementing this design in ASP. NET has obvious advantages over the Classic ASP structure implementation system. Separating user display (view) from action (controller) improves code reusability. Separating data (models) from the action (controller) of operations allows you to design a system that is irrelevant to the data stored in the background. In essence, the MVC structure is a method to solve the coupling system problem.
2.1 View
A view represents a model and provides a user interaction interface. Using multiple user components that contain a single display page, a complex Web page can display content from multiple data sources, and the Web site staff can independently participate in the development and maintenance of these Web pages.
In ASP. NET, view implementation is simple. You can drag the control in the integrated development environment to complete page development, just like developing the WINDOWS interface. This article describes each page in the form of a composite view: a page consists of multiple subviews (User parts; A subview can be the simplest HTML control, server control, or Web custom control nested by multiple controls. The page is defined by the template. The template defines the page layout, the tag and number of user parts. You specify a template and the platform automatically creates a page based on the information. For static template content, such as site navigation, menus, and friendly links on the page, use the default template content configuration. For Dynamic Template content (mainly business content ), different user requests can only be bound later, and the display content of User parts is filtered for different users. Using a combination page composed of user parts configured according to the template, it enhances reusability and prototype the site layout.
The general process of the View section is as follows: first, the page template defines the page layout; the page configuration file defines the specific content of the view label (user part); then, the page layout policy class initializes and loads the page. Each user part initializes according to its own configuration, loads the validator, sets parameters, and delegates events. After the user submits the request, after the validation of the presentation layer, the user component automatically submits the data to the business entity, that is, the model.
This part mainly defines the basic PageBase of WEB pages, PageLayout of the page layout strategy class, complete the page layout, used to load User parts to the page, and UserControlBase of the user part base class is the user part framework, it is used to dynamically load inspection parts and personalize user parts. To achieve WEB application flexibility, many configuration files are also used in the View section, such as template configuration, page configuration, path configuration, and verification configuration.
2.2 Controller
In order to control and coordinate the processing of multiple requests for each user, the control mechanism should be managed in a centralized manner. Therefore, a controller is introduced to achieve centralized management. The application's controller centrally receives requests from the client (typically a user running a browser) and determines what business logic functions to execute, then, the responsibility for generating the next user interface is delegated to an appropriate view component.
The controller provides a centralized entry point for controlling and processing requests. It is responsible for receiving, intercepting, and processing user requests, and entrusting requests to the senders, the view presented to the customer is determined based on the current status and business operation results. In this section, HttpReqDispatcher, HttpCapture, and Controller are defined. They work together to implement the Controller functions. The requester class captures HTTP requests and forwards them to the controller class. The controller class is the initial entry point for processing all requests in the system. After the controller completes some necessary processing, it delegates the request to the sender class. The sender class issuer is responsible for the management and navigation of the view, which view is provided to the user for management, and provides distribution resource control. In this section, we adopt the distributor, policy, Factory method, adapter, and other design modes.
ASP. NET provides low-level request/response APIs for developers to use. NET Framework Class provides services for incoming HTTP requests. To this end, you must create a class that supports the System. Web. IHTTPHandler interface and implements the ProcessRequest () method: The request catcher class, and add a class in the <Httphandlers>
...
...
</Httphandlers>
2.3 model
The model in the MVC system can be divided into two types:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.