Introduction to MVC Mode

Source: Internet
Author: User
Tags classic asp
Http://www.itisedu.com/phrase/200604231324325.html

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 only allows you to collect and process view data.
And user requests, excluding the processing of business processes in the view. The processing of business processes is handed over to the model. For example, the view of an order only accepts data from the model and displays it to the user.
And pass the input data and requests on 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 an application design model.
Framework. 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. MVC design pattern notice
We extract the application model according to certain rules, and the extraction level is very important, which is also the basis for determining whether the developer is excellent in design. Abstraction cannot be separated from the specifics too far or too close. MVC does not
The model design method is provided, which 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.

Controller can be understood as receiving requests from users,
Match the model and view to complete user requests. The Division control layer is also very effective. It clearly tells you that it is a distributor, what model to choose, and what view to choose.
What kind of user request is completed. The control layer does not process any data. For example, after a user clicks a connection and the control layer accepts the request,
It does not process business information. It only transmits user information to the model, tells the model what to do, and selects a view that meets the requirements to return to the user. Therefore, a model may correspond to multiple views, and a view may correspond to multiple
Model.

The separation of models, views, and controllers allows a model to have multiple display views. If the user changes the model data through the controller of a view, all other views dependent on the data should
These changes are reflected. 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. Model and view
Figure 1 shows the relationship between controllers and their main functions.

II. Implementation of the MVC design model

ASP. NET provides a similar environment for implementing this classic design mode. Developers develop data on the ASPX page
The user interface is used 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 Multilayer System provided by implementing this design in ASP. NET
Compared with the classic ASP structure implementation system, the system has obvious advantages. Separating User display (View) from Action (Controller) improves code reusability. Extract the data (model) from its operation
The separation of controllers allows you to design a system that is irrelevant to the backend storage data. 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. It can be directly integrated in the development environment like the Windows interface.
Drag the control to complete page development. This article describes each page in the form of a composite view: a page consists of multiple sub-views (User parts); a sub-view can be the simplest html
Controls, server controls, or web custom controls nested by multiple controls. The page is defined by the template. The template defines the layout of the page, the tag and number of user parts. You specify a template.
This information is automatically created on the page. 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. Use a combination page composed of user parts configured according to the template, which 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 section mainly defines the pagebase for the web page base class, And pagelayout for the page layout policy class.
Surface layout: used to load user parts to pages. The user part base class usercontrolbase is the user part framework. It is used to dynamically load inspection parts and personalize user parts. To implement
Currently, the flexibility of web applications and views also use many configuration files, 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.
The sender Class determines the view presented to the customer based on the current status and business operation results. In this section, we mainly define httpreqdispatcher (distributor class ),
Httpcapture (request capturing class), controller (Controller class), and so on, they work together to complete the controller functions. The request capturing class captures HTTP requests and forwards them
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.
Select the view that is provided to the user and control the distribution resource. In this section, we adopt the distributor, policy, factory method, adapter, and other design modes.

ASP. NET provides a low-level request/response API for the request capturing class to automatically capture and process user requests,
Allows developers to use the. NET Framework class to provide services for incoming HTTP requests. Therefore, you must create a system. Web. ihttphandler
The interface and the class that implements the processrequest () method are: the request capturing class, And the Section. Each incoming HTTP request received by ASP. NET is ultimately implemented by ihttphandler
. Ihttphandlerfactory provides the ihttphandler instance URL
The actual parsing structure of the request. The HTTP handler and factory are declared as part of the Web. config file in the ASP. NET configuration. ASP. NET defines
<Httphandlers> in the configuration section, you can add and remove processing programs and factories. Sub-directories inherit httphandlerfactory and
Httphandler settings. HTTP processing programs and factories are the subjects of ASP. NET page frameworks. The factory assigns each request to a processing program, which processes the request.
For example, in the Global machine. config file, ASP. NET maps all requests to the aspx file to the httpcapture class:

<Httphandlers>
...
...
</Httphandlers>

2.3 Model

The model in the MVC system can be divided into two types: internal state of the system and action to change the state of the system. The model is yours.
The part of the commercial logic code. This article provides the business entity object and business processing object for the model: All business processing objects are subclasses derived from the processbase class. The business processing object is encapsulated
The specific processing logic calls the business logic model and submits the response to the appropriate view component to generate a response. Business Entity objects can describe client form data by defining properties. All business entity objects
Entitybase is a subclass object that can be read and written directly by the business processing object without data interaction with the request and response objects. Through Business Entity pair
It supports interaction between views and models. "What to do" (business processing) and "how to do" (Business Entity) are separated during implementation. In this way, business logic can be reused. Specific applications
The specific code examples are not listed here.

Iii. MVC design pattern Extension

The MVC mode in ASP. NET has excellent scalability. It can easily implement the following functions:

① Implement multiple views of a model;

② Multiple controllers are used;

③ When the model changes, all views are automatically refreshed;

④ All controllers will work independently of each other.

This is the benefit of the MVC mode. You only need to slightly modify the previous program or add new classes, that is
You can easily add many program functions. Many classes developed previously can be reused, and the program structure does not need to be changed at all. Each type is independent of each other, which facilitates group development and improves development efficiency. The following describes how to implement
Model, two views, and a controller program. The model class and View class do not need to be changed at all. This is exactly the same as the previous one. This is the benefit of object-oriented programming. For classes in the controller, you only need to add another view and associate it with the model. In this mode, 2 is shown between views, controllers, and models.


You can also implement other forms of MVC, such as one model, two views, and two controllers. It can be seen from the above that the application implemented through the MVC model has extremely good scalability and is the future direction of ASP. NET Object-Oriented Programming.

Iv. Advantages of MVC

Most Web applications developed using process languages such as ASP and PHP. The initial development template is data programming at the mixing layer. For example, directly sending requests to the database and displaying them in HTML, the development speed is usually relatively fast, but because the data page separation is not very direct, therefore, it is difficult to reflect the appearance of the business model or the reusability of the model. Product design elasticity is very small, and it is difficult to meet the changing needs of users. MVC requires application layering. Although additional work is required, the structure of the product is clear, and the application of the product can be better reflected through the model.

First, the most important thing is to have the ability of multiple views to correspond to one model. With the rapid changes in user requirements, there may be multiple methods
Application access requirements. For example, the Order model may have orders of the system, online orders, or orders of other systems, but the processing of orders is the same, that is, the processing of orders is consistent. Press
The MVC design pattern, one order model and multiple views can solve the problem. This reduces code replication, which reduces the amount of code maintenance. Once the model changes, it is easy to maintain.
Second, because the data returned by the model does not contain any display format, these models can be directly applied to the interface.

Once again, because an application is separated into three layers, sometimes changing one layer can satisfy the application changes. To change the business flow or rules of an application, you only need to change the MVC model layer.

The concept of the control layer is also very effective. Because it combines different models and views to complete different requests, the control layer can be said to include the concept of user request permissions.

Finally, it is also conducive to software engineering management. Because different layers perform their respective jobs, different applications have some same features, which is conducive to generating management program code through engineering and tool.

5. MVC Deficiency

The shortcomings of MVC are embodied in the following aspects:

(1) added the complexity of system structure and implementation. For a simple interface, strictly abide by MVC. Separating Models, views, and controllers increases the complexity of the structure and may lead to too many update operations to reduce the running efficiency.

(2) The views and controllers are too closely connected. Views and controllers are isolated from each other, but they are closely related. A view does not have a controller and its applications are limited. Vice versa, which hinders independent reuse.

(3) inefficient access of views to model data. Depending on the model operation interface, the view may need to be called multiple times to obtain sufficient display data. Unnecessary frequent access to unchanged data will also damage operation performance.

(4) Currently, Advanced Interface tools or constructors do not support the MVC mode. It is costly to transform these tools to adapt to MVC needs and build separation components, resulting in difficulties in using MVC.

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.