The implementation of page controller needs to be created for the public part of the page in the base class.CodeBut with the passage of time, the demand will change a lot, and sometimes have to add non-public code, so that the base class will continue to increase, you may create a deeper hierarchy to delete the conditional logic, which makes it difficult for us to refactor it, so we need to further study the page controller.
By controlling and transmitting all requests, front controller solves the problem of decentralized processing in page controller. It consists of two parts: handler and command tree. Handler processes all public logic, receives http post or get requests and related parameters, selects the correct command object based on the input parameters, and passes control to the command object to complete subsequent operations, here we will use the command mode.
In command mode, a request can be converted into an object to make a request to an unspecified application object. This object can be stored and passed like other objects, the key to this mode is an abstract command class, which defines an interface for executing operations. The simplest form is an abstract execute operation, the specific command subclass uses the receiver as an instance variable, implements the execute Operation, and specifies the action taken by the receiver. The receiver has the specific information required to execute the request.
For more information about handler principles, refer to msdn. Let's look at the specific implementation of the Front Controller mode:
First, define in Web. config:
<〈! -- Specify that the aspx file starting with dummy is handled by Handler -->
<Httphandlers>
<Add verb = "*" Path = "/webpatterns/frontcontroller/dummy *. aspx" type = "webpatterns. frontcontroller. Handler, webpatterns"/>
</Httphandlers>
<〈! -- Specify the page ing block named frontcontrollermap and submit it to the urlmap class for processing, Program Use the URL corresponding to the key as the final execution path. Here you can define multiple key-value pairs with the URL -->
<Configsections>
<Section name = "frontcontrollermap" type = "webpatterns. frontcontroller. urlmap, webpatterns"> </section>
</Configsections>
<Frontcontrollermap>
<Entries>
<Entry key = "/webpatterns/frontcontroller/dummywebform. aspx" url = "/webpatterns/frontcontroller/actwebform. aspx"/>
...
</Entries>
</Frontcontrollermap>
Modify webform. aspx. CS:
Private void button_click (Object sender, system. eventargs E)
{
Response. Redirect ("dummywebform. aspx? Requestparm = "+ dropdownlist. selectedvalue );
}
When the program is executed here, the processrequest event of the handler class will be triggered according to the definition in Web. config:
Handler. CS:
Public class handler: ihttphandler
{
Public void processrequest (httpcontext context)
{
Command command = commandfactory. Make (context. Request. Params );
Command. Execute (context );
}
Public bool isreusable
{
Get
{
Return true;
}
}
}
It will call the make method of commandfactory to process received parameters and return a command object, then it will call the execute method of the command object to submit the processed parameters to the specific processing page.
Public class commandfactory
{
Public static command make (namevaluecollection parms)
{
String requestparm = parms ["requestparm"];
Command command = NULL;
// Get different command objects based on input parameters
Switch (requestparm)
{
Case "1 ":
Command = new firstportal ();
Break;
Case "2 ":
Command = new secondportal ();
Break;
Default:
Command = new firstportal ();
Break;
}
Return command;
}
}
Public interface command
{
Void execute (httpcontext context );
}
Public abstract class redirectcommand: Command
{
// Obtain the key and URL key-value pairs defined in Web. config. For details about the urlmap class, see the code in the downloaded package.
Private urlmap map = urlmap. soleinstance;
Protected abstract void onexecute (httpcontext context );
Public void execute (httpcontext context)
{
Onexecute (context );
// Submit the key-value pairs to the specific processing page
String url = string. Format ("{0 }? {1} ", map. Map [context. Request. url. absolutepath], context. Request. url. query );
Context. server. Transfer (URL );
}
}
Public class firstportal: redirectcommand
{
Protected override void onexecute (httpcontext context)
{
// Add portalid to the input parameters for page processing
Context. items ["portalid"] = "1 ";
}
}
Public class secondportal: redirectcommand
{
Protected override void onexecute (httpcontext context)
{
Context. items ["portalid"] = "2 ";
}
}
Finally, in actwebform. aspx. CS:
DataGrid. datasource = getsubjectdatasource (httpcontext. Current. items ["portalid"]. tostring ());
DataGrid. databind ();
The preceding example shows how to use the Front Controller to centralize and process all requests. It uses commandfactory to determine the specific operation to be executed, regardless of the method and object to be executed, handler only calls the execute method of the command object. You can add additional commands without modifying handler. It allows users to see the actual page. When users enter a URL, the system will. the Config File maps it to a specific URL, which gives programmers more flexibility and obtains an indirect operation layer not included in the page controller implementation.
We use the front controller mode for quite complex web applications. It usually needs to replace the built-in controller of the page with the Custom Handler, in front controllrer mode, we do not even need pages. However, because of its complicated implementation, it may cause some troubles to the implementation of business logic.
The above two controller modes are used to process complicated webform applications. Compared with applications that directly process user input, the complexity is greatly improved and the performance is inevitably reduced, for this reason, we finally look at a mode that can greatly improve program performance: Page cache mode.
Webform in page cache Mode
Almost all webforms face applications with frequent access and few changes. For webform visitors, a lot of content is repeated, therefore, we can try to save the webform or some of the same content in the server memory for a period of time to speed up the response of the program.
This mode is easy to implement. You only need to add the following to the page:
<%@ Outputcache duration = "60" varybyparam = "NONE" %>,
This indicates that the page will expire after 60 seconds. That is to say, the content of the page is the same for all visitors within 60 seconds, but the response speed is greatly improved, just like static html pages.
Maybe you just want to save part of the content instead of saving the whole page, then we will return to sqlhelper. CS in MVC mode, and I made a few changes to it:
Public static dataset getportal ()
{
Dataset dataset;
If (httpcontext. Current. cache ["select_portal_cache"]! = NULL)
{
// If the data exists in the cache, it is taken out directly.
Dataset = (Dataset) httpcontext. Current. cache ["select_portal_cache"];
}
Else
{
// Otherwise, the database is retrieved and inserted into the cache, and the absolute expiration time is set to 3 minutes.
Dataset = getdataset (SQL _select_portal );
Httpcontext. Current. cache. insert ("select_portal_cache", dataset, null, datetime. Now. addminutes (3), timespan. Zero );
}
Return dataset;
}
Here, select_portal_cache is used as the cache key and the content retrieved from getdataset (SQL _select_portal) is used as the cache value. In this way, in addition to database operations performed by the Program for 1st calls, database operations are not performed during the cache expiration time, which also greatly improves the response capability of the program.
Summary
Since. the introduction of the Net Framework into the design model has greatly improved its strength in enterprise-level applications. It is no exaggeration to say that it is enterprise-level applications. net has caught up with the pace of Java and has come to the fore. This article uses an example to show the readers in. net Framework.