Skip to main content

Container

The Container 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

Use createToken<T>() so register and resolve infer types automatically. For factories, wrap dependencies with deps() to type the factory arguments:
Legacy tokens (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
Additionally, the logger uses specific colors for different types of information:
  • 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:
  1. Dependency Resolution: Shows when and how each dependency is resolved
  2. Dependency Graph: Displays the complete dependency graph after first resolution
  3. Cached Instances: Lists all currently cached instances
  4. Problem Detection: Alerts about potential issues like circular dependencies
  5. Instance Creation Process: Details how instances are created
  6. Promise Cache: Information about promise caching for asynchronous methods

Dependency Resolution Flow

Debug mode allows visualization of the complete dependency resolution flow:
  1. Circular Dependency Check: First, checks for circular dependencies
  2. Cache Check: Checks if the instance already exists in cache
  3. Instance Creation: If not cached, creates a new instance
  4. Dependency Resolution: Recursively resolves all required dependencies
  5. Cache Storage: Stores the instance in cache according to configured lifecycle
  6. 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
Note: Debug mode can impact production performance, so it’s recommended to use it only in development environments.

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

The immutable 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:
This is useful for:
  • Debugging issues with state management
  • Ensuring critical services maintain their identity
  • Detecting unexpected modifications to immutable instances

Complete Example