我有一個Unity容器,其中包含許多註冊,這些註冊將被攔截以進行日誌記錄。將InterceptionBehaviour添加到在IoC容器中註冊的所有內容
有沒有辦法我可以貪心地加上攔截器到每註冊?感覺像Ctrl-C, Ctrl+V
過載。
var container = new UnityContainer();
container
.AddNewExtension<Interception>()
.RegisterType<IDummy1, Dummy1>(new Interceptor<InterfaceInterceptor>(), loggingInterceptionBehavior)
.RegisterType<IDummy2, Dummy2>(new Interceptor<InterfaceInterceptor>(), loggingInterceptionBehavior)
.RegisterType<IDummy3, Dummy3>(new Interceptor<InterfaceInterceptor>(), loggingInterceptionBehavior)
.RegisterType<IDummy4, Dummy4>(new Interceptor<InterfaceInterceptor>(), loggingInterceptionBehavior)
.RegisterType<IDummy5, Dummy5>(new Interceptor<InterfaceInterceptor>(), loggingInterceptionBehavior)
我看到下面的例子here,使用XML配置:
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
<!-- Using Interception -->
<container name="Interception">
<extension type="Interception" />
<register type="AopTest.ILogger, AopTest" mapTo="AopTest.Logger, AopTest" />
<register type="AopTest.ICalculator, AopTest" mapTo="AopTest.Calculator, AopTest" />
<interceptor type="InterfaceInterceptor" />
<interceptionBehavior type="Infrastructure.LoggingInterceptionBehavior, Infrastructure" />
</register>
</container>
</configuration>
我假設寄存器Infrastructure.LoggingInterceptionBehavior
兩個AopTest.ILogger
和AopTest.ICalculator
。 (?)
我怎麼能在代碼中做到這一點?
鏈接示例使用名稱匹配規則。還有一個使用名稱空間匹配規則(NamespaceMatchingRule)。您可以註冊您的主命名空間,並且該命名空間下的所有註冊實現將被攔截。 – Wizact