A look at Dependency Injection and Inversion

added by RobertTheGrey
10/20/2011 10:25:53 AM

725 Views

There seems to be a fair amount of confusion around this topic, a particularly useful tool in the SOLID toolbox, but in the wrong hands can go horribly wrong. Let's try and set the record straight now...


4 comments

vijayst
10/20/2011 11:30:53 AM
The article has very good references to understanding the Dependency Injection and Inversion of Control pattern. I used to talk about these two patterns as if they are the same. It is good to know the subtle difference.

Noldorin
10/20/2011 5:27:42 PM
Good job here. This does presents a pretty good collection of links and summaries. However, I must take point slightly with the definition of "Dependency Inversion Principle" -- the term 'pattern' is used very loosely these days, and I don't think it's correct in this case -- a principle is a principle, and that's it. "Depdendency Injection" here is really the pattern, if anything -- an implementation detail -- it should (if done correctly) be a way to implement the aforementioned *principle* (or 'maxim' as I prefer to call it).

RobertTheGrey
10/20/2011 5:40:12 PM
That's an interesting point you bring up. The very problem here is that people think they are the same thing. They are different. One does not achieve the other. It's easy to put an interface in the constructor parameter, throw an instance at it and call that "Dependency Injection". That does not mean that you've inverted the dependencies though that that the objects have been properly decoupled. Only by inverting the dependencies can one achieve that, but all to often I see developers pointing at code saying - "See, I'm passing in the dependency, they're loosely coupled now.".

In most of those cases I was able to show the flaw in their thinking, but until they thought of them as separate, they went ahead and made the same mistake again. This article is an effort to show their differences, not the similarities :)

Noldorin
10/20/2011 5:41:48 PM
Sure, but I still say the latter is a *common way* to carry out the former (principle).

Also, I was just being a stickler with terminology. :-)

RobertTheGrey
10/20/2011 5:51:24 PM
Agreed - I guess I find that talking about Dependency Inversion in practical terms, the word "principle" feels a bit esoteric to me, kind of like trying to define "good behavior" to a child - lost of room for misunderstanding. I guess the same could be said of "pattern", but it just feels closer to code :)

Noldorin
10/20/2011 5:52:56 PM
Yeah, that's fair enough. Software developers tend to be practical people, by nature. :-) I guess it's the physicist in me wanting to deal with principles and maxims heh!