Http://www.zdnet.com.cn/developer/webdevelop/story/0,3800067013,39139599,00.htm
Develop complex dynamic ASP. NET ApplicationsProgramYou must minimize the number of duplicates.CodeTo improve the reusability and flexibility of applications. In some applications, different operations may have different controller logic, but the results are the same view (for example, when a product list is displayed, users may be allowed to add or delete a product. However, after the Controller directs the user to complete the add or delete process, all products are displayed in the same product view, including modified data ).
When developing reusable program logic, the first step is to minimize the number of codes on the server script page. The logic on this page is difficult (and sometimes impossible) to reuse, resulting in insufficient separation of views and controllers. In addition, it is difficult to test and debug it. Therefore, do not add the script code to A. ASPX page. A more effective way is to use classes to implement controllers. In this way, the appearance and navigation mechanisms can be unified throughout the web application, and the presentation logic can be reused within the scope of the application.
Two different modes can be used to implement the control class of ASP. NET applications. In applications built in page controller mode, the navigation mode is static, but web pages are dynamically generated. For more complex applications, if the navigation is dynamic or requires configuration based on a rule set (such as user permissions or application status, using the front controller mode can achieve a more effective implementation. The two modes are discussed in detail below.
Page controller mode
In the page controller mode, a central class named basecontroller is used to implement all the public behaviors required, so as to process HTTP requests, update models, and forward requests to appropriate views. Basecontroller provides common functions including session management, security, and data retrieval from query strings or hidden fields. For every link in a web application, you need to create an independent pagecontroller class, which must be inherited from the basecontroller class. These independent pagecontrollers are responsible for implementing any behavior specific to webpages and directly using the core functions that have been implemented by basecontroller.
In many cases, your applications can be divided into a series of common web page types, which share common logic. For example, you may have a series of data input pages or grid view pages that share the same logic. In this case, it is best to implement a dataentrycontroller or a gridviewcontroller and require them to inherit from the basecontroller class. Based on these derived classes, you can implement your own pagecontroller and use existing public methods. However, it should be noted that the inheritance hierarchy cannot be too complex, otherwise the application logic will become difficult to maintain. To minimize the length of the inheritance chain, you can create a series of "helper classes" that contain a series of public code that can be called at any inheritance level.
To implement the page controller mode, you can inherit a basecontroller class from system. Web. UI. Page and then implement common application functions. For example, basecontroller can provide headers, footers, and user-specific information (such as logon names and departments) to achieve consistent appearance and feeling. Then, you can inherit from the basecontroller class and implement the unique logic of the webpage by the inherited class (put in the hidden code file) to create every page required by the application. This mode is especially suitable for medium-complexity applications that have fixed navigation paths. On the contrary, if you need dynamic navigation, you need to use the front controller mode.
Front Controller Mode
If you need to perform Cooperative Processing between multiple webpages, the efficiency of the page controller mode will be greatly reduced because it requires that an object be implemented for each logical webpage. In this case, the front controller mode is more effective because it uses a controller to receive all requests and then directs requests through a hierarchy of processing programs and command classes. The handler obtains parameters from the HTTP request, selects the correct command, and executes it. After each command object executes a specified operation, it determines which view to use to correctly present the webpage. By implementing the Front Controller, you can get more centralized application control, because all webpage requests are handled by a separate controller rather than by different page controllers. But this is not without a cost: If the processing program performs some high-end processing, such as database search, it may cause the whole application to run slowly. Therefore, the handler should be as efficient as possible and use external resources only when absolutely necessary. You should also consider caching any external resources to improve the processing performance.
To implement the frontcontroller class, you need to create a handler and a commandfactory to determine the commands to be executed in response to a request. ASP. NET provides the ihttphandler interface, which allows developers to create custom interfaces required to provide services for incoming HTTP requests. To implement handler, You need to inherit from system. Web. ihttphandler and add the corresponding logic to instantiate and call appropriate commands from commandfactory. Commandfactory defines a command set and the logic used to determine the command to be executed. When commandfactory is called, an appropriate command object is returned, while handler can callExecuteMethod. In this mode, you can extend the commandfactory logic and create additional commands to handle different actual situations, so as to create a more reliable navigation mechanism and implement them in a centralized manner.
Structured Mode
. Net constructor should try to use the structured mode. With the built-in ASP. NET features, you can easily implement the page controller and Front Controller modes. Use them to obtain highly reusable and Scalable Application designs. For more information about these and other structured modes and the Implementation Details of ASP. NET, visit Microsoft. NET architecture center.