class Di::Registry

Overview

Internal registry for storing providers and tracking shutdown order. Uses a string key format: "TypeName" for default, "TypeName/name" for named providers. For interface bindings, uses "TypeName:ImplName" to allow multiple implementations. Thread-safe for multi-threaded Crystal (-Dpreview_mt).

Defined in:

di/registry.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.interface_key(interface_type : String, impl_type : String) : String #

Build an interface binding key from interface and implementation types.


[View source]
def self.key(type_name : String, service_name : String | Nil = nil) : String #

Build a registry key from type name and optional service name.


[View source]

Instance Method Detail

def clear : Nil #

Clear all providers and reset order.


[View source]
def count_implementations(interface_type : String) : Int32 #

Count implementations for an interface type.


[View source]
def delete(key : String) : Nil #

Remove a provider by key (used for rollback on partial registration failure).


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

Iterate over all providers with their keys. Note: Holds mutex for the duration of iteration.


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

Get a provider by key, raising ServiceNotFound if not registered.


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

Get a provider by key, or nil if not registered.


[View source]
def get_all(interface_type : String) : Array(Provider::Base) #

Get all providers for an interface type (keys starting with "Type:"). Returns array of providers matching the interface prefix.


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

Get all providers keyed by full key for deduplication in child scopes.


[View source]
def implementation_names(interface_type : String) : Array(String) #

Get implementation names for an interface type.


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

Return all registered keys in registration order.


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

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


[View source]
def register_interface(interface_type : String, impl_type : String, provider : Provider::Base) : Nil #

Register an interface binding with implementation tracking. Uses "InterfaceType:ImplType" key format to allow multiple implementations. Does NOT raise for duplicate interface type — multiple impls are tracked.


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

Check if a provider is registered for the given key.


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

Return all registered keys in reverse order (for shutdown).


[View source]
def size : Int32 #

Number of registered providers.


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

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


[View source]