Container
TheContainer is the central component of the dependency injection system. It is responsible for registering, resolving and managing the lifecycle of dependencies.
Import
Creating a Container
Typed Tokens and Factory Dependencies
UsecreateToken<T>() so register and resolve infer types automatically. For factories, wrap dependencies with deps() to type the factory arguments:
string / Symbol) still work but need an explicit generic: container.resolve<UserService>("USER_SERVICE").
API
Dependency Registration
Dependency Resolution
Lifecycle Management
React Integration
Observability
Import/Export
Debug Mode
The Container has a powerful debug mode that allows detailed visualization of the dependency resolution process. When enabled, it displays colored console logs showing:- Dependency registration
- Dependency resolution
- Dependency graph
- Cached instances
- Resolution errors and issues
- Instance lifecycles
Enabling Debug Mode
Logging System
The debug mode uses an internal logging system with color formatting for easy visualization:- INFO (green): General information and section titles
- DEBUG (cyan): Details about dependency resolution and registration
- WARN (yellow): Warnings about potential issues
- ERROR (red): Critical errors like circular dependencies
- Tokens (magenta): Dependency identifiers
- Classes (bright yellow): Implementation names
- Lifecycle (bright cyan): Lifecycle types
Note: The logging system has an intelligent mechanism to prevent duplicate messages. Identical messages emitted within a 200ms interval are automatically suppressed, preventing console pollution during repetitive operations or loops.
Information Displayed in Debug Mode
Debug mode displays detailed information about:-
Dependency Resolution: Shows when and how each dependency is resolved
-
Dependency Graph: Displays the complete dependency graph after first resolution
-
Cached Instances: Lists all currently cached instances
-
Problem Detection: Alerts about potential issues like circular dependencies
-
Instance Creation Process: Details how instances are created
-
Promise Cache: Information about promise caching for asynchronous methods
Dependency Resolution Flow
Debug mode allows visualization of the complete dependency resolution flow:- Circular Dependency Check: First, checks for circular dependencies
- Cache Check: Checks if the instance already exists in cache
- Instance Creation: If not cached, creates a new instance
- Dependency Resolution: Recursively resolves all required dependencies
- Cache Storage: Stores the instance in cache according to configured lifecycle
- Dependency Tracking: Records dependency relationships for future invalidation
When to Use Debug Mode
Debug mode is especially useful during:- Initial application development
- Debugging dependency injection issues
- Performance analysis and optimization
- Understanding dependency resolution flow
- Memory leak identification
Lifecycles
The container supports four types of lifecycles:- singleton: A single instance is created and reused (default)
- transient: A new instance is created for each resolution
- scoped: An instance is created per scope (e.g., per HTTP request)
- immutable: A single instance is created and never invalidated or garbage collected
Lifecycle Comparison
Immutable Lifecycle
Theimmutable lifecycle is particularly useful for:
- State management libraries (React Query, Redux, Zustand)
- API clients that need to maintain stable connections
- Services that need to maintain consistent state across components
Immutable Integrity Verification
The container provides a method to verify the integrity of immutable instances:- Debugging issues with state management
- Ensuring critical services maintain their identity
- Detecting unexpected modifications to immutable instances