У меня есть общий обработчик команд со многими конкретными реализациями.
public interface ICommandHandler<TCommand>
У меня есть декоратор с ограничением типа, который я хочу использовать для нескольких, но не для всех, реализаций ICommandHandler.
public class AddNoteCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand> where TCommand : INoteCommand
{
private readonly ICommandHandler<TCommand> decorated;
public AddNoteCommandHandlerDecorator(ICommandHandler<TCommand> decorated)
{
this.decorated = decorated;
}
public void Handle(TCommand command)
...
Я пытаюсь зарегистрироваться в AutoFac следующим образом:
public static void Register(HttpConfiguration config)
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
builder.RegisterAssemblyTypes(assemblies)
.As(t => t.GetInterfaces()
.Where(a => a.IsClosedTypeOf(typeof(ICommandHandler<>)))
.Select(a => new KeyedService("commandHandler", a)));
builder.RegisterGenericDecorator(
typeof(AddNoteCommandHandlerDecorator<>),
typeof(ICommandHandler<>),
fromKey: "commandHandler");
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
config.DependencyResolver = new AutoFacContainer(new AutofacDependencyResolver(container));
}
Однако когда для команды, реализующей INoteCommand, вызывается конкретный обработчик команд, декоратор не работает.
Я неправильно настроил регистрацию в AutoFac? Или я что-то еще пропустил?