Using the Micrisoft.net Design Scenario Chapter III Web presentation mode Web mode cluster detailed introduction

Source: Internet
Author: User

Pagecontroller ( page Controller)

Context

When you decide to use MVC to dynamically create applications, you need to focus on separating the user interface, controller, and business logic.

Problem:

How do I create a controller for a more complex application?

Impact factor:

MVC focuses on the separation of business models from views and less on controllers. For FAT customers, the need to separate the controller from the view is not obvious. But in thin clients, the controller is separate from the view itself. Because the view is displayed on the client's browser, the controller is executed on the server side.

Because of the actions of different users, there will be multiple operations, a common view. If the controller and view are not detached, the view code appears repeatedly. For example, product query system, product query, pagination will use the same view, but do different actions.

Different controllers and views in a Web system are loaded with a series of operations such as manager authentication, authorization, string queries, and so on, which can cause code duplication.

The script service's end page is easy to create, but the display logic and business logic are mixed together, and as the complexity increases, it is difficult to maintain, extend, and reuse.

Testing the view is difficult from a test perspective, but testing the business logic is easier and more reusable, so it is easy to test if the view is separate from the business logic and the controller.

Solution Solutions

Use Pagecontroller to receive user input, invoke business logic actions based on input, and return results, and finally determine which view to use. This pattern separates the business logic from the view logic (for example). It is common practice to write common business logic as a base class that Pagecontroller inherits from this base class for easy testing and consistency.

The page controller receives the user's input and extracts the input data, and invokes the model operation to render the results of the model operation on the view. Views are displayed based on the data retrieved by the model. The page controller separates the model from the view, and the page controller takes on the tasks of HTTP requests, such as session management, caching, authentication, and so on. So we can create a controller for each link, and each controller performs one operation, which makes it simple.

Creating a controller for each page will result in a lot of code duplication, so you need to encapsulate these common operations into a base class controller, each of which inherits the base class, or some helper classes.

The higher the page similarity, the more complex and the deeper the degree of inheritance. such as product Inquiry system some need to display product list some need to input data, we will make two base class one is Listcontroller and Dataentrycontroller, page control inherits them. In this way, with the complexity of the system increased, the change is not easy to maintain, so the developer is Pagecontroller is bad design, and Frontcontroller is a good design, in fact, this is due to pagecontroller lack of perfect support.

Most frameworks support Pagecontroller, such as ASP, JSP, PHP, and so on. Most of these frameworks mix the controller and view in the server, making it difficult to separate. In this way, ASP. NET has separated the mode, put the controller into a separate class, and used the code hiding technique.

Variant

In most cases, the page Controller relies on HTTP requests, such as request domains, request headers, form fields, string queries, and so on, so that the actions related to HTTP requests can be encapsulated in a class, separating the HTTP-related logic from the HTTP-independent logic, see:

The disadvantage of this is that increasing the overhead of the system, the benefit is that it is easy to maintain and develop, especially for multi-person development teams.

Advantages of Pagecontroller:

1, simple, each link or request, corresponding to a controller, easy to create and maintain, it is easy to understand.

2, built-in framework, all frameworks are implemented by default this mode.

3, easy to expand, you can use some help to extend the class.

4. Separation of duties

5. High degree of reuse

Disadvantages of Pagecontroller:

1, one controller per page. Because of its simplicity, this pattern can only be used in Web systems where navigation is relatively simple, and if the system is more complex it is recommended to use the front Controller.

2, the inheritance depth is big: because this pattern uses the inheritance way realizes the reuse, if the business is more complex, the inheritance depth is relatively big, is not easy to maintain.

3. reliance on Web frameworks

in the ASP . implement Page Controller in

Build Web Applications in ASP. Leveraging the built-in Pagecontroller framework while leveraging the event-driven features of ASP

Implementation strategy

By default, the ASP. NET page framework implements various concepts of pagecontroller. The ASP. NET page framework is automatically implemented, the client is aware of user actions, sends the user request to the server side, the server side automatically calls the appropriate method to respond. This process is not known to the developer. The page frame can be extended, and the various events of the page frame are open to the outside.

Page life cycle

    • The most common stages in the page life cycle are listed below in the order in which they occur. It also includes specific events that are raised, and some typical actions that may be performed at various stages when a request is processed:
    • ASP. NET page framework initialization (event: Init). This is the first step in the life cycle, which initializes the ASP. NET runtime to prepare for responding to requests.
    • User code initialization (event: Load). You should perform common tasks that are specific to your application, such as opening a database connection when the page controller raises the Load event. You can assume that after the Load event is raised, the server control has been created and finished initializing, the state is restored, and the form control reflects the client's changes. [REILLY02]
    • Application-related event handling. At this stage, you should perform application-related processing in response to events raised by the controller:
    • Cleanup (event: Unload). The page has finished generating and can now be discarded. You should close any database connections opened by the Load event and discard any objects that are no longer needed. After the connection object is garbage collected, the Microsoft.NET Framework automatically shuts down the database connection. However, you do not have any control over when to do garbage collection. Therefore, it is a good practice to explicitly close the database connection to make full use of the database connection pool.

Note: There are several page processing stages that are not listed here. However, these phases are not used for most page processing scenarios.

Implementation method

Using the Micrisoft.net Design Scenario Chapter III Web presentation mode Web mode cluster detailed introduction

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.