Dependency Injection

Dependency Injection
πŸ‘¨β€πŸ’Ό Now let's see the power of composition! We can swap different logger implementations without changing EmailService. This is dependency injectionβ€”a key benefit of composition.
🐨 Open
index.ts
and:
  1. Create a MockLogger class that extends Logger and stores logs in memory (useful for testing)
  2. Create a SilentLogger class that extends Logger and does nothing (useful for production)
  3. Both should extend the Logger class from step 01
  4. Demonstrate that EmailService works with any logger implementation
πŸ’° This is the power of dependency injection: EmailService doesn't care which logger it getsβ€”it just uses whatever logger is provided. This makes testing easy and allows you to swap implementations at runtime.

Please set the playground first

Loading "Dependency Injection"
Loading "Dependency Injection"
Login to get access to the exclusive discord channel.
Loading Discord Posts