Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用SimpleInjector将条件装饰器应用于命令处理程序?_C#_.net_Simple Injector - Fatal编程技术网

C# 如何使用SimpleInjector将条件装饰器应用于命令处理程序?

C# 如何使用SimpleInjector将条件装饰器应用于命令处理程序?,c#,.net,simple-injector,C#,.net,Simple Injector,我有以下命令处理程序接口: public interface ICommandHandler<TCommand> where TCommand : ICommand { void Handle(TCommand command); } 显然,如果任何给定的ICommandHandler都不存在IValidator实例,则此操作将失败。For info程序集是中用于注册泛型类的程序集的集合 如果可能的话,我应该使用什么来注册装饰器/验证器来实现我想要做的事情?我不想改用Sim

我有以下命令处理程序接口:

public interface ICommandHandler<TCommand> where TCommand : ICommand
{
    void Handle(TCommand command);
}
显然,如果任何给定的
ICommandHandler
都不存在
IValidator
实例,则此操作将失败。For info
程序集
是中用于注册泛型类的程序集的集合

如果可能的话,我应该使用什么来注册装饰器/验证器来实现我想要做的事情?我不想改用SimpleInjector


此外,如果可能的话,这是建议的还是违反了可靠的原则,甚至只是一种代码味道?

您可以通过分析容器中的注册并决定是否装饰每个实例来注册条件装饰器,但我认为这不是最好的选择。最简单的解决方案是为那些实际的
IValidator
不存在的实例定义并注册回退
NullValidator

public class NullValidator<TCommand> : IValidator<TCommand> where TCommand : ICommand
{
    public void Validate(TCommand command)
    {
    }
}
公共类NullValidator:IValidator,其中TCommand:ICommand
{
公共无效验证(TCommand命令)
{
}
}
登记为有条件的:

var container=newcontainer();
容器寄存器(typeof(ICommandHandler),程序集);
容器。寄存器(类型(IValidator),组件);
容器注册表条件(
类型(IValidator),
类型(空校验器),
c=>!c.Handled);
container.RegisterDecorator(
类型(ICommandHandler),
类型(ValidationCommandHandlerDecorator));
container.Verify();
我不想改用SimpleInjector

好人

此外,如果可能,这是建议的还是违反了可靠的原则,甚至只是一种代码味道


这正是存在于:-)的注册条件。另一种解决方案是将验证器注册为集合(可选地包装在组合中)。我总是使用这种条件解决方案,很好、快速且干净。:-)
public class CreateFooCommandHandler : ICommandHandler<CreateFooCommand>
public class CreateFooCommandValidator : IValidator<CreateFooCommand>
var container = new Container();

container.Register(typeof(ICommandHandler<>), assemblies);
container.Register(typeof(IValidator<>), assemblies);
container.RegisterDecorator(
    typeof(ICommandHandler<>), 
    typeof(ValidationCommandHandlerDecorator<>));
public class NullValidator<TCommand> : IValidator<TCommand> where TCommand : ICommand
{
    public void Validate(TCommand command)
    {
    }
}
var container = new Container();

container.Register(typeof(ICommandHandler<>), assemblies);
container.Register(typeof(IValidator<>), assemblies);
container.RegisterConditional(
    typeof(IValidator<>), 
    typeof(NullValidator<>), 
    c => !c.Handled);
container.RegisterDecorator(
    typeof(ICommandHandler<>), 
    typeof(ValidationCommandHandlerDecorator<>));

container.Verify();