Имам общ манипулатор на команди с много конкретни реализации.
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? Или пропуснах нещо друго?