What is control inversion? How does this relate to dependency injection?

Possible duplicates:
The difference between dependency injection (DI) and control inversion (IOC)
Inversion of Control <Dependency Injection

Hey, this is a question from an interview with Scott Hanselman . I always find this question difficult to answer. Maybe parts of this question could be answered on the stack, but overall it is very important.

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

Can someone explain some real-time examples to me.

thanks

+6
design-patterns dependency-injection inversion-of-control
source share
2 answers

Injection injection is not a form of IoC. Inversion of Control is a template that is not associated with DI at all, except for the fact that they are usually used together in some kind of structure, which makes people think that they are the same when they are not.

Enabling a dependency simply means that you insert class dependencies into it, through the constructor or a series of setters, rather than creating them in the class. This can be done without any type of IoC container, completely by hand.

In fact, a simple example of manual DI might be:

import org.apache.http.client.HttpClient; public class TwitterClient { private HttpClient httpClient; public TwitterClient(HttpClient httpClient){ this.httpClient = httpClient; } } 

Whenever you create TwitterClient in your code, you will also need to create an HttpClient and pass it. Since this would be rather tedious, there are frameworks to simplify it, but, as I said, it is quite possible to do this manually. This article discusses manual DI - http://googletesting.blogspot.com/2009/01/when-to-use-dependency-injection.html , and in fact, early versions of some Google products were built entirely around manual DI.

The advantage here is that you can swap implementations, so if you want to go through a cut-out client for unit testing, it will be easy for you. Otherwise, there would be no real way to unit test a class like this.

IoC means that you have some kind of structure that governs the life cycle of the application. A good example of an IoC that is not DI related would be just about any of the Cocoa frameworks that control the life cycle of a Cocoa application. You implement certain methods that are called at specific points in the application life cycle. That is why this is the "principle of Hollywood", you do not call the framework, the structure calls you.

+12
source share

For the interview, I would be brief and say that control inversion is the ability to separate components from each other, sharing the execution order with the actual business rules that are executed. An example would be in an MVC application, a view that invokes multiple controllers, not caring what the controllers will do, but controlling the order in which they are executed.

Dependency injection uses interfaces to call between classes, and only at run time will the actual class be specified (for example, in the XML configuration file). DI allows testing at a much more granular level. This helps with maintenance by isolating errors from testing.

They are similar in that dependency injection uses control inversion to allow code to call interfaces without worrying about the real implementation class, just an interface. Inclusion of dependencies allows (from an example) to look at the control of the order of interface calls, without worrying about which class will really be used.

+6
source share

All Articles