Asp. Implementation of MVC design pattern under net

Source: Internet
Author: User
Tags config functions http request
asp.net| design ASP. NET is a part of Microsoft's latest new architecture. NET Framework, which provides strong support for structuring the next generation of dynamic Web sites and web-based distributed applications. Compared to previous WEB development models, ASP.net offers many important benefits such as simplicity, security, manageability, and so on. Moreover, the object-oriented technology is fully implemented in the ASP.net compared with the ASP page technology based on the process. Using the traditional ASP technology to build a Web application example, in the page to achieve display, business logic and Process control, which from the point of view of engineering, it has a lot of deficiencies. The user interface takes on the role of displaying the problem model to the user and interacting with the user and I/O. Users want to maintain the relative stability of the interactive interface, but they want to change and adjust the content and form of the display as needed. Under the. NET Framework, the ASP.net technology combined with the MVC design pattern solves these problems well.

1 Introduction to MVC design pattern

MVC, proposed by Trygve Reenskaug, is first applied to the SMALLTALK-80 environment and is the foundation of many interactive and interface systems. The MVC structure is designed for applications that need to provide multiple views for the same data, and it is a good way to separate the data layer from the presentation layer. MVC, as a development model, is commonly used in the design and analysis of distributed application systems and in the identification of organizational relationships between parts of the system. For the variability of Interface design, MVC (Model-view-controller) decomposes the composition of the interactive system into three components of model, view and controller.

The view part presents the information that represents the model data and the logical relationship and state to the user in a specific form. It obtains display information from the model, and can have several different display forms or views for the same information.

The controller part handles the interaction between the user and the software, the responsibility is to control the propagation of any changes in the model, to ensure the interface between the models, to accept the input from the user, to feed the input to the model, to realize the calculation control of the model, and is to make the model and view coordinate the work of the parts.

The model part holds data that is displayed by the view and controlled by the controller, which encapsulates the computational relationships of the core data, logic, and functionality of the problem, independent of the specific interface expression and I/O operations.

The separation of models, views, and controllers allows a model to have multiple display views. If a user changes the model's data through a controller of a view, all other views that depend on the data should reflect these changes. Therefore, whenever a data change occurs, the controller notifies all views of the change, causing the update to be displayed. This is actually a model of the change-propagation mechanism. The relationships between models, views, controllers, and their main functions, as shown in Figure 1.


2 Implementation of MVC design pattern

Asp. NET provides a good environment for implementing this classic design pattern. The developer implements the view by developing the user interface in the ASPX page; the function of the controller is implemented in the logical function code (. cs); The model usually corresponds to the business part of the application system. In ASP.net, a multi-layer system provided by this kind of design has obvious advantages in the system of the classical ASP structure implementation. Separating the user display (view) from the action (Controller) improves the reusability of the code. Separating the data (model) from the Action (Controller) of its operations allows you to design a system that is not related to storing data in the background. As far as the nature of MVC structure is concerned, it is a method to solve the problem of coupling system.

2.1 View

A view is a representation of a model that provides a user interaction interface. With multiple user parts that contain a single display page, complex Web pages can display content from multiple data sources, and Web page people, artists, can participate in the development and maintenance of these web pages on their own.

The implementation of the view is simple under asp.net. You can complete the page development by dragging controls directly in the integrated development environment, just as you would develop a Windows interface. This article describes each page in the form of a composite view: A page consists of multiple child views (user parts); A child view can be a Web custom control that is nested from the simplest HTML control, server control, or multiple controls. The page is defined by the template, the template defines the layout of the page, the label and number of user parts, the user specifies a template, and the platform automatically creates the page based on that information. For static template content, such as site navigation on the page, menus, friendly links, these use the default template content configuration, for dynamic template content (mainly business content), because the user's request is different, can only use late binding, and for different users, user part of the display content to filter. Using a composite page made up of user parts based on the template configuration, it enhances reusability and prototypes the layout of the site.

The view section roughly processes the process as follows: first, the page template defines the layout of the page, the page profile defines the contents of the View label (user part), then initializes and loads the page by the page layout policy class, each user part initializes according to its own configuration, loads the validator, and sets the parameters. And the entrustment of the event, after the user submits, through the verification of the presentation layer, the user part automatically submits the data to the business entity, i.e. the model.

This section mainly defines the Web page base class pagebase, the page layout policy class pagelayout, completes the page layout, uses to load the user part to the page, the user part base class Usercontrolbase is the user part framework, uses for the dynamic loading inspection part, and personalization of user parts. In order to achieve the flexibility of Web applications, the View section also uses a number of configuration files such as template configuration, page configuration, path configuration, authentication configuration, and so on.

2.2 Controller

In order to be able to control and coordinate the processing of each user across multiple requests, the control mechanism should be managed in a centralized manner. Therefore, in order to achieve the goal of centralized management, the controller is introduced. The application's controller concentrates on receiving requests from the client (typically a user running a browser), deciding what business logic functions to perform, and delegating responsibility for creating the next user interface to an appropriate view component.

The controller provides a centralized entry point for controlling and processing requests, which is responsible for receiving, intercepting, and processing user requests, and delegating the request to the Distributor class, depending on the current state and the results of the business operation to determine the view to the customer. In this section, we mainly define the Httpreqdispatcher (Distributor Class), the Httpcapture (Request capture Class), the Controller (Controller Class), etc., they cooperate with each other to complete the function of the controller. The request capture class captures the HTTP request and forwards it to the Controller class. The Controller class is the initial entry point for all requests processed in the system. The controller completes some necessary processing and delegates the request to the Distributor class; The Distributor class Distributor is responsible for managing and navigating the View, which manages which views are selected for the user and provides control of the distribution resources. In this section, distributors, strategies, factory methods, adapters, and other design patterns are used.

To enable the request-capture class to automatically capture and process user requests, ASP.net provides a low-level request/response API that enables developers to service incoming HTTP requests using the. NET Framework classes. To do this, you must author a class that supports the System.Web.IHTTPHandler interface and implement the ProcessRequest () method, that is, the request capture class, and the class is added to the Web.config

...
<add verb= "*" path= "*. ASPx "Type=" Sys.UI.HttpCapture, Sys.ui "/>
...


2.3 Model

The models in an MVC system can be conceptually divided into two categories-the internal state of the system and the action to change the state of the system. The model is where all of your business logic code snippets are located. This article provides a 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, invokes the business logic model, and submits the response to the appropriate view component to produce a response. Business entity objects can describe client form data by defining attributes. All business entity objects are entitybase derived subclass objects, and the business processing object can read and write directly to it without the need for data interaction with the request and response objects. Support for interaction between the view and the model is implemented through the business entity object. When implemented, separate "What to do" (business process) and "How To Do" (business entity). This enables the reuse of business logic. Since the specific business of each application is different, the specific code instance is no longer listed here.

3 expansion of the MVC design pattern


Written in the MVC pattern in asp.net, it has a very good scalability. It can easily implement the following features:

① implements multiple views of a model;

② using multiple controllers;

③ all views will be refreshed automatically when the model is changed;

④ all controllers will work independently of each other.

This is the benefit of the MVC pattern, where you can easily add a lot of program functionality by simply modifying or adding new classes to the previous program. Many of the previously developed classes can be reused, and the program structure no longer needs to be changed, and the various categories are independent of each other to facilitate group development and improve development efficiency. The following discusses how to implement a program for a model, two views, and a controller. Where model classes and view classes do not need to be changed at all, as in the preceding, this is the benefit of object-oriented programming. For a class in the controller, you just need to add another view and associate with the model. The schematic diagram of the view, controller, and model in this mode is shown in Figure 2.



Fig. 2 schematic diagram of the relationship between view, controller and model

You can also implement other forms of MVC, such as a model, two views, and two controllers. As can be seen from the above, the application implemented through the MVC pattern has extremely good scalability and is the future direction of ASP.net object-oriented programming.

4 Advantages and disadvantages of MVC design pattern

Advantages of 4.1 MVC

The advantages of MVC are embodied in the following areas:

(1) Multiple views can be established and used at run time for a model. The change-propagation mechanism ensures that all relevant views are in time for model data changes so that all associated views and controllers are synchronized.

(2) The accessibility of view and controller allows for the replacement of view and controller objects, and can be dynamically turned on or off according to demand, or even object substitution during run time.

(3) The portability of the model. Because the model is independent of the view, a model can be ported independently to the new platform. All you need to do is make new changes to the view and controller on the new platform.

(4) The potential frame structure. You can build an application framework based on this model, not just in design interface design.

The deficiencies of 4.2 mvc

The lack of MVC is reflected in the following areas:

(1) The complexity of system structure and implementation is increased. For a simple interface, strictly following MVC, separating the model, view, and controller will increase the complexity of the structure and may result in excessive update operations and lower operational efficiency.

(2) Too tight a connection between the view and the controller. The view and controller are separate, but closely connected parts, the view has no controller, its application is very limited, and vice versa, which hinders their independent reuse.

(3) Inefficient access of the view to model data. Depending on the model's operating interface, the view may require multiple calls to obtain sufficient display data. Unnecessary frequent access to unchanged data also impairs operational performance.

(4) At present, the general advanced interface tools or constructors do not support the MVC pattern. The cost of adapting these tools to MVC needs and building separate parts is high, resulting in the difficulty of using MVC.

5 concluding remarks

Compared with the intrinsic model of the problem that the software deals with, the user interface needs to change frequently, and the MVC design pattern can satisfy the requirement of the interface, and make the computing model of the software independent of the structure of the interface. You can also build a large distributed application framework based on this model. This paper introduces the principle of MVC design pattern, the MVC design pattern three components (model parts, view parts and control parts) and the work to be done based on MVC in the ASP.net environment, the expansion of MVC design pattern, and finally the analysis of the advantages and disadvantages of MVC.


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.