From time to time, I hear questions what is the difference
between
Dependency Injection (DI),
Dependency Inversion (also DI),
Inversion of Control (IoC) and so
on. Despite you can make decision yourself, I will gather all
related stuff in this post. Also check my recommendations.
Hope you like them.
The Dependency Inversion Principle
As per title, Dependency Inversion is The
Principle. Means that this is just theoretical stuff and this
is your responsibility to invent how to implement it. The
original publication (at least I think so) was at
Object Mentor -
The Dependency Inversion Principle.
Publication defines this principle in two sentences:
High level modules should not depend upon low level modules. Both should depend upon abstractions.
Abstractions should not depend upon details. Details should depend upon abstractions.
As you can see, it is very general. Whole bunch of patterns helps to solve this problem.
The Dependency Injection Pattern
Yes-yes, it's pattern. This is one of the concrete solutions
to the application/code composition problem. And yes this
solution is implementation of the
Dependency Inversion Principle. Probably the most
known place to read is Martin Fowler -
Inversion of Control Containers and the Dependency
Injection pattern.
The Inversion of Control
There is no clear answer what the
Inversion of Control is. Some people say this is
the principle, some people - the pattern. Because name does
not suggest implementation, I prefer to refer this as the
principle. Anyways this is just another name of the
Dependency Inversion Principle or the
Dependency Injection Pattern, respectively.
Hollywood Principle
The
Hollywood Principle
is another name of the
Inversion of Control Principle.
Don't call us, we'll call you.
Simple!
The Inversion of Control Container
It is hard to not mention the
Inversion of Control Containers. This is common
name of the libraries that implement the
Dependency Injection Pattern. Why
Inversion of Control? Probably because of the
history.
The Service Locator Pattern
The
Service Locator
is another cool stuff. For some reasons many people think that
there is no relationship between
Service Locator and DI. Not true.
The Service Locator is yet another implementation
of the Dependency Inversion Principle. And yes
Service Locator is competitor for the
Dependency Injection Pattern.
For comments or feedback, write at x.com/chaliy.