私は現在、StructureMap の上にある IoC コンテナーの抽象化に取り組んでいます。アイデアは、他の IoC コンテナーでも機能する可能性があるということです。
public interface IRegister
{
IRegister RegisterType(Type serviceType, Type implementationType, params Argument[] arguments);
}
public abstract class ContainerBase : IRegister
{
public abstract IRegister RegisterType(Type serviceType, Type implementationType, params Argument[] arguments);
}
public class StructureMapContainer : ContainerBase
{
public StructureMapContainer(IContainer container)
{
Container = container;
}
public IContainer Container { get; private set; }
public override IRegister RegisterType(Type serviceType, Type implementationType, params Argument[] arguments)
{
// StructureMap specific code
Container.Configure(x =>
{
var instance = x.For(serviceType).Use(implementationType);
arguments.ForEach(a => instance.CtorDependency<string>(a.Name).Is(a.Value));
});
return this;
}
}
public class Argument
{
public Argument(string name, string value)
{
Name = name;
Value = value;
}
public string Name { get; private set; }
public string Value { get; private set; }
}
次のようにして、このコードを実行できます。
//IContainer is a StructureMap type
IContainer container = new Container();
StructureMapContainer sm = new StructureMapContainer(container);
sm.RegisterType(typeof(IRepository), typeof(Repository));
コンストラクターの引数を渡そうとすると問題が発生します。StructureMap を使用すると、必要な数の CtorDependency 呼び出しを流暢に連鎖させることができますが、コンストラクター パラメーターの名前、値、および型が必要です。だから私はこのようなことができます:
sm.RegisterType(typeof(IRepository), typeof(Repository), new Argument("connectionString", "myConnection"), new Argument("timeOut", "360"));
CtorDependency は現在文字列型であり、両方の引数も文字列であるため、このコードは機能します。
instance.CtorDependency<string>(a.Name).Is(a.Value)
このメソッドに任意の型の複数のコンストラクター引数を渡すにはどうすればよいですか? 出来ますか?どんな助けでも大歓迎です。