Implementation of MVC design pattern in ASP. NET

Source: Internet
Author: User
Tags classic asp
Author: wrclub from: http://dev.csdn.net/

ASP. NET is part of Microsoft's latest New Architecture. NET Framework. It provides powerful support for building a new generation of dynamic websites and distributed network-based applications. Compared with the previous Web development model, ASP. NET provides many important advantages, such as simplicity, security, and manageability. In addition, compared with the process-based ASP page technology, object-oriented technology is fully implemented in ASP. NET. Web Application Instances built with traditional ASP technology implement display, business logic, and process control on pages at the same time, which has many shortcomings from the engineering point of view. The user interface is responsible for displaying the problem model to the user and performing operations and I/O interaction with the user. Users want to maintain relatively stable interactive interfaces, but want to change and adjust the displayed content and format as needed. Under the. NET Framework, the ASP. NET technology works well with the MVC design pattern to solve the above problems.

1 Introduction to MVC design patterns

Proposed by Trygve Reenskaug, MVC is first applied in the SmallTalk-80 environment and is the basis of interaction and interface systems. The MVC structure is designed for applications that need to provide multiple views for the same data. It achieves the separation between the data layer and the presentation layer. As a development model, MVC is usually used in the design and analysis of distributed application systems, as well as to determine the organizational relationship between various parts of the system. For the requirement of Interface Design variability, MVC (Model-View-Controller) divides the composition of an interactive system into three parts: Model, View, and Controller.

View components display model data, logical relationships, and statuses in a specific form. It obtains the display information from the model and can have multiple different display forms or views for the same information.

The controller is used to process the interaction between users and software. Its responsibility is to control the transmission of any changes in the model and ensure the corresponding connection between the user interface. It accepts user input, the input is fed back to the model to control the calculation of the model. It is a part of the coordination between the model and the view.

The model component stores the data displayed in the view and controlled by the Controller. It encapsulates the computing relationship between the core data, logic, and functions of the problem, it is independent of specific interface expressions and I/O operations.

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 1.

2. Implementation of the MVC design pattern

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>
...
<Add verb = "*" path = "*. ASPx" type = "Sys. UI. HttpCapture, Sys. UI"/>
...
</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 where all your business logic code snippets are located. 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 encapsulates 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 are subclass objects derived from EntityBase. business processing objects can directly read and write them, without data interaction with request and response objects. Business Entity objects are used to support 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. Because the specific services of each application are different, the specific code examples are not listed here.

3. 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 can easily add many program functions by slightly modifying the previous program or adding new classes. 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 a 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.

Figure 2 Relationship 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.

4 advantages and disadvantages of the MVC design pattern

4.1 advantages of MVC

The advantages of MVC are as follows:

(1) You can create and use multiple views for a model at runtime. Change-the propagation mechanism ensures that all related views get model data changes in a timely manner, so that all associated views and controllers can synchronize behavior.

(2) the connection between views and controllers allows you to change view and controller objects. You can enable or disable views and controller objects dynamically as needed, or even replace objects during running.

(3) model portability. Because the model is independent from the view, a model can be transplanted to the new platform independently. All you need to do is modify the view and controller on the new platform.

(4) potential framework structure. You can build an application framework based on this model, not just for design purposes.

4.2 limitations of MVC

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.

5 conclusion

Compared with the internal model of the software, the user interface needs to change frequently. The MVC design mode can meet the interface requirements while, make the software computing model independent of the interface. You can also establish a large-scale distributed application framework based on this model. This article introduces the principles of the MVC design pattern. The MVC design pattern consists of three components (model components, view components and control components) and ASP. NET environment, MVC design pattern extension, and the advantages and disadvantages of MVC are analyzed.

Related Article

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.