문제

I have an application with an command/handler based architecture. I have the following interface:

public interface ICommandHandler<TCommand>
{
    void Handle(TCommand command);
}

There are many non-generic implementations of this interface. Those implementations are wrapped by generic decorators such as:

public class ProfilingCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
{
    private readonly ICommandHandler<TCommand> decoratee;

    public ProfilingCommandHandlerDecorator(ICommandHandler<TCommand> decoratee)
    {
        this.decoratee = decoratee;
    }

    public void Handle(TCommand command)
    {
        // do profiling here
        this.decoratee.Handle(command);
        // aaand here.
    }
}

Some of these decorators however should be applied conditionally based on the flag in the config file. I found this answer that refers to applying non-generic decorators conditionally; not about generic decorator. How can we achieve this with generic decorators in Autofac?

도움이 되었습니까?

해결책

This most like involves implementing your own IRegistrationSource. If you pull the code for Autofac and look at OpenGenericDecoratorRegistrationSource, that should get you on the right track.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top