Skip to main content

ADR-006: Graph-Based Dependency Injection

Context

The framework’s dependency injection required manual ordering of dependencies, making it error-prone and difficult to debug. Complex applications needed automatic dependency resolution with circular dependency detection.

Decision

Implemented a graph-based dependency resolution system that automatically analyzes constructor dependencies and resolves them in correct order with built-in circular dependency detection.

Key Components

  • Dependency Graph Construction: Automatic analysis of constructor signatures
  • Topological Sorting: Correct dependency resolution order using graph traversal
  • Circular Dependency Detection: Early detection with clear error messages
  • Automatic Registration: Seamless integration with existing DI container

Core Algorithm

Consequences

Positive

  • Eliminates manual dependency ordering
  • Prevents circular dependencies with clear error messages
  • Improves debugging with clear dependency chains
  • Maintains type safety through annotations
  • Scales efficiently with complex applications

Negative

  • Slight startup overhead for graph construction
  • Additional memory for graph structure
  • Requires proper type annotations
  • Less explicit control over resolution order

Usage Example

The system automatically resolves dependencies in correct order regardless of registration sequence.