如何在中注册条件装饰器SimpleInjector
?这是我的定义:
public interface ICommand { }
public interface ICleanableCommand : ICommand {
void Clean();
}
public interface ICommandHandler<in TCommand>
where TCommand : ICommand {
void Handle(TCommand command);
}
public class CleanableCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand>
where TCommand : ICleanableCommand {
private readonly ICommandHandler<TCommand> _handler;
public CleanableCommandHandlerDecorator(
ICommandHandler<TCommand> handler) {
_handler = handler;
}
void ICommandHandler<TCommand>.Handle(TCommand command) {
command.Clean();
_handler.Handle(command);
}
}
我正在尝试:
container.RegisterManyForOpenGeneric(
typeof(ICommandHandler<>),
AppDomain.CurrentDomain.GetAssemblies()
);
container.RegisterDecorator(
typeof(ICommandHandler<>),
typeof(CleanableCommandHandlerDecorator<>)
// ,context => context.ImplementationType ???
// I want to register this decorator for commandhandlers
// which their command implements ICleanableCommand
);