views:

128

answers:

4

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?

+1  A: 

Yes, IoC means the class itself does not control things, but is called from outside. Dependency injection is the way to do this.

Dependency injection is indeed a much more concrete term, more well-defined than inversion of control.

Sjoerd
+6  A: 

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.

Mark Seemann
Thanks, that does clarify things. Do you know of a place where I might find more examples of IoC?
John Leidegren
In reality, IoC (as opposed to DI) is everywhere in OO. It's harder to find examples of the opposite. I'm only really knowledgeable of the .NET platform, but almost any type of sub-framework you use there (ASP.NET, Windows Forms, WCF etc.) are IoC frameworks. Only console apps really fall outside of this category.
Mark Seemann
That's an assurance given Fowler's statement: "Inversion of Control is too generic a term". To be more specific, I'd love to see DP and IoC examples. I'm really hung up on learning more about how to achive good IoC through DP.
John Leidegren
DP? Do you mean DI?
Mark Seemann
@Mark - Yes, silly typo. I'm learning about how to leverage IoC and DI and code examples would definitely go along way there.
John Leidegren
Well, I *am* working on a book on the subject if you can read C# examples... http://www.manning.com/seemann/
Mark Seemann
A: 

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.

Krzysztof Koźmic
A: 

You could represent this in C# like below... :)

public abstract class IoC
{
}

public class DependencyInjection  : IoC
{    
}

public class ServiceLocator : IoC
{        
} 
Matt