开发者

What is Inversion of Control? How does that relate to dependency injection? [duplicate]

This question already has answers here: 开发者_如何学Python Closed 11 years ago.

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜