【问题标题】:Applying generic decorators conditionally in Autofac based on configuration values根据配置值在 Autofac 中有条件地应用通用装饰器
【发布时间】:2014-05-05 12:35:27
【问题描述】:

我有一个带有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.
    }
}

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

【问题讨论】:

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


    【解决方案1】:

    这最喜欢涉及实现您自己的IRegistrationSource。如果您提取 Autofac 的代码并查看 OpenGenericDecoratorRegistrationSource,那应该会让您走上正轨。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-21
      • 2016-11-25
      • 1970-01-01
      • 2017-03-22
      • 2013-09-30
      • 1970-01-01
      • 2016-05-24
      • 2021-01-02
      相关资源
      最近更新 更多