没有具体类型的 StructureMap 构造函数参数或实例名称的参数
StructureMap Constructor arguments without Concrete type or args by instance name
我确实阅读了其他答案,但 none 完全适合,所以请多多包涵
后台伪代码(无视SM版本)
public interface IFilter() {}
public class A: IFilter {}
public class B: IFilter {}
public class C: IFilter {}
public class D: IFilter {}
public interface IProcessor()
{
}
public Processor(List<IFilter> filters) //List, IEnumerbale, Array doesn't matter
{
}
ObjectFactory.Initialize(x =>
{
x.Scan(scanner =>
{
...
scanner.AddAllTypesOf<IFilter>().NameBy(type => type.Name);
});
});
如何配置SM默认使用实例"B"和"C"?我可以这样配置,但我不想:
ObjectFactory.Configure(x =>
{
var filters = new List<IFilter>();
filters.Add(ObjectFactory.GetNamedInstance<IFilter>("B")); //I don't like create instance before concrete use
filters.Add(ObjectFactory.GetNamedInstance<IFilter>("C"));
x.For<IProcessor>().Use(new Processor(filters)); //this works, but I don't like to use concrete type, what if not accessible?
});
有没有办法这样配置(伪)
x.For<IProcessor>().Ctor<List<IFilter>>().ByInstanceName(new[] {"B","C"})
//or assuming only one concrete type is defined for IProcessor
x.For<IProcessor>().Named("Alpha").Ctor<List<IFilter>>().ByInstanceName(new[] {"B","C"});
x.For<IProcessor>().Named("Beta").Ctor<List<IFilter>>().ByInstanceName(new[] {"A","D"});
或者通常在代码参考中不使用具体类型的任何其他方式?
我知道 IProcessor 可能有多个实例,但是 SM 真的没有办法匹配 Ctor 签名并在匹配时使用命名实例连接任何 IProcessor 实例吗?
如果必须使用具体类型,那么至少仍然使用 ctor 的实例名称而不是创建列表
x.For<IProcessor>().Use(new Processor()).WithCtorInstanceByName("B","C");
您要查找的语法是:
For<IProcessor>().Use<Processor>().EnumerableOf<IFilter>().Contains(_ => {
_.TheInstanceNamed("A");
_.TheInstanceNamed("A");
}
我确实阅读了其他答案,但 none 完全适合,所以请多多包涵 后台伪代码(无视SM版本)
public interface IFilter() {}
public class A: IFilter {}
public class B: IFilter {}
public class C: IFilter {}
public class D: IFilter {}
public interface IProcessor()
{
}
public Processor(List<IFilter> filters) //List, IEnumerbale, Array doesn't matter
{
}
ObjectFactory.Initialize(x =>
{
x.Scan(scanner =>
{
...
scanner.AddAllTypesOf<IFilter>().NameBy(type => type.Name);
});
});
如何配置SM默认使用实例"B"和"C"?我可以这样配置,但我不想:
ObjectFactory.Configure(x =>
{
var filters = new List<IFilter>();
filters.Add(ObjectFactory.GetNamedInstance<IFilter>("B")); //I don't like create instance before concrete use
filters.Add(ObjectFactory.GetNamedInstance<IFilter>("C"));
x.For<IProcessor>().Use(new Processor(filters)); //this works, but I don't like to use concrete type, what if not accessible?
});
有没有办法这样配置(伪)
x.For<IProcessor>().Ctor<List<IFilter>>().ByInstanceName(new[] {"B","C"})
//or assuming only one concrete type is defined for IProcessor
x.For<IProcessor>().Named("Alpha").Ctor<List<IFilter>>().ByInstanceName(new[] {"B","C"});
x.For<IProcessor>().Named("Beta").Ctor<List<IFilter>>().ByInstanceName(new[] {"A","D"});
或者通常在代码参考中不使用具体类型的任何其他方式? 我知道 IProcessor 可能有多个实例,但是 SM 真的没有办法匹配 Ctor 签名并在匹配时使用命名实例连接任何 IProcessor 实例吗?
如果必须使用具体类型,那么至少仍然使用 ctor 的实例名称而不是创建列表
x.For<IProcessor>().Use(new Processor()).WithCtorInstanceByName("B","C");
您要查找的语法是:
For<IProcessor>().Use<Processor>().EnumerableOf<IFilter>().Contains(_ => {
_.TheInstanceNamed("A");
_.TheInstanceNamed("A");
}