zoukankan      html  css  js  c++  java
  • 我心中的核心组件(可插拔的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,我们看了两种拦截器的实现,选用哪种方式完全是看你的具体场合了,呵呵.

    对缓存组件的封装请下载

    回到目录

     

     
  • 相关阅读:
    [转]asp.net页面缓存技术
    UL和LI在div中的高度的IE6下兼容性
    jquery制作的横向图片滚动带横向滚动条TackerScroll
    电脑可以上网,但是qq登陆不上去?
    Introduction to discrete event system学习笔记4.6
    Introduction to Discrete event system学习笔记4.9
    Introduction to discrete event systemsstudy 4.5
    Symbolic synthesis of obserability requirements for diagnosability B.Bittner,M.Bozzano,A.Cimatti,and X.Olive笔记4.16
    Introduction to discrete event system学习笔记4.8pm
    Introduction to discrete event system学习笔记 4.8
  • 原文地址:https://www.cnblogs.com/micro-chen/p/5780745.html
Copyright © 2011-2022 走看看