views:

130

answers:

2

I'm reading theory about dependency inversion and decoupling and I can't see the difference between the two.

Dependency inversion talks about decoupling functional components so that higher level components don't depend on lower level components.

Decoupling talks about the same thing and how to achieve it. But then we have IoC Containers that mess things up even further. Why aren't they rather called Dependency Inversion Containers or even better Dependency Injection Containers, because they serve runtime coupling of independent components?

Then we have Inversion of Control. It's basically the same thing as Dependency Inversion isn't it? Why are there three terms that describe the same thing? Or am I blind?

  1. What is the difference between the three?
  2. What does IoC have to do in IoC Containers?
+4  A: 

Dependency Injection achieves Decoupling using Inversion of Control.

Boris Pavlović
Couldn't be more simpler.
Pradeep
is it **thought Inversion of Control** or is it **though Dependency Inversion**? I'd say it's the latter.
Robert Koritnik
-1 this actually *couples* the term "dependency injection" to "inversion of control". Dependency injection is a decoupling technique on its own.
Mauricio Scheffer
@Mauricio: I don't think **Dependency Injection** is **Decoupling** or decoupling technique at all. DI provides means for coupling decoupled components. Not the other way around.
Robert Koritnik
+5  A: 

Decoupling is a very general principle applicable in many fields. Dependency inversion is a specific form of decoupling where you decouple the higher levels of your system from the lower levels by separating them into libraries and using interfaces. This allows you to replace lower level parts of your system without major rework.

For example, instead of the higher level parts of the system creates concrete instances of the lower level classes an IoC container can be used to decouple how objects are created.

Inversion of control is a design principle used by framework libraries that allow the framework regain some control from the application. I.e., a windowing framework may call back into application code when certain user interface events occur. Martin Fowler uses the term Hollywood Principle as in Don't call us, we'll call you. Decoupling is an important part of inversion of control.

But what has an IoC container to do with inversion of control? To quote Martin Fowler:

Inversion of Control is too generic a term, and thus people find it confusing. As a result with a lot of discussion with various IoC advocates we settled on the name Dependency Injection.

(Note that Martin Fowler talks about dependency injection, not dependency inversion.)

An IoC container helps to implement dependency injection and perhaps a better term would be dependency injection container. However, the IoC container name seems to stick. Dependency injection is an important component in dependency inversion, but the use of IoC containers for dependency injection can be confusing as inversion of control is a broader and more generic principle.

You point out that the naming isn't very consistent but that shouldn't be a big surprise as these terms have been independently invented and used even though they overlap.

Martin Liversage
I agree, though I would not use the term IoC container since it is, as you say, not really correct. I think it is unimportant to follow the (incorrect) popular naming scheme here, since anyone who understands principle of DI containers should be able to understand correct naming.
Andrey Shchekin