I'm getting the feeling that there's is not such thing as inversion of control or rather the correct term is dependency injection. Am I wrong to assume this?
I've been trying to define IoC for my own sake. In doing so I've learned a great deal about IoC containers and dependency injection.
Just now, I read this from Martin Fowler's website:
As a result I think we need a more specific name for this pattern. 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.
In the world of modern IoC isn't dependency injection just one way to achieve IoC?
Dependency Injection is the method of providing the dependencies and Inversion of Control is the end result of Dependency Injection. IoC is a design principle where the control flow of the program is inverted. Dependency Injection is one of the subtypes of the IOC principle.
Dependency Injection was originally called Inversion of Control (IoC) because the normal control sequence would be the object finds the objects it depends on by itself and then calls them. Here, this is reversed: The dependencies are handed to the object when it's created.
You should not use IoC/DI where you cannot demonstrate concrete examples of how the complexity added through the use of IoC/DI is out weighed by the benefits of using IoC/DI.
A great example of an implementation of IoC is Spring Framework. The Spring container instantiates and manages the lifecycle of the objects that are a part of the program. The user in the configuration file provides the information related to what objects and dependencies are required by the application.
If you accept Fowler's definition, Inversion of Control is a much broader term than DI that covers all framework usage where you plug into a framework, but the framework is still in control.
For example, in .NET, frameworks such as ASP.NET or Windows Presentation Foundation are ultimately in control, but provide various events and Seams you can use to build an application. The same is true on other platforms.
Dependency Injection is a specialization of IoC that applies IoC specifically to manage dependencies.
That's one of the few points me and Mark disagree on. By my book, Inversion of Control principle is same as here so I won't rehash it.
Dependency injection is merely an act of externalizing creation of dependencies to the outside world by components.
Managing these dependencies (and lots of other stuff) is what Inversion of Control Containers do, and using DI as part of it, is merely an implementation detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With