Autofac-注册多个装饰器

本文关键字:注册 Autofac- | 更新日期: 2023-09-27 18:20:44

给定以下条件:

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

我可以使用以下代码用TransactionCommandHandlerDecorator装饰MoveCustomerCommandHandler

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 

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

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

Autofac-注册多个装饰器

@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<>));

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

您只需要将"TransactionCommandHandlerRecorded"ICommandHandler注册为Keyed服务,并在注册第二个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