How can I register a generic decorator using Castle Windsor?

前端 未结 2 2027

I need decorate all based onICommandHandler types using a corresponding DeadlockRetryCommandHandlerDecorator type

I tried

2条回答
  •  臣服心动
    2020-12-01 20:52

    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);
    }
    

提交回复
热议问题