根据配置值在Autofac中有条件地应用通用装饰器

时间:2014-05-05 12:35:27

标签: c# dependency-injection ioc-container autofac

我有一个command/handler based architecture的应用程序。我有以下界面:

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

此接口有许多非通用实现。这些实现由通用装饰器包装,例如:

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.
    }
}

然而,应根据配置文件中的标志有条件地应用其中一些decotator。我发现this answer指的是有条件地应用非通用装饰器;不是关于通用装饰器。我们如何通过Autofac中的通用装饰器实现这一目标?

1 个答案:

答案 0 :(得分:0)

这最像是要实现自己的IRegistrationSource。如果您提取Autofac代码并查看OpenGenericDecoratorRegistrationSource,那么这应该会让您走上正确的轨道。