我正在逐个注册几种类型:
container.Register(
Component.For<IService1>().ImplementedBy<ServiceImpl1>(),
Component.For<IService2>().ImplementedBy<ServiceImpl2>(),
Component.For<IService3>().ImplementedBy<ServiceImpl3>(),
Component.For<IService4>().ImplementedBy<ServiceImpl4>()
);
有没有办法为已注册的所有类型的程序集注册拦截器?我不希望按惯例注册所有类型AllTypes
。我只是想添加一个拦截器而不写
.Interceptors(InterceptorReference.ForType<DummyInterceptor>())
.SelectedWith(new FooInterceptorSelector()).Anywhere
数十万次。
谢谢!
答案 0 :(得分:0)
使用ComponentModel贡献者时,如何指定.SelectedWith? 我可以通过这样做为所有方法添加一个拦截器。
public class MyContributor : IContributeComponentModelConstruction
{
public void ProcessModel(IKernel kernel, ComponentModel model)
{
model.Dependencies.Add(new DependencyModel(DependencyType.Service, null, typeof(MyInterceptor), false));
model.Interceptors.AddFirst(InterceptorReference.ForType<MyInterceptor>());
}
}
答案 1 :(得分:0)
作为suggested by Dan,可以使用自定义ComponentModel construction contributors来实现。
首先,请注意
...
.Interceptors(InterceptorReference.ForType<DummyInterceptor>())
.SelectedWith(new FooInterceptorSelector())
.Anywhere
基本等同于
...
.SelectInterceptorsWith(new FooInterceptorSelector())
.Interceptors(InterceptorReference.ForType<DummyInterceptor>())
.Anywhere
即,将IInterceptorSelector
与ComponentModel
相关联独立于注册将使用IInterceptorSelector
选择的拦截器。而且,每个ComponentModel
可能只有一个拦截器选择器。
由于这种分离,最好使用两个独立的贡献者解决问题 - 一个用于注册拦截器,另一个用于注册拦截器选择器。
第一个与Dan's answer中显示的基本相同。但是,没有必要为拦截器添加DependencyModel
,因为拦截器应该已经使用类似
container.Register(Component.For<DummyInterceptor>().LifestyleTransient());
所以它看起来像
using Castle.Core;
using Castle.MicroKernel;
using Castle.MicroKernel.ModelBuilder;
...
public class InterceptorContributor : IContributeComponentModelConstruction
{
private readonly InterceptorReference interceptor;
public InterceptorContributor(InterceptorReference interceptor)
{
this.interceptor = interceptor;
}
public void ProcessModel(IKernel kernel, ComponentModel model)
{
if (...)
{
model.Interceptors.Add(interceptor)
}
}
}
或者如果您希望能够一次注册多个拦截器,则可以利用InterceptorDescriptor
的现有实现,而是:
using System;
using Castle.Core;
using Castle.MicroKernel;
using Castle.MicroKernel.ModelBuilder;
using Castle.MicroKernel.ModelBuilder.Descriptors;
...
public class InterceptorsContributor : IContributeComponentModelConstruction
{
private readonly Predicate<ComponentModel> predicate;
private readonly IComponentModelDescriptor interceptorsDescriptor;
public InterceptorsContributor(Predicate<ComponentModel> predicate, params InterceptorReference[] interceptors)
{
this.predicate = predicate ?? throw new ArgumentNullException(nameof(predicate));
interceptorsDescriptor = new InterceptorDescriptor(interceptors);
}
public void ProcessModel(IKernel kernel, ComponentModel model)
{
if (predicate.Invoke(model))
{
interceptorsDescriptor.BuildComponentModel(kernel, model);
interceptorsDescriptor.ConfigureComponentModel(kernel, model);
}
}
}
对于注册IInterceptorSelector
,可以通过执行InterceptorSelectorDescriptor
执行的操作来实现贡献者:
public void ProcessModel(IKernel kernel, ComponentModel model)
{
if (...)
{
var options = model.ObtainProxyOptions();
options.Selector = new InstanceReference<IInterceptorSelector>(selector);
}
}
或直接使用InterceptorSelectorDescriptor
:
using System;
using Castle.Core;
using Castle.DynamicProxy;
using Castle.MicroKernel;
using Castle.MicroKernel.ModelBuilder;
using Castle.MicroKernel.ModelBuilder.Descriptors;
...
public class InterceptorSelectorContributor : IContributeComponentModelConstruction
{
private readonly Predicate<ComponentModel> predicate;
private readonly IComponentModelDescriptor interceptorSelectorDescriptor;
public InterceptorSelectorContributor(Predicate<ComponentModel> predicate, IInterceptorSelector selector)
{
this.predicate = predicate ?? throw new ArgumentNullException(nameof(predicate));
var selectorRef = new InstanceReference<IInterceptorSelector>(selector);
interceptorSelectorDescriptor = new InterceptorSelectorDescriptor(selectorRef);
}
public void ProcessModel(IKernel kernel, ComponentModel model)
{
if (predicate.Invoke(model))
{
interceptorSelectorDescriptor.BuildComponentModel(kernel, model);
interceptorSelectorDescriptor.ConfigureComponentModel(kernel, model);
}
}
}