What is dependency injection?

The best definition I’ve found so far is one by James Shore:

“Dependency Injection” is a 25-dollar
term for a 5-cent concept. […]
Dependency injection means giving an
object its instance variables. […].

There is an article by Martin Fowler that may prove useful, too.

Dependency injection is basically providing the objects that an object needs (its dependencies) instead of having it construct them itself. It’s a very useful technique for testing, since it allows dependencies to be mocked or stubbed out.

Dependencies can be injected into objects by many means (such as constructor injection or setter injection). One can even use specialized dependency injection frameworks (e.g. Spring) to do that, but they certainly aren’t required. You don’t need those frameworks to have dependency injection. Instantiating and passing objects (dependencies) explicitly is just as good an injection as injection by framework.

Leave a Comment