Dependency Injection Container
Dependency Injection Container
Section titled “Dependency Injection Container”Build a simple dependency injection container that manages service lifetimes and resolves dependencies.
Recommended Prerequisites
Complete these exercises first for the best learning experience:
- ○Clean Architecture Layers
0/1 completed
Background
Section titled “Background”A Dependency Injection (DI) Container (or IoC Container) manages service creation and lifecycle:
- Factory Registration: Register factories that know how to create services
- Singleton Lifetime: Same instance returned for all requests
- Transient Lifetime: New instance created each time
- Dependency Resolution: Container can resolve dependencies recursively
This pattern decouples service construction from usage and centralizes configuration.
Your Task
Section titled “Your Task”Implement a DI container with:
Register- Register a factory function for creating servicesRegisterSingleton- Register a pre-created singleton instanceResolve- Get a service by name (creating it if needed)- Support for different service lifetimes
Simple DI Container
~30 minhard
Implement dependency injection container with singleton and factory patterns
Key Concepts
Section titled “Key Concepts”- Service Container: Central registry for managing object creation and dependencies
- Factory Pattern: Functions that encapsulate object creation logic
- Singleton Lifetime: One instance shared across all requests
- Transient Lifetime: New instance created each time
- Dependency Resolution: Container recursively resolves dependencies when creating services