Show / Hide Table of Contents

Class InstanceLifetime

Inheritance
Object
InstanceLifetime
Inherited Members
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Object.ToString()
Namespace: Unity
Assembly: Unity.Abstractions.dll
Syntax
public static class InstanceLifetime

Properties

| Improve this Doc View Source

External

This lifetime stores a weak reference to an instance and returns an instance while it is available.

Declaration
public static IInstanceLifetimeManager External { get; }
Property Value
Type Description
IInstanceLifetimeManager

A new instance of a ExternallyControlledLifetimeManager lifetime manager.

Remarks

Container gets a hold of a weak reference to the object and 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 return NoValue.

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

| Improve this Doc View Source

PerContainer

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

Declaration
public static IInstanceLifetimeManager PerContainer { get; }
Property Value
Type Description
IInstanceLifetimeManager

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.

| Improve this Doc View Source

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.

Declaration
public static IInstanceLifetimeManager Singleton { get; }
Property Value
Type Description
IInstanceLifetimeManager

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.

  • Improve this Doc
  • View Source
In This Article
Back to top Copyright © 2020 .NET Foundation and Contributors. All Rights Reserved