• Software design patterns where object receives dependencies from an external source, rather than creating them itself
  • essential for large, scalable systems
  • good for modularity, reusability, and testability by decoupling from dependencies
    • easier to mock dependencies
    • easy to change out things because it’s created in one place

roles of dependency injection

  • client
    • depends on the services provided by another source
    • receives dependencies from injector
  • service
    • offers a particular functionality - independent of clients
  • injector
    • creates instances of services and injects into the client
    • aware of client’s dependencies, provides services during runtime
  • interface
    • defines contract that service must implement
    • clients rely on these interfaces, rather than specific implementation, enabling ease of swapping implementation beneath
      • for example - a NotificationService client receives a NotificationProvider object rather than a more specific implementation like EmailProvider , enabling loose coupling