3

我有一个基于命令修改记录的应用程序。我使用 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; }
    }
}
4

1 回答 1

2

你要

scanner.ConnectImplementationsToTypesClosing(typeof (IValidator<>));

代替

scanner.AddAllTypesOf(typeof(IValidator<>));

请参阅结构图:泛型类型

于 2016-04-14T20:49:25.103 回答