Implementation of MVC Design Mode under ASP.NET

xiaoxiao2021-03-06  108

ASP.NET is part of Microsoft's latest new architecture. Net framework, which provides powerful support for constructing a new generation of dynamic sites and network-based distributed applications. ASP.NET provides many important advantages, for example: simpleness; safety; manageability, etc. And object-oriented technology is fully implemented in ASP.NET compared to process-based ASP page technologies. In a web application example established by traditional ASP technology, display, business logic, and process control are simultaneously implemented, which is considered from engineering perspective, and there are many shortcomings. The user interface is responsible for displaying the problem model to the user and the role of operating and I / O interaction with the user. The user wants to keep the interaction interface relative stability, but more want to change and adjust the displayed content and form as needed. The ASP.NET technology combined with the MVC design pattern in the .NET framework has solved the above problems.

1 MVC Design Mode Introduction

The MVC is proposed by Trygve Reenskaug, first applied in a SmallTalk-80 environment, is the basis for many interaction and interface systems. The MVC structure is designed for applications that need to provide multiple views for the same data, which achieves separation of data layers and representations. As a development model, MVC is commonly used in the design and analysis of distributed applications, and the organization relationships used to determine each part of the system. For the demand for interface design variability, MVC (Model-View-Controller) divides the composition of the interactive system into models, views, and controllers.

View components show the information indicating model data and logical relationships and status to the user in a particular form. It obtains display information from the model, with multiple different display forms or views for the same information.

The controller component is the interaction of the user and the software. The role is to control the propagation of any changes in the model to ensure that the user interface is related to the corresponding connection between the model; it accepts the user's input, the input feedback to the model, and then implement The calculation of the model is a component that works to coordinate the model and view.

The model component saves the data displayed by the view, and the data controlled by the controller; it encapsulates the core data, logical and functional computational relationships of the problem, which is independent of the specific interface expression and I / O operation.

The model, the view is separated from the controller, so that a model can have multiple display elements. If the user changes the model's data through the controller of a view, all other views depending on these data should be reflected in these changes. Therefore, whenever data changes happen, the controller will notify all the views and cause the displayed updates. This is actually a change in model - communication mechanism. The relationship between the model, the view, the three controllers and the respective main functions, as shown in Figure 1.

2 MVC design mode implementation

ASP.NET provides a very good environment for achieving this classic design pattern. Developers have implemented views by developing user interfaces in the ASPX page; the functionality of the controller is implemented in logical function code (.cs); models usually correspond to the business part of the application system. A multi-layer system provided by this design is implemented in ASP.NET, which is significantly advantageous for systems implemented by classic ASP structures. Display the user (view) from the action (controller), improve the reuse of the code. Separate data (model) from the action (controller) of its operation, you can design a system that is not related to the storage data of the background. In terms of the essence of the MVC structure, it is a method of solving the problem of coupling system.

2.1 view

The view is the representation of the model, which provides the user interactive interface. Using multiple users components including single display pages, complex web pages can show content from multiple data sources, and webmasters, artists can participate in the development and maintenance of these web pages.

Under ASP.NET, the implementation of the view is simple. You can complete the page development by dragging the control in the integrated development environment like developing a Windows interface. This article describes the form of composite views in each page: a page consisting of multiple subviews (user parts); sub-view can be the simplest HTML control, server control, or multiple controls of WEB customizations Control. The page is defined by the template, the template defines the layout of the page, the tag and number of the user parts, the user specifies a template, and the platform automatically creates a page based on this information. For static template content, such as site navigation, menu, friendly links, these use default template content configuration; for dynamic template content (mainly business content), due to user requests, only later tie Depending on the user, the display content of the user component is filtered. Using a combination page consisting of a template configuration by the user component, it enhances reusability and prototypes the layout of the site. The view part is approximately: First, the page template defines the layout of the page; the page configuration file defines the specific content of the view tag (user component); then, the page layout policy class is initialized and loaded; each user part according to itself The configuration is initialized, loaded with the verifier and sets parameters, as well as the commission of the event, etc. After the user is submitted, the user's part is automatically submitted to the business entity to the business entity by the user.

This part mainly defines the web page base class pagebase; page layout policy class PageLayout, complete page layout, used to load user parts to page; user component base class UserControlBase, Personalization. In order to implement the flexibility of web applications, the view section also uses many profiles such as: set files with template configuration, page configuration, path configuration, verification configuration, etc.

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, the controller is introduced in order to achieve centralized management. The application's controller sets from the client receiving request (typically a user running a browser), decides what business logic function is executed, and then the responsibility of the next user interface will be delegated to an appropriate view component.

Provide a centralized entry point for control and processing requests, which is responsible for receiving, intercepting and handling user requests; and delegates the request to the distributor class, decide to rendering the customer based on the current state and the result of the business operation. This part mainly defines httpreqdispatcher, httpcapture, controller, etc., which cooperate with each other to complete the controller. Request the capture class to capture the HTTP request and forward to the controller class. The controller class is the initial entry point that handles all requests in the system. The controller completes some necessary processes to delegate the request to the distributor class; the distributor class distributor is responsible for the management and navigation of the view, and it is managed which view will be given to the user and provide distribution resource control. In this part, the distributors, strategies, factory methods, adapters, etc. are used.

In order to automatically capture user requests and processes the requesting capture class, ASP.NET provides a low-level request / response API to enable developers to use the .NET Framework class as incoming HTTP requests. To do this, you must create a class that supports the System.Web.ihttPHandler interface and implementation processRequest () method: request the capture class, and add classes in Web.config . Each incoming HTTP request received by ASP.NET is finally processed by a specific instance of the class that implements the IHTTPHANDLER. IHTTPHandlerFactory provides the actual parsing of the IHTTPHANDLER instance URL request. The HTTP handler and the factory declared part of the web.config file in the ASP.NET configuration. ASP.NET defines a configuration section where you can add and remove handlers and plants. The subdirectory inherits the settings of HTTPHANDLERFACTORY and HTTPHANDLER. The HTTP handler and factory is the subject of the ASP.NET page framework. The factory assigns each request to a handler, which processes the request. For example, in the global machine.config file, ASP.NET maps all requests for ASPX files to HTTPCAPTURE Class: ... ...

2.3 model

The model in the MVC system can be divided into two types-system of internal state and the action of changing system status. The model is where all your business logic codes are located. This article provides a business entity object and business processing object: All business handling objects are subclasses from the ProcessBase class. The business handling object encapsulates the specific processing logic, calls the business logic model, and submits the response to the appropriate view assembly to generate a response. Business entity objects can describe client form data by defining attributes. All business entity objects ENTITYBASE derived sub-objects, and the business handling object can read and write it directly, and no longer need to interact with the request, the Response object. Support for interaction between views and models is achieved through business entity objects. What to do "(business processing) and" how to do "(business entity) are separated when implementing. This can achieve the reuse of business logic. Since the specific business of each application is different, the specific code example is not listed here. 3 MVC design mode extension

With its extremely good scalability by writing MVC mode in ASP.NET. It can easily implement the following features:

1 Implement multiple views of a model;

2 use multiple controllers;

3 When the model changes, all views will be automatically refreshed;

4 All controllers will work independently.

This is the benefit of the MVC mode, just modify or add new classes on the previous program to easily add a number of program features. Many of the previously developed many classes can be reused, and the program structure does not need to be changed, and all kinds of independence, facilitate group development, and improve development efficiency. Here is how to implement a model, two views, and a controller program. The model class and the view class do not need to change, the same as the previous, which is the benefits of object-oriented programming. For classes in the controller, you only need to add another view and it can be associated with the model. The schematic diagram of the three views, controllers, and models are shown in Figure 2.

Figure 2 is a schematic diagram of the relationship between the three views, the controller, and the model. Other forms of MVC can also be implemented, for example: a model, two views, and two controllers. As can be seen from the above, the application implemented through the MVC mode has extremely good scalability, which is the future direction of ASP.NET object-oriented programming. 4 MVC design model advantages and deficiency

4.1 Advantages of MVC

The advantages of MVC are reflected in the following aspects:

(1) Multiple views can be established and used for one model at the same time. The change-propagation mechanism ensures that all related views get the model data change, so that all associated views and controllers make behavioral synchronization.

(2) Connectivity of the view and the controller, allows replacement views and controller objects, and can be replaced or changed during operation based on demand dynamics.

(3) Portability of the model. Because the model is independent of the view, you can transplant a model to a new platform work. It is only necessary to make new modifications on the new platform.

(4) Potential frame structure. The application framework can be established based on this model, not just in the design interface.

4.2 The deficiency of MVC

The shortcomings of MVC are reflected in the following aspects:

(1) Increase the complexity of system structure and implementation. For simple interfaces, strictly follow the MVC, separate the model, view and controller, increase the complexity of the structure, and may generate too much update operation, reduce operational efficiency.

(2) The view is too close to the controller. The view and the controller are separated from each other, but it is indeed closely close components, and the view does not have the existence of the controller. Its application is very limited, and vice versa, this hampering their independent reuse.

(3) View the low efficiency accesses of model data. According to the different model operating interface, the view may need to call multiple calls to get enough display data. Unnecessary frequent access to untrurified data will also harm the operating performance.

(4) Currently, generally advanced interface tools or constructors do not support MVC mode. The cost of transforming these tools to accommodate MVC needs and the establishment of the part of the component is high, resulting in difficulties in using MVC.

5 Conclusion

Compared with the inner model of the problem handled by the software, the user interface is required to change frequently. The MVC design pattern can be used to make the software calculation model independently of the interface. Large distributed application frameworks can also be established based on this model. This article describes the principles of MVC design mode; the MVC design mode three constituent components (model components, view components, and control components) and work based on MVC-based applications in the ASP.NET environment; the expansion of MVC design mode; Analysis of the advantages and deficiencies of MVC.

转载请注明原文地址:https://www.9cbs.com/read-103079.html

New Post(0)