7

Possible Duplicates:
Difference between Dependency Injection (DI) & Inversion of Control (IOC)
Inversion of Control < Dependency Injection

Hey, This is a Scott Hanselman interview question. I always find this question hard to answer. May be parts of this question could be answered on stack but on a whole this is very important.

I would also like to know other forms of IoC apart from DI.

Can someone explain me with some real time examples.

Thanks

Community
  • 1
  • 1
Praneeth
  • 2,527
  • 5
  • 30
  • 47
  • http://martinfowler.com/articles/injection.html#InversionOfControl. explains the differences. – Robert Harvey Feb 18 '11 at 03:25
  • @duffymo, @Robert Harvey, sorry guys they're not. – ocodo Feb 18 '11 at 03:42
  • 1
    @duffymo No they're not the same at all, they just tend to be used together. The point of the interview question is to figure out who actually knows what they are, since they're so commonly used and generally poorly understood. Also because interviewers like being pedantic :-) – mblinn Feb 18 '11 at 03:43
  • Duplicate: http://stackoverflow.com/questions/3226605/inversion-of-control-dependency-injection – Mark Seemann Feb 18 '11 at 12:57
  • Not just pedantic, but misdirected. From (Dependency Injection Prasanna2009) "The phrase Inversion of Control is rather vague and connotes a general reversal of responsibilities, which is nonspecific...In common use, dependency injectors are frequently referred to as IoC containers. In the interest of clarity, for the rest of this book I will abandon the term IoC and its evil cousin IoC container." – Jeff Axelrod Jun 28 '11 at 13:15

2 Answers2

13

Dependency injection is not a form of IoC. Inversion of Control is a pattern that's not related to DI at all, except for the fact that they're usually used together in some sort of framework, which lead people to think they're the same thing when they're not.

Dependency injection just means that you inject a class's dependencies into it, through a constructor or a series of setters, rather than instantiating them in the class. It can be done without an IoC container of any sort, completely manually.

A really simple example of manual DI could be:

import org.apache.http.client.HttpClient;


public class TwitterClient {

    private HttpClient httpClient;

    public TwitterClient(HttpClient httpClient){
        this.httpClient = httpClient;
    }
}

Whenever you create a TwitterClient in your code, you would have to also create an HttpClient and pass it in. Since this would be rather tedious, there are frameworks to make it easier, but as I mentioned it's totally possible to do it manually. This article touches on manual DI - http://googletesting.blogspot.com/2009/01/when-to-use-dependency-injection.html, and in fact early versions of some Google produces were built entirely around manual DI.

The benefit here is that you can swap out the implementations, so if you wanted to pass in a stubbed-out client for unit testing purposes it's easy. Otherwise, there would be no real way to unit test a class like that.

IoC means that you've got some sort of framework that's in control of the application's lifecycle. A good example of IoC that doesn't involve DI would be just about any of the Cocoa apple frameworks, that control the lifecycle of a Cocoa app. You implement certain methods that get called at certain points in the lifecycle of the app. That's why it's the "Hollywood principle", you don't call the framework, the framework calls you.

mblinn
  • 3,264
  • 17
  • 15
  • Can you please explain the IoC in .net or java terms. I am not familiar with Apple framework. – Praneeth Feb 18 '11 at 07:27
  • It's a very general concept. All it means is that the framework you're deploying your code to is in control of the lifecycle of that code, as opposed to a your code controlling its own lifecycle. – mblinn Feb 18 '11 at 13:19
  • Probably the simplest example would be deploying a servlet to a container like Tomcat. In this case, the container is responsible for creating and initializing your application, and it controls what application handles what request. An app container may not be something people think of as IoC, but it is... – mblinn Feb 18 '11 at 13:25
  • I wouldn't say that DI is *completely* unrelated to IoC. IoC uses DI to achieve its aims. And IoC doesn't require a framework. – Robert Harvey Feb 18 '11 at 19:30
  • @Robert Harvey IoC doesn't need to use DI. Period. Full stop. Most of the time, it does not. It's used together in Spring and Spring-like frameworks, so people often conflate the two. The fact that they're two separate things is the point of interview questions like this (probably more because it's easy to ask than because it shows actual useful knowledge). First time I came across the term was on Martin Fowler's site - http://martinfowler.com/bliki/InversionOfControl.html, and his examples actually rely on a windowing toolkit with no reference to DI anywhere. – mblinn Feb 18 '11 at 19:50
  • @mblinn: Fair enough. But now we are *really* splitting hairs. Does the employer really want to know whether or not I know the pedantic difference, or does he want to know that I can do my job, and do it well? That, I think, is the salient point here. And if I were being interviewed, that is *exactly* what I would say. – Robert Harvey Feb 18 '11 at 21:40
6

For an interview I would be brief and say inversion of control is the ability to decouple components from each other by separating order of execution with the actual business rules being execute. An example would be in an MVC app, a view calling several controllers, not caring what the controllers will do, but controlling the order they execute in.

Dependency injection is using interfaces to call between classes, and only at runtime will the actual class be specified (for example in a config XML file). DI allows testing to be done at a much more granular level. It helps with maintenance by isolating bugs from testing.

They are similar in that dependency injection uses inversion of control to allow code to call interfaces without caring about the actual implementation class, just the interface. Dependency injection allows (from the example) the view to control the order of the interface calls without caring which class will actually be used.

Russell
  • 17,481
  • 23
  • 81
  • 125