Implementation of Wrapper Mode
As a newly added assembly of. net framework 3.5 sp1, all classes in System. Web. javasactions are in Wrapper/Decorator mode. (For the classes in System. Web. Category actions, see the appendix below. System. Web. Category actions is also early in Asp.net Mvc)
Here, all Wrapper Classes inherit from Base classes (such as HttpApplicationStateWraper and HttpApplicationStateBase ).
The Wrapper class uses constructor to input instances of the original class such as HttpApplicationState. You can use this [] to read data.
During initialization, use the following method to pass in the object of the HttpApplicationState class in the System. Web assembly
Public HttpApplicationStateWrapper (HttpApplicationState httpApplicationState)
{
If (httpApplicationState = null)
{
Throw new ArgumentNullException ("httpApplicationState ");
}
This. _ application = httpApplicationState;
}
Then, use the indexer to set the access method.
Public override object this [int index]
{
Get
{
Return this. _ application [index];
}
}
And
Code
Public override object this [string name]
{
Get
{
Return this. _ application [name];
}
Set
{
This. _ application [name] = value;
}
}
Make it accessible, and implement various methods in the Base class. In this way, the Wrapper/Decorator mode is implemented.
Note that HttpApplicationState and HttpApplicationStateBase are not the same class, which is in conflict with the decorator mode. This is because we didn't think of future scalability when we set up the HttpApplicationState, so we didn't perform image class processing on it. Therefore, we added HttpApplicationStateBase in 3.5sp1, which is nearly the same as its members. Of course, this is also true for other classes. So UML, such
In fact, this is the function that Wrapper wants to implement.
Of course, this is also true for other Wrapper.
It is precisely because of such a good extension that new Application or Session storage methods can be easily extended in their applications.
Application in Asp. ne tMvc
For example, the Session Declaration method in the Controller class of Asp.net Mvc:
Public HttpSessionStateBase Session {get ;}
The HttpSessionStateBase class is used here, so that Wrapper can be easily expanded.
All the contexts in Asp.net are created by HttpContext because Application and Server objects are their attributes.
Create HttpContext in MvcHandler for Mvc
Protected virtual void ProcessRequest (HttpContext httpContext ){
HttpContextBase iHttpContext = new HttpContextWrapper (httpContext );
ProcessRequest (iHttpContext );
}
This method has been used in RenderRoute of HtmlHelper:
Public static void RenderRoute (this HtmlHelper helper, RouteValueDictionary values ){
Var routeData = new RouteData ();
Foreach (var kvp in values ){
RouteData. Values. Add (kvp. Key, kvp. Value );
}
Var httpContext = helper. ViewContext. HttpContext;
Var requestContext = new RequestContext (httpContext, routeData );
Var handler = new RenderActionMvcHandler (requestContext );
Handler. ProcessRequestInternal (httpContext );
}
Here, RenderActionMvcHandler is an inheritance of MvcHandler.
Appendix: class and Wrapper in System. Web. Category actions
HttpApplicationStateBase
HttpApplicationStateWrapper
HttpBrowserCapabilitiesBase
HttpBrowserCapabilitiesWrapper
HttpCachePolicyBase
HttpCachePolicyWrapper
HttpContextBase
HttpContextWrapper
HttpFileCollectionBase
HttpFileCollectionWrapper
HttpPostedFileBase
HttpPostedFileWrapper
HttpRequestBase
HttpRequestWrapper
HttpResponseBase
HttpResponseWrapper
HttpServerUtilityBase
HttpServerUtilityWrapper
HttpSessionStateBase
HttpSessionStateWrapper
HttpStaticObjectsCollectionBase
HttpStaticObjectsCollectionWrapper