Skip to content

Unity Lifetime Managers

Eugene Sadovoi edited this page Apr 3, 2018 · 6 revisions

Unity Built-In Lifetime Managers

Unity uses lifetime managers to control lifetime of objects it creates. It includes several lifetime managers that you can use directly in your code, but you can also create your own lifetime managers to implement specific lifetime scenarios.

TransientLifetimeManager

For this lifetime manager Unity creates and returns a new instance of the requested type for each call to the Resolve method. This lifetime manager is used by default for all types registered using the RegisterType method where no specific manager has been provided. When registering transient types it is not necessary to pass instance of TransientLifetimeManager to the registration. Example:

RegisterType<Service>();
RegisterType<IService, Service>();

ContainerControlledLifetimeManager

This manager registers an existing or resolved object as a singleton instance in the container the object of instance are registered. For this lifetime manager Unity returns the same instance of the registered type or object each time you call the Resolve method or when the dependency mechanism injects instances into other classes. This lifetime manager effectively implements a singleton behavior for objects. Unity uses this lifetime manager by default for the RegisterInstance method if different lifetime manager was not specified.

If you want singleton behavior for an object that Unity will create when you specify a type mapping in configuration or when you use the RegisterType method, you must explicitly specify this lifetime manager.

If you registered a type mapping using configuration or using the RegisterType method, Unity creates a new instance of the registered type during the first call to the Resolve method or when the dependency mechanism injects instances into other classes. Subsequent requests return the same instance.

If you registered an existing instance of an object using the RegisterInstance method, the container returns the same instance for all calls to Resolve or when the dependency mechanism injects instances into other classes, provided that one of the following is true:

  • You have specified a container-controlled lifetime manager
  • You have used the default lifetime manager while registering instance

Singleton is visible in the container it was registered and all children of that container. The singleton could be overridden in child containers.

RegisterType<Service>(new ContainerControlledLifetimeManager());           <-- Service is a singleton
RegisterType<IService, Service>(new ContainerControlledLifetimeManager()); <-- IService is a singleton
RegisterInstance(new OtherService());                                      <-- OtherService is a singleton

HierarchicalLifetimeManager.

For this lifetime manager, as for the ContainerControlledLifetimeManager, Unity returns the same instance of the registered type or object each time you call the Resolve method or when the dependency mechanism injects instances into other classes. The distinction is that when there are child containers, each child resolves its own instance of the object and does not share one with the parent. When resolving in the parent, the behavior is like a container controlled lifetime; when resolving the parent and the child you have different instances with each acting as a container-controlled lifetime. If you have multiple children, each will resolve its own instance.

RegisterType<Service>(new HierarchicalLifetimeManager());                <-- Service is a per child singleton
RegisterType<IService, Service>(new HierarchicalLifetimeManager());      <-- IService is a per child singleton
RegisterInstance(new OtherService(), new HierarchicalLifetimeManager()); <-- OtherService is a per child singleton
Clone this wiki locally