It looks like you can only have one lifecycle policy per plugin type in StructureMap. Is there any way around this? This simple console app demonstrates that whatever lifecycle is “last” “wins.”
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");
}
}
}
A coworker of mine found that Jeremy Miller states this is a long time limitation of StructureMap. http://jeremydmiller.com/2012/01/11/kicking-off-structuremap-3/