看起来您在 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");
}
}
}