看起来在 StructureMap 中,每种插件类型只能有一个生命周期策略。 有什么办法吗? 这个简单的控制台应用程序演示了"最后一个"生命周期的"获胜"。
namespace StructureMapLifecycle
{
public class Program
{
public static void Main(string[] args)
{
StructureMap.ObjectFactory.Initialize(
x =>
{
x.For<ISomething>().Add<SomethingA>().Named("A");
x.For<ISomething>().Singleton().Add<SomethingB>().Named("B");
x.For<ISomething>().Add<SomethingC>().Named("C");
x.For<ISomething>().HybridHttpOrThreadLocalScoped().Use<SomethingC>();
});
Console.Write(StructureMap.ObjectFactory.WhatDoIHave());
Console.ReadLine();
}
}
public interface ISomething
{
void DoSomething();
}
public class SomethingA : ISomething
{
public void DoSomething()
{
Console.WriteLine("Do something A");
}
}
public class SomethingB : ISomething
{
public void DoSomething()
{
Console.WriteLine("Do something B");
}
}
public class SomethingC : ISomething
{
public void DoSomething()
{
Console.WriteLine("Do something C");
}
}
}
我的一位同事发现Jeremy Miller指出这是StructureMap的长期限制。 http://jeremydmiller.com/2012/01/11/kicking-off-structuremap-3/
这在StructureMap 3.0中得到了修复。 现在,您只需更改每个实例的生命周期即可。