コマンドに基づいてレコードを変更するアプリケーションがあります。私はコンテナとしてStructureMapを使用していますが、期待通りに動作していません。Structure Map GetAllInstances複数インスタンスがある場合に1つのインスタンスを返す
アプリケーションには、いくつかのビジネスエンティティを更新するコマンドがいくつかあります。これらのコマンドを検証するために、いくつかのFluentValidationバリデータがあります。いくつかのコマンドは同じフィールドを共有しています。私は私がMoveCommand、var validators = container.GetAllInstances<IValidator<MoveCommand>>();
のためのバリデータを取得しようとすると、私は2つを得ることを期待するので、
var container = new StructureMap.Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(Program)); //Everything is in same assembly as Program
scanner.AddAllTypesOf(typeof(IValidator<>));
});
});
のようなのStructureMapコンテナを設定していたもののインターフェイス上で
//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();
}
}
をいくつかのバリデータを実装しましたバリデータ、GameConfigurationValidator、およびMoveActionValidatorを返しますが、GetAllInstancesはGameConfigurationValidatorだけを返します。最初にMoveActionValidatorを定義すると、GameConfigurationValidatorは返されずに返されます。
GetAllInstancesを使用する場合、つまりGameConfigurationValidatorとMoveActionValidatorの両方を使用する場合、MoveCommandのすべてのバリデータを取得するためには、何が必要ですか?
全例(の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; }
}
}
感謝を参照したいです問題を解決しました。あなたは、StructureMapの登録をデバッグする良い方法を知っていますか?私は現在、IContainerに依存しています。登録されたもののレポートを取得するにはどうしたらよいですか? 'ConnectImplementationsToTypesClosing'または' AddAllTypesOf'を使用しても同じレポートが表示されます –