class Di::Scope

Overview

A scope provides isolated provider registration with parent inheritance. Child scopes shadow parent providers without modifying them. Thread-safe for multi-threaded Crystal (-Dpreview_mt).

Defined in:

di/scope.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(name : Symbol, parent : Scope | Nil = nil, fallback_registry : Registry | Nil = nil) #

[View source]

Instance Method Detail

def clear : Nil #

Clear local providers only (does not affect parent).


[View source]
def each(& : String, Provider::Base -> ) #

Iterate over local providers only. Note: Holds mutex for the duration of iteration.


[View source]
def fallback_registry : Registry | Nil #

[View source]
def get(key : String) : Provider::Base #

Get a provider by key, raising ServiceNotFound if not in scope chain.


[View source]
def get?(key : String) : Provider::Base | Nil #

Get a provider by key, checking parent scope then fallback registry.


[View source]
def local?(key : String) : Bool #

Check if a provider is registered locally in this scope only.


[View source]
def name : Symbol #

[View source]
def order : Array(String) #

Return keys in registration order for this scope only.


[View source]
def parent : Scope | Nil #

[View source]
def register(key : String, provider : Provider::Base) : Nil #

Register a provider with the given key. Raises AlreadyRegistered if the key already exists in this scope.


[View source]
def registered?(key : String) : Bool #

Check if a provider is registered in this scope, parent, or fallback registry.


[View source]
def reverse_order : Array(String) #

Return keys in reverse order for shutdown (this scope only).


[View source]
def size : Int32 #

Number of local providers.


[View source]
def snapshot : Hash(String, Provider::Base) #

Return a snapshot of local providers for iteration without holding mutex. Use this when callbacks may call back into Di (e.g., health checks).


[View source]