I have two interfaces both implemented by class Apple
interface a { }
interface b { }
class Apple: a,b
{
}
Now if I register Apple against both the interfaces but only one is with ContainerControlledLifeTimeManager then resolving the other interface also returns the same instance. Why is this the way it is and anyway to work around it?
IUnityContainer container = new UnityContainer();
container.RegisterType<a, Apple>(new ContainerControlledLifetimeManager());
container.RegisterType<b, Apple>();
var a = container.Resolve<a>();
Console.WriteLine(container.Resolve<b>().Equals(a).ToString());
Injection configuration is done against the implementation class, not the interface. This includes lifetime. So when you set the container controlled lifetime for Apple, it'll always be the same instance regardless of which interface you used to get it.
If you truly need different interfaces on the same type with different lifetimes, at this point in time the only option would be named registrations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With