When developing complex dynamic asp.net applications, you must minimize the duplication of code and increase the reusability and flexibility of your application. In some applications, the different actions taken by the user may have different controller logic, but the result is the same view (for example, when displaying a list of products, users may be allowed to add or remove a product.) But after the controller has booted the user through the Add or remove process, the final display is the same product view, which contains the modified data.
The first step in developing reusable program logic is to minimize the number of code in the server Script page. The logic in the script page is difficult (and sometimes even impossible) to reuse, causing the view and controller to be detached well enough. In addition, its testing and debugging also appear to be extremely difficult. Therefore, instead of adding scripting code to an. aspx page, it is more efficient to use a class to implement the controller. This will unify the appearance and navigation mechanisms throughout the Web application and reuse presentation (rendering) logic across the entire application.
There are two different patterns that can be used to implement the control class for a asp.net application. In applications built in Page Controller mode, the navigation mode is static, but the Web page is dynamically generated. For more complex applications, if the navigation is dynamic or requires configuration based on a rule set such as user rights or application state, then a more efficient implementation is achieved using the front controller pattern. These two patterns are discussed in detail below.
Page Controller Mode
When you use Page Controller mode, you implement all the common behaviors that are required by a central class named Basecontroller to handle HTTP requests, update the model, and forward the request to the appropriate view. The common features provided by Basecontroller include session management, security, and retrieving data from query strings or hidden fields. For each link in a Web application, you need to create a separate Pagecontroller class that inherits from the Basecontroller class. These independent pagecontroller are responsible for implementing any behavior specific to the Web page and directly using the core functionality that has been implemented by Basecontroller.
Many times, your application can be decomposed into a series of common Web page types that share common logic. For example, you might have a series of data entry pages or grid view pages, all of which share the same logic. In this case, it is best to implement a dataentrycontroller or a gridviewcontroller and ask them to inherit from the Basecontroller class. Based on these derived classes, you can implement your own Pagecontroller and use out-of-the-box public methods. Note, however, that the inheritance hierarchy cannot be too complex, or the application logic becomes difficult to maintain. To minimize the inheritance chain, you can create a series of "helper Classes" that contain a series of common code that can be invoked at any one of the inheritance levels.
To implement page Controller mode, you can inherit a Basecontroller class from System.Web.UI.Page, and then implement common application functionality. For example, Basecontroller can provide headers, footers, and some user-specific information (such as login name and department) to achieve a consistent look and feel. You can then inherit from this Basecontroller class, and the inherited class (placed in the code-behind file) to implement the logic specific to the Web page, creating every page the application needs. For moderately complex applications, this pattern is especially appropriate if it has a fixed navigation path. Conversely, if dynamic navigation is required, front Controller mode is required.
Front Controller Mode
If you need to collaborate on multiple pages, the page Controller mode is less efficient because it requires that you implement an object for each logical Web page. In this case, the Front controller mode is more efficient because it uses a controller to receive all requests, and then a hierarchy of handlers and command classes to boot the request. The handler takes the parameter from the HTTP request, selects the correct command, and executes it. After each command object performs the specified action, it determines which view is required to render the page correctly. By implementing front Controller, you get more centralized application control because all page requests are handled by a single controller rather than by a different page Controller. But this is not without cost: if the handler does some expensive processing, such as a database lookup, it can cause the entire application to run slowly. Therefore, the handlers 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 performance of your handlers.
In order to implement the Frontcontroller class, you need to create a handler (handler) and a commandfactory, which is used to determine the commands that need to be executed in response to a request. Asp. NET provides a IHttpHandler interface that allows developers to create custom interfaces that are required to provide services for incoming HTTP requests. To implement handler, you need to inherit from System.Web.IHttpHandler and add the appropriate logic to instantiate and invoke the appropriate command from the commandfactory. Commandfactory defines a collection of commands and the logic used to determine what commands should be executed. Calling Commandfactory returns the appropriate command object, and handler can call the object's Execute method. With this pattern, you can extend commandfactory logic and create additional commands to handle different situations, creating more reliable navigation mechanisms and centralizing them.
Structured mode
. NET architects should use structured patterns as much as possible. With the built-in features of ASP.net, it is easy to implement patterns such as page controller and front controller. Use them to achieve highly reusable and scalable application design. For these and other structured patterns, to learn more about them and the details of the ASP.net implementation, visit Microsoft. NET Architecture Center.