Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use ExternallyControlledLifetimeManager?

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!

like image 674
Abdel Raoof Olakara Avatar asked Feb 23 '11 08:02

Abdel Raoof Olakara


People also ask

When should I use ContainerControlledLifetimeManager?

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 .

What is Hierarchicallifetimemanager?

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.


1 Answers

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.

like image 107
onof Avatar answered Oct 05 '22 11:10

onof