class Di::Provider::Instance(T)

Overview

Generic provider that stores a typed factory and optional singleton cache.

Singleton (default): the factory runs once on first resolve, result is cached. Transient: the factory runs on every resolve, nothing is cached.

Defined in:

di/provider.cr

Constructors

Instance Method Summary

Instance methods inherited from class Di::Provider::Base

check_health : Bool | Nil check_health, key=(key : String) key=, reset! : Nil reset!, shutdown_instance : Nil shutdown_instance, transient? : Bool transient?

Constructor Detail

def self.new(factory : -> T, transient : Bool = false) #

[View source]

Instance Method Detail

def check_health : Bool | Nil #

Call .healthy? on the cached instance if it responds to it. Returns false if the health probe raises an exception.


[View source]
def instance : T | Nil #

Returns the cached singleton instance, or nil if not yet resolved or transient.


[View source]
def key : String #

Registry key used for cycle detection (set on registration).


[View source]
def key=(key : String) #

Registry key used for cycle detection (set on registration).


[View source]
def reset! : Nil #

[View source]
def resolve_typed : T #

Type-safe resolve with circular dependency guard.

The fast-path reads @instance without holding @mutex. This is safe because:

  • @instance transitions nil → T exactly once under @mutex in resolve_singleton.
  • A stale nil read just falls through to the mutex-protected path.
  • @instance is only set to nil again via reset!, which requires no active scopes.

[View source]
def shutdown_instance : Nil #

Call .shutdown on the cached instance if it responds to it.


[View source]
def transient? : Bool #

[View source]