19

鉴于以下情况:

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

public class MoveCustomerCommand
{

}

public class MoveCustomerCommandHandler : ICommandHandler<MoveCustomerCommand>
{
    public void Handle(MoveCustomerCommand command)
    {
        Console.WriteLine("MoveCustomerCommandHandler");
    }
}

public class TransactionCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
{
    private readonly ICommandHandler<TCommand> _decorated;

    public TransactionCommandHandlerDecorator(ICommandHandler<TCommand> decorated)
    {
        _decorated = decorated;
    }

    public void Handle(TCommand command)
    {
        Console.WriteLine("TransactionCommandHandlerDecorator - before");
        _decorated.Handle(command);
        Console.WriteLine("TransactionCommandHandlerDecorator - after");
    }
}

public class DeadlockRetryCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
{
    private readonly ICommandHandler<TCommand> _decorated;

    public DeadlockRetryCommandHandlerDecorator(ICommandHandler<TCommand> decorated)
    {
        _decorated = decorated;
    }

    public void Handle(TCommand command)
    {
        Console.WriteLine("DeadlockRetryCommandHandlerDecorator - before");
        _decorated.Handle(command);
        Console.WriteLine("DeadlockRetryCommandHandlerDecorator - after");
    }
}

我可以使用以下代码装饰MoveCustomerCommandHandlera :TransactionCommandHandlerDecorator

var builder = new ContainerBuilder();

builder.RegisterAssemblyTypes(typeof(MoveCustomerCommandHandler).Assembly)
    .As(type => type.GetInterfaces()
    .Where(interfaceType => interfaceType.IsClosedTypeOf(typeof(ICommandHandler<>)))
    .Select(interfaceType => new KeyedService("commandHandler", interfaceType)));

builder.RegisterGenericDecorator(
        typeof(TransactionCommandHandlerDecorator<>),
        typeof(ICommandHandler<>),
        fromKey: "commandHandler");

var container = builder.Build();

var commandHandler = container.Resolve<ICommandHandler<MoveCustomerCommand>>();
commandHandler.Handle(new MoveCustomerCommand());

这将输出:

TransactionCommandHandlerDecorator - before  
MoveCustomerCommandHandler 
TransactionCommandHandlerDecorator - after 

我如何也可以用 , 来装饰TransactionCommandHandlerDecorator,DeadlockRetryCommandHandlerDecorator以生成以下输出

DeadlockRetryCommandHandlerDecorator- before
TransactionCommandHandlerDecorator - before  
MoveCustomerCommandHandler 
TransactionCommandHandlerDecorator - after 
DeadlockRetryCommandHandlerDecorator- after
4

2 回答 2

16

@nemesv 已经回答了这个问题,但是我只是想补充一点,您可以添加一些简单的辅助方法,以减少在 Autofac 中连接许多通用装饰器的痛苦:

    private static void RegisterHandlers(
        ContainerBuilder builder, 
        Type handlerType,
        params Type[] decorators)
    {
        RegisterHandlers(builder, handlerType);

        for (int i = 0; i < decorators.Length; i++)
        {
            RegisterGenericDecorator(
                builder,
                decorators[i],
                handlerType,
                i == 0 ? handlerType : decorators[i - 1],
                i != decorators.Length - 1);
        }
    }

    private static void RegisterHandlers(ContainerBuilder builder, Type handlerType)
    {
        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            .As(t => t.GetInterfaces()
                    .Where(v => v.IsClosedTypeOf(handlerType))
                    .Select(v => new KeyedService(handlerType.Name, v)))
            .InstancePerRequest();
    }

    private static void RegisterGenericDecorator(
        ContainerBuilder builder,
        Type decoratorType,
        Type decoratedServiceType,
        Type fromKeyType,
        bool hasKey)
    {
        var result = builder.RegisterGenericDecorator(
           decoratorType,
           decoratedServiceType,
           fromKeyType.Name);

        if (hasKey)
        {
            result.Keyed(decoratorType.Name, decoratedServiceType);
        }
    }

如果您将这些方法粘贴到您正在配置 Autofac 的位置,那么您可以这样做:

    RegisterHandlers(
        builder, 
        typeof(ICommandHandler<>),
        typeof(TransactionCommandHandlerDecorator<>),
        typeof(ValidationCommandHandlerDecorator<>));

它会连接你所有的命令处理程序并按给定的顺序添加装饰器。

于 2014-09-24T14:04:29.297 回答
13

您只需将“TransactionCommandHandlerDecoratored”注册ICommandHandlerKeyed服务并在注册第二个时使用该新密钥DeadlockRetryCommandHandlerDecorator

builder.RegisterGenericDecorator(
        typeof(TransactionCommandHandlerDecorator<>),
        typeof(ICommandHandler<>),
        fromKey: "commandHandler")
        .Keyed("decorated", typeof(ICommandHandler<>));

builder.RegisterGenericDecorator(
        typeof(DeadlockRetryCommandHandlerDecorator<>),
        typeof(ICommandHandler<>),
        fromKey: "decorated");

您将获得以下输出:

DeadlockRetryCommandHandlerDecorator - before
TransactionCommandHandlerDecorator - before
MoveCustomerCommandHandler
TransactionCommandHandlerDecorator - after
DeadlockRetryCommandHandlerDecorator - after
于 2013-07-03T04:59:48.173 回答