Languages / Paradigms

Dependency Injection

Dependency injection explained: why a class should receive its dependencies instead of creating them, how it makes testing easy, and what DI containers do.

  • 7 min read
  • 5 questions
  • +35 XP
Transcript

Dependency injection explained by cats. Kitty is hungry. Kitty wants food. Kitty goes to the feeder. The feeder doesn't make food. It just serves whatever you put in. Today, human put fish. Kitty eat fish. Tomorrow, human will put biscuits.

Kitty will eat biscuits. Same feeder, but different food. Human inject different food in the feeder every day. That's dependency injection. Where feeder is a class. Food is a dependency. Human is the injector. Simple. Follow for more simple coding explanations.