发文章
发文工具
撰写
网文摘手
文档
视频
思维导图
随笔
相册
原创同步助手
其他工具
图片转文字
文件清理
AI助手
留言交流
本节主要说一下Unity家族里的拦截组件,对于方法拦截有很多组件提供,基本上每个Ioc组件都有对它的实现,如autofac,它主要用在orchard项目里,而castle也有以拦截的体现,相关可以看我的Castle~实现IoC容器这篇文章,而今天主要说一个Unity里的方法拦截的实现,事实上本篇文章是对第二回 缓存拦截器的一个扩展和补充,对于unity这东西在微软的Nlayer项目里有所体现,它是基于DDD构架的,无论在架构选型还是技术选型上都很超前,也都结合了很大微软高手的心血,可读性很高,呵呵.
通过IoC建立对象实例的方法时,它们的配置信息一般有两种方式存储,第一可以通过C#程序进行存储并建立,第二可以通过配置文件先进行配置,然后在程序里直接调用即可,今天这篇文章,我们将对这两种方法进行说明.
<!--BEGIN: Unity--> <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" /> <container> <extension type="Interception" /> <register type="Project.Caching.ICacheProvider, MvcApplication2" mapTo="Project.Caching.EntLibCacheProvider, MvcApplication2" /> <!--缓存的拦截--> <register type="接口类型,程序集" mapTo="接口实现,程序集"> <!--<interceptor type="InterfaceInterceptor" />--> <interceptor type="InterfaceInterceptor" /> <interceptionBehavior type="Project.InterceptionBehaviors.CachingBehavior, MvcApplication2" /> </register> </container> </unity> <!--END: Unity--> <cachingConfiguration defaultCacheManager="ByteartRetailCacheManager"> <cacheManagers> <add name="ByteartRetailCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" expirationPollFrequencyInSeconds="600" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" /> <!-- expirationPollFrequencyInSeconds:过期时间(seconds) maximumElementsInCacheBeforeScavenging:缓冲中的最大元素数量 numberToRemoveWhenScavenging:一次移除的数量 --> </cacheManagers> <backingStores> <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="NullBackingStore" /> </backingStores> </cachingConfiguration>
程序里直接通过IOrderRepository来触发它自己的方法拦截
Repository.IOrderRepository iOrderRepository = ServiceLocator.Instance.GetService<IOrderRepository>();
如果希望在程序里控制它,代码就多了一些,控制上比较灵活,配置文件是全局性的,而代码里,可以有需要的时候进行创建
config配置中不需要对unity初始化,直接对caching节点进行声明即可
<cachingConfiguration defaultCacheManager="ByteartRetailCacheManager"> <cacheManagers> <add name="ByteartRetailCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" expirationPollFrequencyInSeconds="600" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" /> <!-- expirationPollFrequencyInSeconds:过期时间(seconds) maximumElementsInCacheBeforeScavenging:缓冲中的最大元素数量 numberToRemoveWhenScavenging:一次移除的数量 --> </cacheManagers> <backingStores> <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="NullBackingStore" /> </backingStores> </cachingConfiguration>
C#程序部分
//创建容器 IUnityContainer container = new UnityContainer(); //注册映射 container.RegisterType<IOrderRepository, OrderRepository>(); //添加unity扩展,扩展类型是一个拦截器 container.AddNewExtension<Interception>(); //为接口IOrderRepository注册拦截器,它的方式是接口拦截器,拦截器的实现是一个行为,它的实现体是Project.InterceptionBehaviors.CachingBehavior container.RegisterType<IOrderRepository, OrderRepository>( new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<Project.InterceptionBehaviors.CachingBehavior>());
OK,我们看了两种拦截器的实现,选用哪种方式完全是看你的具体场合了,呵呵.
对缓存组件的封装请下载
来自: 昵称10504424 > 《工作》
0条评论
发表
请遵守用户 评论公约
Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)
5. 要使用缓存应用程序模块, 需要导入相应的Dll文件,在此我们要导入的是Microsoft.Practices.EnterpriseLibrary.Caching.dll ,将App....
使用Unity实现 AOP
使用Unity实现 AOP.Unity.dll.Unity.14: type="Microsoft.Practices.Unity.InterceptionExtension.TransparentProxyInterceptor, M...
再谈PIAB与Unity之间的集成
NuGet Gallery | Unity Interception Extension ...
NuGet Gallery | Unity Interception Extension ...Unity Interception Extension 2.1.505.2.Unity interception enables you to effectively capture calls to objects and add additional functional...
Web.Config配置数据库连接
企业库中 DatabaseFactory.CreateDatabase 方法创建数据库实例的
企业库中 DatabaseFactory.CreateDatabase 方法创建数据库实例的 企业库中 DatabaseFactory.CreateDatabase 方法创建数据库实例的 逻辑过程。这个数据库配置文件生成的 Database dbSvc = DatabaseFacto...
Enterprise Library 4.1学习笔记2----数据访问程序块 - 菩提树下的杨过.Net - 博客园
<connectionStrings> <add name="Conn"connectionString="Data Source=.;Initial Catalog=Six;Persist SecurityInfo=True;User ID=sa;Password=***"providerName="S...
使用StructureMap扩展ASP.NET MVC三层架构6
使用StructureMap扩展ASP.NET MVC三层架构6使用StructureMap扩展ASP.NET MVC三层架构6-Enterprise Library实现异常处理?Interfaces{ p...
通过定义UnityContainer扩展变”Explicit Interception”为”Automatic Interception”
其中Foo实现了接口IFoo,而Foo依赖于Bar,Bar依赖于Baz。6: public class Foo : IFoo.7: IFoo foo = container.现在我们将应用在Foo、Bar和Baz上的SimpleCallHandlerAttribute特性全部删除,通过如下的...
微信扫码,在手机上查看选中内容