I have a type called TypeA.
TypeA implements 2 interfaces: IOne and ITwo
public class TypeA : IOne, ITwo
{
public TypeA(IThree ithree)
{
...
}
}
I want to configure StructureMap so that there is only ever one instance of TypeA per thread. I want any resolution of EITHER IOne or ITwo to return the same instance of TypeA (one per thread).
I have tried the following config:
ObjectFactory.Configure(x =>
{
var thread = Lifecycles.GetLifecycle(InstanceScope.ThreadLocal);
x.For<TypeA>().LifecycleIs(thread).Use<TypeA>();
x.For<IOne>().LifecycleIs(thread).Use<TypeA>();
x.For<ITwo>().LifecycleIs(thread).Use<TypeA>();
x.For<IThree>().LifecycleIs(thread).Use<TypeB>();
});
and then resolve like this
var test = ObjectFactory.GetInstance<IOne>();
var test2 = ObjectFactory.GetInstance<ITwo>();
But these two calls resolve to different objects.
Firstly, is what I'm trying to achieve possible using StructureMap? If so then how do I do it?