我心中的核心組件(可插拔的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,我們看了兩種攔截器的實現,選用哪種方式完全是看你的具體場合了,呵呵.

對緩存組件的封裝請下載

回到目錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM