Skip to main content

Lifetime Overview

  • Singleton : Single instance for all containers.
    • Same type cannot be registered in the same container.
  • Transient : Instance per resolving.
  • Scoped : Instance per LifetimeScope.
    • If LifetimeScope is single, similar to Singleton.
    • If you create a LifetimeScope child, the instance will be different for each child.
    • When LifetimeScope is destroyed, its references are released and it calls all the registered IDisposable.

Lifetime with Parent/Child relationship#

LifetimeScope can build parent-child relationship. it has following behaviours:

  • If registered object is not found, LifetimeScope will look for a parent LifetimeScope.
  • For Lifetime.Singleton
    • Basically, always returns the same instance.
    • If parent and child have the same type, it returns the instance with the closest scope.
    • When a LifetimeScope is destroyed, objects with IDisposable implemented are called Dispose().
  • For LifeTime.Transient
    • Instance creating for each resolving.
    • If parent and child have the same registration, the child will create its own instance.
  • For Lifetime.Scoped
    • Instance will be different for each child.
      • If same child, returns same instance.
    • If parent and child have the same registration, the child will create its own instance.
    • When a LifetimeScope is destroyed, objects with IDisposable implemented are called Dispose().
caution

If the scene is alive and only LifetimeScope is destroyed, MonoBehaviours registered as Lifetime.Scoped are not automatically destroyed. If you want to destroy with LifetimeScope, make it a child transform of LifetimeScope or consider implement IDisposable.