第九回 Microsoft.Practices.Unity.Interception实现基于数据集的缓存(针对六,七,八讲的具体概念和配置的解说)

时间:2022-03-31 15:59:43

返回目录

概念

Microsoft.Practices.Unity.Interception是一个拦截器,它隶属于Microsoft.Practices.Unity组成之中,主要完成AOP的功能,而实现AOP方法拦截(页向切面)的基础就是IoC,我们需要配置相关接口或者类型的IoC方式,然后在生产对象时,使用Unity的方法进行动态生产对象,这时,你的Interception拦截器也会起作用!

相关技术

IoC: 控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题。 控制反转一般又被称为依赖注入(Dependency Injection,简称DI)。即将配置中的对象,动态注入到项目程序当中,使程序的某个功能可以动态进行切换。

AOP: 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

一般用它来实现:日志记录,性能统计,安全控制,事务处理,异常处理等等。

持久化方式

目前我的Project.UnityCaching组件支持Microsoft.Practices.EnterpriseLibrary.Caching和Redis两种数据持久化的方式,前者也是Microsoft.Practices. EnterpriseLibrary企业库的一部分,主要实现内存持久化(也可以实现文件持久化),主要用在单台WEB服务器上;后者可以单独部署到一台或者多台服务器上,主要实现分布式缓存,它是未来的大数据的一个方向。

键名组成

缓存解决方案名_命名空间_方法名,其中缓存解决方案名可以自己去配置,对应config中的CacheProjectName节点,而键对应的值采用字典类型,字典的键对应方法的各参数的组合。

涉及到的相关程序集

IoC基础库程序集

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXUAAACfCAIAAACJN97PAAAM1klEQVR4nO2dz24byRGHmafIK+gJcs5jGNA+hi85G9BZ8jGA3kCCJQgCCNqGFzF08WVPWixiBBsE2CC5JDYlUhL/aHJQLNDT1d1VzS5ySH7fwaCG3VW/qpn5De1dsXoNAIAPvXULAICtBX8BAC/wFwDwAn8BAC/wFwDwAn8BAC/M/vJjk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CIRuwKy0w2lrXaA2hFbArDQArIObMQAugQMBcAkcCIBL4EAAXAIHAuCSfwEYCY3W03Ll/QAAAABJRU5ErkJggg==" alt="" />

配置文件中进行对IoC,AoP的配置

  <configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
<section name="RedisConfig" type="Redis.Client.RedisConfigInfo, Redis.Client"/>
</configSections> <RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" MaxWritePoolSize="" MaxReadPoolSize="" AutoStart="true" LocalCacheTime="" RecordeLog="false">
</RedisConfig> <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="" maximumElementsInCacheBeforeScavenging="" numberToRemoveWhenScavenging="" 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> <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" /> <container>
<!--泛型类型注入,数据仓储服务注入,可以由ef,linq,ado,memory,file,nosql等方式实现-->
<register type="IRepository.Core.IUnitOfWork,IRepository.Core" mapTo="NLayer_IoC_Demo.Entity.backgroundTestIoCEntities,NLayer_IoC_Demo.Entity" />
<!--使用redis作为持久化存储-->
<!--<register type="IRepository.Core.IRepository`1,IRepository.Core" mapTo="Redis.Data.Core.RedisRepository`1,Redis.Data.Core" />-->
<!--使用SQLSERVER作为持久化存储-->
<register type="IRepository.Core.IRepository`1,IRepository.Core" mapTo="NLayer_IoC_Demo.DATA.backgroundRepositoryBase`1,NLayer_IoC_Demo.DATA" />
<register type="IRepository.Core.IExtensionRepository`1,IRepository.Core" mapTo="NLayer_IoC_Demo.DATA.backgroundRepositoryBase`1,NLayer_IoC_Demo.DATA" /> <!-- AOP注入 -->
<extension type="Interception" />
<register type="NLayer_IoC_Demo.BLL.IUserService,NLayer_IoC_Demo.BLL" mapTo="NLayer_IoC_Demo.BLL.UserService,NLayer_IoC_Demo.BLL" >
<!--接口拦截-->
<interceptor type="InterfaceInterceptor" />
<!--缓存注入-->
<interceptionBehavior type="Project.UnityCaching.CachingBehavior,Project.UnityCaching"/>
</register> <register type="NLayer_IoC_Demo.BLL.OrderService,NLayer_IoC_Demo.BLL" >
<!--接口拦截-->
<interceptor type="VirtualMethodInterceptor" />
<!--虚方法注入,生成派生类进行拦截,让我们省去了建立接口的时间,不错的选择-->
<!--Transparent Proxy Interceptor -->
<!--它是注入所有的virutal method ,method,interface,但它的性能太差了-->
<!--缓存注入-->
<interceptionBehavior type="Project.UnityCaching.CachingBehavior,Project.UnityCaching"/>
</register> <register type="Project.InterceptionBehaviors.IHandle, Project.InterceptionBehaviors" mapTo="Project.InterceptionBehaviors.StandardHandle, Project.InterceptionBehaviors">
<!--接口拦截-->
<interceptor type="InterfaceInterceptor" />
<interceptionBehavior type="NLayer_IoC_Demo.BLL.AOP.LoggerBehavior,NLayer_IoC_Demo.BLL" />
</register> </container>
</unity> <connectionStrings>
<add name="backgroundTestIoCEntities" connectionString="metadata=res://*/background.csdl|res://*/background.ssdl|res://*/background.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=backgroundTestIoC;persist security info=True;user id=sa;password=zzl123;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-NLayer_IoC_Demo-20141024091423;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-NLayer_IoC_Demo-20141024091423.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>

使用时的相关注意要点

  1. Unity组件中引入了服务定位器ServiceLocator的概念,它可以使我们不引用目标程序集,而自动在bin目录自动去定位。
  2. 对于Redis方式的缓存来说,进行缓存的实体类需要被声明为Serializable特性。
  3. 实现IoC,AoP时,只引用需要的程序集,而不用将所有Microsoft.Practices.Unity组件都引入,在程序进行编译时,这些需要的程序集会自动添加到UI项目的BIN目录。
  4. 方法拦截这块有三种,但TransparentProxyInterceptor由于性能太差,我们并不提倡使用,项目中我们主要使用VirtualMethodInterceptor对于虚方法的拦截和InterfaceInterceptor对于接口的拦截,两种方式各有好处,如果实现方式比较单一,可以直接使用虚方法注入,这样可以省去写接口的代码量。

返回目录