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

次のコードを使用して をMoveCustomerCommandHandler装飾できます。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 に答える