StructureMap GetAllInstances 在多个预期时返回一个实例
StructureMap GetAllInstances returning one instance when several expected
我有一个应用程序可以根据命令修改记录。我正在使用 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; }
}
}
你想要
scanner.ConnectImplementationsToTypesClosing(typeof (IValidator<>));
而不是
scanner.AddAllTypesOf(typeof(IValidator<>));
我有一个应用程序可以根据命令修改记录。我正在使用 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; }
}
}
你想要
scanner.ConnectImplementationsToTypesClosing(typeof (IValidator<>));
而不是
scanner.AddAllTypesOf(typeof(IValidator<>));