Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inversion of Control < Dependency Injection [closed]

Tags:

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?

like image 408
John Leidegren Avatar asked Jul 12 '10 07:07

John Leidegren


People also ask

Is Dependency Injection Inversion of Control?

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.

What is Inversion of Control How does that related to Dependency Injection?

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.

When should you not use Inversion of Control?

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.

What is IoC example?

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.


2 Answers

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.

like image 154
Mark Seemann Avatar answered Sep 20 '22 17:09

Mark Seemann


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.

like image 37
Krzysztof Kozmic Avatar answered Sep 18 '22 17:09

Krzysztof Kozmic