如何配置Unity 2.0 Policy Injection以在配置文件中使用自定义属性匹配规则?

时间:2012-01-10 06:25:44

标签: unity-container enterprise-library aop policy-injection unity-interception

如何配置Unity 2.0策略注入以在配置文件中使用自定义属性匹配规则?

我想要的是在Unity配置文件中翻译以下代码片段。

myContainer.Configure<Interception>()
       .AddPolicy("MyPolicy")
       .AddMatchingRule<CustomAttributeMatchingRule>
           (new InjectionConstructor(typeof(MyAttributeType), true))
       .AddCallHandler<MyCallHandler>
            ("MyValidator", 
            new ContainerControlledLifetimeManager());

1 个答案:

答案 0 :(得分:-1)

可以如下配置,[TypeName]需要根据您的程序集正确配置。

......

<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>

<container>
  <extension type="Interception"/>
  <interception>
    <policy name="MyPolicy">
      <matchingRule name="customAttribute" type="CustomAttributeMatchingRule">
        <constructor>
          <param name="attributeType" type="[MyAttributeType]"/>
          <param name="inherited" value="true"/>
        </constructor>
      </matchingRule>
      <callHandler name="MyValidator" type="[MyCallHandler]">
        <lifetime type="transient"/>
      </callHandler>
    </policy>
  </interception>
</container>

......