[英]How can I register a generic decorator using Castle Windsor?
I need decorate all based on ICommandHandler<T>
types using a corresponding DeadlockRetryCommandHandlerDecorator<T>
type 我需要使用相应的
DeadlockRetryCommandHandlerDecorator<T>
类型基于ICommandHandler<T>
类型来装饰所有类型
I tried this solution, but unfortunately it doesn't work. 我试过这个解决方案,但遗憾的是它不起作用。
container.Register(
Component.For(typeof(ICommandHandler<>))
.ImplementedBy(typeof(DeadlockRetryCommandHandlerDecorator<>)));
container.Register(
AllTypes.FromThisAssembly()
.BasedOn(typeof(ICommandHandler<>))
.WithService.Base());
How can i register a generic decorator ( DeadlockRetryCommandHandlerDecorator<T>
) to wrap all generic ICommandHandler<T>
implementations? 如何注册通用装饰器(
DeadlockRetryCommandHandlerDecorator<T>
)来包装所有通用ICommandHandler<T>
实现?
currently this is not supported OOTB due to the fact that Windsor always favours mode specific component over an open-generic. 目前这不支持OOTB,因为Windsor总是倾向于特定于模式的组件而不是开放式泛型。
You can get that working quite easily with an ISubDependencyResolver
though. 但是,使用
ISubDependencyResolver
可以很容易地实现这ISubDependencyResolver
。 The code below assumes you name the component for your decorator "DeadlockRetryCommandHandlerDecorator"
下面的代码假设您为装饰器
"DeadlockRetryCommandHandlerDecorator"
命名组件
public class CommandHandlerResolver : ISubDependencyResolver
{
private readonly IKernel kernel;
public FooResolver(IKernel kernel)
{
this.kernel = kernel;
}
public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
{
return (dependency.TargetType.IsGenericType &&
dependency.TargetType.GetGenericTypeDefinition() == typeof (ICommandHandler<>)) &&
(model.Implementation.IsGenericType == false ||
model.Implementation.GetGenericTypeDefinition() != typeof (DeadlockRetryCommandHandlerDecorator<>));
}
public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
{
return kernel.Resolve("DeadlockRetryCommandHandlerDecorator", dependency.TargetItemType);
}
}
The recommended way of achieving scenarios like that with Windsor however is by using interceptors. 然而,使用拦截器来实现与Windsor一样的场景的推荐方法。
I had the same issue. 我遇到过同样的问题。 I managed to resolve it by registering each type explicity as more specific type.
我设法通过将每种类型明确注册为更具体的类型来解决它。 For me this solution is more clear than using sub dependency resolver
对我来说,这个解决方案比使用子依赖解析器更清晰
var commandTypes = businessAssembly.GetTypes()
.Where(t => !t.IsInterface && typeof(ICommand).IsAssignableFrom(t));
foreach(var commandType in commandTypes)
{
var handlerInterface = typeof(ICommandHandler<>).MakeGenericType(new[] { commandType });
var transactionalHandler = typeof(DeadlockRetryCommandHandlerDecorator<>).MakeGenericType(new[] { commandType });
container.Register(Component.For(handlerInterface)
.ImplementedBy(transactionalHandler)
.LifeStyle.PerWebRequest);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.