Autofac-注册多个装饰器 [英] Autofac - Register multiple decorators
问题描述
给出以下内容:
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
:
I can decorate the MoveCustomerCommandHandler
with a TransactionCommandHandlerDecorator
using the following code:
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
,以生成以下输出
How can I also decorate the TransactionCommandHandlerDecorator
with the DeadlockRetryCommandHandlerDecorator
, to generate the following output
DeadlockRetryCommandHandlerDecorator- before
TransactionCommandHandlerDecorator - before
MoveCustomerCommandHandler
TransactionCommandHandlerDecorator - after
DeadlockRetryCommandHandlerDecorator- after
推荐答案
您只需要将"TransactionCommandHandlerDecoratored" ICommandHandler
注册为Keyed
服务,并在注册第二个DeadlockRetryCommandHandlerDecorator
时使用该新密钥:>
You just need to register your "TransactionCommandHandlerDecoratored" ICommandHandler
as a Keyed
service and use that new key when registering your second 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
这篇关于Autofac-注册多个装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!