我有一个根据命令修改记录的应用程序。我使用StructureMap作为我的容器,它的行为并不像我期望的那样。
该应用程序有几个命令来更新某些业务实体。我有许多FluentValidation验证器来验证这些命令。一些命令共享相同的字段,我已将其拉入接口。我在这些接口上实现了一些验证器
//example command
public class MoveCommand
: IMoveAction, IConfigurationAction
{
public string Section { get; set; }
public string Bank { get; set; }
public string Slot { get; set; }
public List<Configuration> Configurations { get; set; }
}
//Interfaces
public interface IConfigurationAction
{
List<Configuration> Configurations { get; set; }
}
public interface IMoveAction
{
string Section { get; set; }
string Bank { get; set; }
string Slot { get; set; }
}
//Validators
public class GameConfigurationValidator
: AbstractValidator<IConfigurationAction>
{
public GameConfigurationValidator()
{
RuleFor(action => action.Configurations).NotEmpty();
}
}
public class MoveActionValidator
: AbstractValidator<IMoveAction>
{
public MoveActionValidator()
{
RuleFor(action => action.Section).NotEmpty();
RuleFor(action => action.Bank).NotEmpty();
RuleFor(action => action.Slot).NotEmpty();
}
}
我像这样配置StructureMap容器
var container = new StructureMap.Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(Program)); //Everything is in same assembly as Program
scanner.AddAllTypesOf(typeof(IValidator<>));
});
});
当我尝试检索MoveCommand的验证器var validators = container.GetAllInstances<IValidator<MoveCommand>>();
时,我希望得到两个验证器,GameConfigurationValidator和MoveActionValidator,但GetAllInstances只返回GameConfigurationValidator。如果我首先定义MoveActionValidator,它将被返回,但不会返回GameConfigurationValidator。
在使用GetAllInstances时,我需要做什么才能获得MoveCommand的所有验证器,即GameConfigurationValidator和MoveActionValidator?
完整示例(需要StructureMap和ServiceStack nuget包):
using System.Collections.Generic;
using ServiceStack.FluentValidation;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var container = new StructureMap.Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(Program));
scanner.AddAllTypesOf(typeof(IValidator<>));
});
});
var stuff = container.WhatDoIHave();
//IValidator<IConfigurationAction> ServiceStack.FluentValidation Transient ConsoleApplication5.GameConfigurationValidator (Default)
//IValidator<IMoveAction> ServiceStack.FluentValidation Transient ConsoleApplication5.MoveActionValidator (Default)
var validators = container.GetAllInstances<IValidator<MoveCommand>>();
}
}
public class GameConfigurationValidator
: AbstractValidator<IConfigurationAction>
{
public GameConfigurationValidator()
{
RuleFor(action => action.Configurations).NotEmpty();
}
}
public class MoveActionValidator
: AbstractValidator<IMoveAction>
{
public MoveActionValidator()
{
RuleFor(action => action.Section).NotEmpty();
RuleFor(action => action.Bank).NotEmpty();
RuleFor(action => action.Slot).NotEmpty();
}
}
public interface IConfigurationAction
{
List<Configuration> Configurations { get; set; }
}
public interface IMoveAction
{
string Section { get; set; }
string Bank { get; set; }
string Slot { get; set; }
}
public class Configuration
{
public string Theme { get; set; }
public decimal Denomination { get; set; }
public string Par { get; set; }
}
public class MoveCommand
: IMoveAction, IConfigurationAction
{
public string Section { get; set; }
public string Bank { get; set; }
public string Slot { get; set; }
public List<Configuration> Configurations { get; set; }
}
}
答案 0 :(得分:2)
你想要
scanner.ConnectImplementationsToTypesClosing(typeof (IValidator<>));
而不是
scanner.AddAllTypesOf(typeof(IValidator<>));