回到目錄
說在前
本節主要說一下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,我們看了兩種攔截器的實作,選用哪種方式完全是看你的具體場合了,呵呵.
對緩存元件的封裝請下載下傳
作者:倉儲大叔,張占嶺,
榮譽:微軟MVP
QQ:853066980
支付寶掃一掃,為大叔打賞!
