我心中的核心组件(可插拔的AOP)~第十三回 实现AOP的拦截组件Unity.Interception
说在前
本节主要说一下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,我们看了两种拦截器的实现,选用哪种方式完全是看你的具体场合了,呵呵.
对缓存组件的封装请下载
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2011-12-09 MVC项目中对Service(BLL)层的抽象
2011-12-09 事务应该放在BLL层还是DAL层