StructureMap没有Concrete类型的构造函数参数或按实例名称

时间:2016-02-29 22:23:32

标签: c# dependency-injection structuremap constructor-injection

我确实读过其他答案,但没有一个完全合适,所以请耐心等待 背景伪代码(无视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以使用实例&#34; B&#34;和&#34; C&#34;默认?我可以像这样配置,但我不想:

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");

1 个答案:

答案 0 :(得分:4)

您正在寻找的语法是:

UPDATE loginlogout
SET [Logout Time] = CAST(CONVERT(VARCHAR(10), GETDATE(), 110) AS DATETIME)
WHERE [Logout Time] = '0'