Skip to content

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

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.

Implement a DI container with:

  1. Register - Register a factory function for creating services
  2. RegisterSingleton - Register a pre-created singleton instance
  3. Resolve - Get a service by name (creating it if needed)
  4. Support for different service lifetimes

Simple DI Container

~30 minhard

Implement dependency injection container with singleton and factory patterns

  • 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