QamoosTech
Architecture & DesignPatternsIntermediate

Dependency Injection

Pronunciationdih-PEN-den-see in-JEK-shun

Definition

A class receives the things it needs (database, mailer) from outside instead of creating them itself, so they are easy to replace in tests.

Where you hear it

NestJS, Spring, Angular, and testing discussions.

Examples

  • Thanks to dependency injection, we replaced the real mailer with a fake in tests.
  • Inject the repository instead of creating it with new.
  • The service receives the payment client through its constructor, so tests can pass a fake.

Common mistake

Creating dependencies with new inside the class. Then you cannot replace them for testing.

Don't confuse with

Dependency injection is often confused with the service locator pattern, but while dependency injection passes required objects from the outside, a service locator lets objects pull dependencies themselves.

Say it at work

  • In a meeting or chatLet's use dependency injection for the payment service so we can mock it easily in our unit tests.
  • In an email or ticketPlease update this component to receive its configuration via dependency injection rather than importing the global config directly.