C# 如何使用structuremap asp.net mvc注册可选装饰器或具有可选参数的装饰器?

C# 如何使用structuremap asp.net mvc注册可选装饰器或具有可选参数的装饰器?,c#,asp.net-mvc,decorator,cqrs,structuremap3,C#,Asp.net Mvc,Decorator,Cqrs,Structuremap3,我已经在我的应用程序中实现了一种CQRS方法,它深受这篇精彩文章的影响:。我的命令和处理程序代码的设置与本文相同,这部分工作正常。当我试图实现一个decorator类来处理命令的验证时,我的问题就出现了。简单的命令处理界面如下所示: public interface ICommand { } public interface ICommandHandler<TCommand> { void Handle(TCommand command); } 所有这些都连接到结构图注册

我已经在我的应用程序中实现了一种CQRS方法,它深受这篇精彩文章的影响:。我的命令和处理程序代码的设置与本文相同,这部分工作正常。当我试图实现一个decorator类来处理命令的验证时,我的问题就出现了。简单的命令处理界面如下所示:

public interface ICommand
{
}

public interface ICommandHandler<TCommand>
{
    void Handle(TCommand command);
}
所有这些都连接到结构图注册表中的IoC容器:

public class CommandRegistry : Registry
{
    public CommandRegistry()
    {
        Scan(s =>
        {
            s.AssemblyContainingType<CommandBase>();
            s.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
            s.ConnectImplementationsToTypesClosing(typeof(ICommandValidator<>));
            s.WithDefaultConventions();

            For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(ValidationCommandHandlerDecorator<>));
        });
    }
}
For(typeof(ICommandValidator<>)).Use(typeof(DefaultCommandValidator<>));
公共类命令注册表:注册表
{
公共命令注册表()
{
扫描(s=>
{
s、 AssemblyContainingType();
s、 连接实现到typesClosing(typeof(ICommandHandler));
s、 连接实现到类型关闭(类型化(ICommandValidator));
s、 使用默认约定();
对于(typeof(ICommandHandler)).decoreallwith(typeof(validationcommandlerdecorator));
});
}
}
现在,由于我为每个ICommandHandler注册了decorator,如果我有一个不需要验证器也不定义验证器的命令,无法找到
validationCommandValidator
类的
ICommandValidator\u commandValidator
私有字段,因为它当然不存在,并且将始终抛出以下结构映射错误:

“未注册任何默认实例,因此无法自动 已为类型“ICommandValidator”确定存在 没有为ICommandValidator指定配置”

在结构映射中是否有一种方法可以定义如何构造
validationCommanderDecorator
,以便在不存在验证程序时使用某种类型的默认验证程序,而无需依赖类中的容器或创建
IValidateableCommandHandler
接口来处理带验证器的命令


谢谢。

如果以后有人遇到这种情况,我提出的解决方案是添加一个DefaultCommandValidator类作为空对象模式类:

public class DefaultCommandValidator<TCommand> : ICommandValidator<TCommand> where TCommand : CommandBase
{
    public IEnumerable<string> Validate(TCommand command)
    {
        return Enumerable.Empty<string>();
    }
}
公共类DefaultCommandValidator:ICommandValidator,其中TCommand:CommandBase
{
公共IEnumerable验证(TCommand命令)
{
返回可枚举的.Empty();
}
}
然后将此行添加到结构图注册表:

public class CommandRegistry : Registry
{
    public CommandRegistry()
    {
        Scan(s =>
        {
            s.AssemblyContainingType<CommandBase>();
            s.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
            s.ConnectImplementationsToTypesClosing(typeof(ICommandValidator<>));
            s.WithDefaultConventions();

            For(typeof(ICommandHandler<>)).DecorateAllWith(typeof(ValidationCommandHandlerDecorator<>));
        });
    }
}
For(typeof(ICommandValidator<>)).Use(typeof(DefaultCommandValidator<>));
For(typeof(ICommandValidator))。使用(typeof(DefaultCommandValidator));
我不知道这个结构映射语法只有在找不到
ICommandValidator
的具体实现时才会使用默认实例。现在,如果我没有验证器,我只是不添加验证器,
DefaultCommandValidator
实例用于返回空的/成功的验证

For(typeof(ICommandValidator<>)).Use(typeof(DefaultCommandValidator<>));