简体   繁体   English

如何使用Castle Windsor注册通用装饰器?

[英]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.

相关问题 如何在温莎城堡中向多个通用服务注册多个通用组件? - How can I register multiple generic components with multiple generic services in castle windsor? 城堡温莎公约注册-如何基于实体注册通用存储库? - Castle Windsor Register by Convention - how to register generic Repository based on Entity? 我该如何在温莎城堡注册? - How do would I register this in Castle Windsor? 如何通过Castle Windsor接口注册开放泛型类型? - How to register open generic type by interface with Castle Windsor? 如何在温莎城堡注册mongodb? - How to register mongodb with castle windsor? 在 Castle Windsor 中注册泛型接口的多个实现 - Register multiple implementations of generic interface in Castle Windsor 在Castle Windsor中,我可以注册一个Interface组件并获得实现的代理吗? - In Castle Windsor, can I register a Interface component and get a proxy of the implementation? 如何命名温莎城堡中的容器? - How can I name a container in Castle Windsor? 温莎城堡-如何将实现泛型接口的接口注册到具有泛型的类? - Castle Windsor - How can I register an interface that implements an interface with generics to a class with generics? 当还有专门的服务类型时,使用 Castle Windsor 的流畅接口在装饰器链中注册组件? - Using Castle Windsor's fluent interface to register components in a decorator chain, when there are also specialised service-types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM