Table of Contents

Class TypeLifetime

Namespace
Unity
Assembly
Unity.Abstractions.dll
public static class TypeLifetime
Inheritance
TypeLifetime
Inherited Members

Properties

ContainerControlled

Unity returns the same instance each time the Resolve(...) method is called or when the dependency mechanism injects the instance into other classes.

public static ITypeLifetimeManager ContainerControlled { get; }

Property Value

ITypeLifetimeManager

A new instance of a ContainerControlledLifetimeManager object.

Remarks

Per Container lifetime allows a registration of an existing or resolved object as a scoped singleton in the container it was created or registered. In other words this instance is unique within the container it war registered with. Child or parent containers could have their own instances registered for the same contract.

External

This lifetime keeps a weak reference to object it holds.

public static ITypeLifetimeManager External { get; }

Property Value

ITypeLifetimeManager

A new instance of a WeakReferenceLifetimeManager lifetime manager.

Remarks

When no object is associated with the manager container creates and returns a new object. It gets and holds a weak reference to the created object. As long as the object still exists and has not been garbage collected the container will return the object when requested.

If the object went out of scope and has been garbage collected the container will create and return a new instance.

This lifetime manager does not dispose an object when container is disposed

Hierarchical

Unity returns a unique value for each child container.

public static ITypeLifetimeManager Hierarchical { get; }

Property Value

ITypeLifetimeManager

A new instance of a HierarchicalLifetimeManager object.

Remarks

The Unity container allows creating hierarchies of child containers. This lifetime creates local singleton for each level of the hierarchy. So, when you resolve a type and this container does not have an instance of that type, the container will create new instance. Next type the type is resolved the same instance will be returned.

If a child container is created and requested to resolve the type, the child container will create a new instance and store it for subsequent resolutions. Next time the child container requested to resolve the type, it will return stored instance.

If you have multiple children, each will resolve its own instance.

PerContainer

Unity returns the same instance each time the Resolve(...) method is called or when the dependency mechanism injects the instance into other classes.

public static ITypeLifetimeManager PerContainer { get; }

Property Value

ITypeLifetimeManager

A new instance of a ContainerControlledLifetimeManager object.

Remarks

Per Container lifetime allows a registration of an existing or resolved object as a scoped singleton in the container it was created or registered. In other words this instance is unique within the container it war registered with. Child or parent containers could have their own instances registered for the same contract.

PerContainerTransient

This lifetime is similar to TransientLifetimeManager with exception how the container holds references to created objects.

public static ITypeLifetimeManager PerContainerTransient { get; }

Property Value

ITypeLifetimeManager

A new instance of a ContainerControlledTransientManager object.

Remarks

On each call to the Resolve{T}() method a container will create a new objects. If the objects implements IDisposable, the container will hold a reference to the interface and will dispose the object when the container goes out of scope.

This lifetime is particularly useful in session based designs with child containers associated with the session

PerResolve

This lifetime keeps a reference to an instance only for the duration of one resolution call

public static ITypeLifetimeManager PerResolve { get; }

Property Value

ITypeLifetimeManager

A new instance of a PerResolveLifetimeManager object.

Examples

Consider this scenario:

class a {}

class b 
{
    b(a arg1)
    {...}
}

class c
{
    c(a arg1, b arg2)
    {...}
}

When you resolve type c, it depends on type b and type a. Type b, in turn, also depends on type a, and both types, c and b, require a to be the same instance.

If type `a` is a singleton, the logic is easy. But if you require each instance of `c` to have a unique `a`, you could use per resolve lifetime. The instance of `a` will act as a singleton only during that one resolution. Next call to resolve the dependent type will create a new object.

In the case of recursion, the singleton behavior is still applies and prevents circular dependency

Remarks

This type of lifetime is useful when you need to pass the same instance of the dependency to a different nodes of the resolution graph.

PerThread

Per thread lifetime means a new instance of the registered Type will be created once per each thread. In other words, if a Resolve{T}() method is called on a thread the first time, it will return a new object. Each subsequent call to Resolve{T}(), or when the dependency mechanism injects instances of the type into other classes on the same thread, the container will return the same object.

public static ITypeLifetimeManager PerThread { get; }

Property Value

ITypeLifetimeManager

A new instance of a PerThreadLifetimeManager object.

Scoped

Unity returns a unique value for each scope.

public static ITypeLifetimeManager Scoped { get; }

Property Value

ITypeLifetimeManager

A new instance of a HierarchicalLifetimeManager object.

Remarks

The Unity container allows creating hierarchies of child containers. This lifetime creates local singleton for each level of the hierarchy. So, when you resolve a type and this container does not have an instance of that type, the container will create new instance. Next type the type is resolved the same instance will be returned.

If a child container is created and requested to resolve the type, the child container will create a new instance and store it for subsequent resolutions. Next time the child container requested to resolve the type, it will return stored instance.

If you have multiple children, each will resolve its own instance.

Singleton

Singleton lifetime creates globally unique singleton. Any Unity container tree (parent and all the children) is guaranteed to have only one global singleton for the registered type.

public static ITypeLifetimeManager Singleton { get; }

Property Value

ITypeLifetimeManager

A new instance of a SingletonLifetimeManager object.

Remarks

Registering a type with singleton lifetime always places the registration at the root of the container tree and makes it globally available for all the children of that container. It does not matter if registration takes places at the root of child container the destination is always the root node.

Repeating the registration on any of the child nodes with singleton lifetime will always override the root registration.

Transient

This lifetime creates and returns a new instance of the requested type for each call to the Resolve(...) method.

public static ITypeLifetimeManager Transient { get; }

Property Value

ITypeLifetimeManager

An instance of a TransientLifetimeManager object.

Remarks

Transient lifetime is a default lifetime of the Unity container. As the name implies it lasts very short period of time, actually, no time at all. In the Unity container terms, having transient lifetime is the same as having no lifetime manager at all.