Wrapper模式的实现
作为.net framework 3.5 sp1 新添加的一个程序集,System.Web.Abstractions里所有的类,都是Wrapper/Decorator模式的。(System.Web.Abstractions里的类可以参见后面的附录,System.Web.Abstractions其实也早早出现在了Asp.net Mvc中)
在这里,所有的Wrapper类都是继承于Base类的(例如HttpApplicationStateWraper与HttpApplicationStateBase)。
Wrapper类使用构造函数将原来的HttpApplicationState等类的实例传入。并用this[]实现了可以读取。
在初始化时用以下方法将System.Web程序集中的HttpApplicationState类的对象传入
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->publicHttpApplicationStateWrapper(HttpApplicationStatehttpApplicationState)
{
if(httpApplicationState==null)
{
thrownewArgumentNullException("httpApplicationState");
}
this._application=httpApplicationState;
}
然后使用索引器设置其访问方式
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->publicoverrideobjectthis[intindex]
{
get
{
returnthis._application[index];
}
}
及
Code
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->publicoverrideobjectthis[stringname]
{
get
{
returnthis._application[name];
}
set
{
this._application[name]=value;
}
}
使之可以访问,再加上实现了Base类中的各种方法,这样,就实现了Wrapper/Decorator模式
但有一点要注意:HttpApplicationState和HttpApplicationStateBase并非同一个类,这一点与装饰者模式有些冲突。这是因为当初在建立HttpApplicationState并没有想到日后的扩展性,所以没有对它进行抽像类的处理,所以在3.5sp1中又加入了和它的成员几近相同的HttpApplicationStateBase。当然其它类也是这样。所以UML如下图
其实Wrapper要实现的功能是这样的
当然,其它的Wrapper也是这样。
正是由于有了这么好的扩展所以在其应用中可以比较方便地扩展出新的Application或Session存储方式。
在Asp.ne tMvc中的应用
如Asp.net Mvc的Controller类中Session的声明方式:
public HttpSessionStateBase Session { get; }
这里就使用了HttpSessionStateBase类,这样就可以很方便地扩展Wrapper了
而Asp.net中所有的Context都是由HttpContext来创建的因为Application、Server这些对象都是它的属性。
Mvc创建HttpContext在MvcHandler中
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->protectedvirtualvoidProcessRequest(HttpContexthttpContext){
HttpContextBaseiHttpContext=newHttpContextWrapper(httpContext);
ProcessRequest(iHttpContext);
}
这个方法在HtmlHelper的RenderRoute中有使用到过:
<!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->publicstaticvoidRenderRoute(thisHtmlHelperhelper,RouteValueDictionaryvalues){
varrouteData=newRouteData();
foreach(varkvpinvalues){
routeData.Values.Add(kvp.Key,kvp.Value);
}
varhttpContext=helper.ViewContext.HttpContext;
varrequestContext=newRequestContext(httpContext,routeData);
varhandler=newRenderActionMvcHandler(requestContext);
handler.ProcessRequestInternal(httpContext);
}
其中RenderActionMvcHandler是MvcHandler的一个继承。
附录:System.Web.Abstractions中的类及Wrapper
HttpApplicationStateBase
HttpApplicationStateWrapper
HttpBrowserCapabilitiesBase
HttpBrowserCapabilitiesWrapper
HttpCachePolicyBase
HttpCachePolicyWrapper
HttpContextBase
HttpContextWrapper
HttpFileCollectionBase
HttpFileCollectionWrapper
HttpPostedFileBase
HttpPostedFileWrapper
HttpRequestBase
HttpRequestWrapper
HttpResponseBase
HttpResponseWrapper
HttpServerUtilityBase
HttpServerUtilityWrapper
HttpSessionStateBase
HttpSessionStateWrapper
HttpStaticObjectsCollectionBase
HttpStaticObjectsCollectionWrapper
相关推荐
刚刚程序报错,需要System.Web.Abstractions.dll这个程序集,上网搜索到CSDN这里竟然要5分,那位兄弟太不厚道了,所以到DLL站上面下了个下来,现在上传上去,希望能对你有用。
[System.Web.Mvc.dl][System.Web.Abstractions.dll][System.Web.Routing.dll] 三个dll
System.Web.Abstractions.zip
解决以下错误:“未能加载文件或程序集...该下载文件,包含了缺失引用的MVC3.0所有相关核心DLL集合:System.Web.Routing、System.Web.Extensions、System.Web.Mvc、System.Web.Abstractions、Microsoft.Web.Mvc 等等。
System.Web.Mvc.dll, System.ComponentModel.DataAnnotations.dll, Microsoft.Web.Infrastructure.dll, System.ComponentModel.DataAnnotations.dll, System.Web.Abstractions.dll, System.Web.Helpers.dll 等等。...
该库的核心是IFileSystem和FileSystem 。 与其直接调用File....dotnet add package System.IO.Abstractions public class MyComponent { readonly IFileSystem fileSystem ; // <summary>Create MyComponent with
与网络上现有样本相比的功能/改进使用ASP.NET Core MVC构建的干净WebAPI(无对System.Web的引用) 使用新的ASP.NET Core功能(配置等) 可以自托管或在IIS下运行文件操作被提取到自己的抽象层(不依赖于System.IO)...
EQXMedia.TxFileSystem是使用System.IO.Abstractions的.NET Standard文件系统抽象的事务性文件系统包装。 该文件系统包装程序支持以下事务: 文件, 目录, 文件流。 NuGet软件包 EQXMedia.TxFileSystem每个...
28 Adding Interactivity to Your Web Apps with ASP.NET AJAX 648 What Is AJAX? 648 Setting Up an ASP.NET AJAX Site 649 The AJAX Page Life Cycle 650 Loading Custom Script Libraries 652 ASP.NET...
ImageCirclePlugin, 为你的Xamarin.Forms 应用程序提供圆形图像 用于 Xamarin.Forms的图像控制插件简单但优雅的在 Xamarin.Forms 项目中显示圆形图像的方法设置在NuGet上可用:...
此快速IServiceProviderFactory{T}展示了如何使用ASP.NET Core支持的IServiceProviderFactory{T}集成来帮助您自动构建根服务提供者。 如果您想要更多的手动控制,。 请参考NuGet的Autofac.Extensions....
Avoid common pitfalls by choosing the right abstractions. Understand the performance implications of the underlying operating system and runtime. Improve the performance of your applications by ...
java写webapi源码日志记录 此库包含使用各种记录器框架和Microsoft.Extensions.Logging.ILogger接口的 .NET Core 和 ASP.NET Core 日志记录的有用资源。 - 只是辅助方法 - 只是辅助方法 地位 从 1.0.5 版开始生产...
GraphQL.Client.Abstractions.Websocket GraphQL.Client.LocalExecution GraphQL.Client.Serializer.Newtonsoft GraphQL.Client.Serializer.SystemTextJson GraphQL.Primitives 规范: 图书馆将尝试
3.7. General Web Improvements ............................................................................... 19 3.8. WebSocket, SockJS, and STOMP Messaging ..............................................
pluginfactory是.NET Core下一个依赖注入实现的插件框架,此框架是插件化开发与依赖注入的完美集合,同时集成了.NET Core中的配置机制,可以很好地与ASP.NET Core等框架融合。 使用向导 示例项目可参考: Xfrogcn....
3.7. General Web Improvements ............................................................................... 19 3.8. WebSocket, SockJS, and STOMP Messaging ..............................................
可以在本地使用或在CI / CD方案中使用 信息 项目 聊天室 问题 质量 建立Azure 质量 声纳虫 覆盖范围 NuGet软件包 官方 预习 WireMock.Net WireMock.Net.StandAlone WireMock.Net.FluentAssertions WireMock...
Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”和“Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”中 怎么解决?