Unity实现Aop的配置

本文介绍如何使用Unity框架配置AOP(面向切面编程),通过实现ICallHandler接口并定义相应的策略来为特定命名空间或成员名称应用日志记录和异常处理等切面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先实现ICallHandler接口,得到自己的AOP,然后如下配置

 

<?xml version="1.0" encoding="utf-8" ?>
<!--<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
-->

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <!--新增配置节扩展,用于下面的<interception>配置节-->
    <sectionExtension  type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Microsoft.Practices.Unity.Interception.Configuration" />

    <alias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,Microsoft.Practices.Unity" />
    <alias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity" />
    <alias alias="interception" type="Microsoft.Practices.Unity.InterceptionExtension, Microsoft.Practices.Unity.Interception" />
    <alias alias="transparentProxy" type="Microsoft.Practices.Unity.InterceptionExtension.TransparentProxyInterceptor, Microsoft.Practices.Unity.Interception" />
    <alias alias="typeMatchingRule" type="Microsoft.Practices.Unity.InterceptionExtension.TypeMatchingRule, Microsoft.Practices.Unity.Interception"/>
    <alias alias="memberNameMatchingRule" type="Microsoft.Practices.Unity.InterceptionExtension.MemberNameMatchingRule, Microsoft.Practices.Unity.Interception"/>
    
    <alias alias="ICustomerRepository" type="AzureCRM.Domain.Model.BaseModel.Entity.Aggregates.CustomerAgg.ICustomerRepository,AzureCRM.Domain.Model" />
    <alias alias="CustomerRepository" type="AzureCRM.Infrastructure.Data.Repositories.BaseModel.Repositories.CustomerRepository,AzureCRM.Infrastructure.Data.Repositories" />
    <alias alias="ILogAop" type="AzureCRM.Infrastructure.Crosscutting.Aop.Aspects.ILogAop,AzureCRM.Infrastructure.Crosscutting" />
    <alias alias="LogAop" type="AzureCRM.Infrastructure.Crosscutting.NetFramework.Aop.Aspects.LogAop,AzureCRM.Infrastructure.Crosscutting.NetFramework" />
    <alias alias="ExceptionAop" type="AzureCRM.Infrastructure.Crosscutting.NetFramework.Aop.Aspects.ExceptionAop,AzureCRM.Infrastructure.Crosscutting.NetFramework" />
    <alias alias="IOut" type="AzureCRM.Infrastructure.CrossCutting.NetFramework.Test.Aop.IOut,AzureCRM.Infrastructure.CrossCutting.NetFramework.Test" />
    <alias alias="Out" type="AzureCRM.Infrastructure.CrossCutting.NetFramework.Test.Aop.Out,AzureCRM.Infrastructure.CrossCutting.NetFramework.Test" />
     
    
    <containers>
      <container name="AzureCRMContainer">
        <extension type="Interception" />

        <interception>
          <!--第一种策略,直接指定匹配规则-->
          <policy name="LogAopPolicy">
            <matchingRule name="LogMatching" type="NamespaceMatchingRule">
              <constructor>
                <param name="namespaceName" value="AzureCRM.Infrastructure.CrossCutting.NetFramework.Test.Aop" />
              </constructor>
            </matchingRule>
            <callHandler name="Log" type="LogAop" />
          </policy>
          <!--第二种策略,只指定匹配规则名,后面注册规则名-->
          <policy name="ExceptionAopPolicy">
            <matchingRule name="ExceptionAopMatching" />
            <callHandler name="exception" type="ExceptionAop" />
          </policy>
        </interception>

        <register type="IOut" mapTo="Out">
          <interceptor type="TransparentProxyInterceptor" />
          <interceptionBehavior type="PolicyInjectionBehavior" />
        </register>
        
        <!--注册匹配规则-->
        <register type="IMatchingRule" name="ExceptionAopMatching" mapTo="MemberNameMatchingRule">
          <constructor>
            <param name="nameToMatch" value="Console*" />
          </constructor>
        </register>
        
      </container>
    </containers>
  </unity>
<!--</configuration>-->

 

转载于:https://www.cnblogs.com/wudingfeng/archive/2011/12/18/2291920.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值