I was going through different LifetimeManagers available in Unity and was wondering when will we use ExternallyControlledLifetimeManager? Can somebody give me an real life example? The MSDN doc says "A LifetimeManager that holds a weak reference to it's managed instance"..
How can the developer manage the instance and when should he go for such a lifetime manager? Your thoughts on how and when you used it would be great for me and others to understand. Thanks!
Use the ContainerControlledLifetimeManager when you want to create a singleton instance. In the above example, we specified ContainerControlledLifetimeManager in the RegisterType() method. So, Unity container will create a single instance of the BMW class and inject it in all the instances of Driver .
A special lifetime manager which works like ContainerControlledLifetimeManager, except that in the presence of child containers, each child gets it's own instance of the object, instead of sharing one in the common parent.
It could be useful if you're going to register an object created by other frameworks or library and you haven't the control of its life, for example a singleton provided by a third-party library.
Because the container keeps only a weak reference, the GC will delete that singleton if there aren't any strong reference. IMO, you should use it only if you can't manage the life time of the instance.
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