Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've been seeing a lot of references of Dependency Injection (DI) & Inversion Of Control (IOC), but I don't really know if there is a difference between them or not.

I would like to start using one or both of them, but I'm a little confused as to how they are different.

share|improve this question

migrated from stackoverflow.com Jan 23 '12 at 20:38

This question came from our site for professional and enthusiast programmers.

    
Inversion of Control usually refers to the "containers" while Dependency Injection refers to the actual pattern. But they go hand in hand. I would recommend reading Martin Fowler's article to get a handle on the topic. –  Ben Hoffstein Sep 26 '08 at 12:56
    
Dependency Injection is a thing that you do, which leads to a command structure called Inversion of Control. They are inherently linked. –  pfranza Sep 26 '08 at 12:56
1  
DI is a form of IoC, I gave a pretty detailed explanation of DI and IoC in this answer –  aku Sep 26 '08 at 13:01
    
I'd say that DI is a special case of IOC. Traditional control goes module->request module from module manager, in DI it is inverted to module manager->get requested dependencies from module. –  Rafał Dowgird Sep 26 '08 at 13:02
    
So in other words, basically IoC is an implementation of using DI. Am I getting that correctly? –  Sung Meister Mar 6 '09 at 14:59

4 Answers 4

Inversion of control is a design paradigm with the goal of giving more control to the targeted components of your application, the ones getting the work done. In a traditional top down designed system, not only is the system designed that way, but because it is, the logical control of the application flows from the top components, the ones designed first, to the ones designed last, the components usually doing any real work. Inversion of control somewhat literally is giving control of the application to the operation specific components.

Dependency injection is a pattern used to create instances of objects that other objects rely on without knowing at compile time which class will be used to provide that functionality. Inversion of control relies on dependency injection because a mechanism is needed in order to activate the components providing the specific functionality. How does the framework know which components to create if it is no longer in control?

A great example of these concepts at work is the plug-in framework in Reflector. The plug-ins have much more control of the system even though they are the same code doing the work. The application framework allows access to its functionality through a variety of service providers. A plug-in is given a reference to the service providers when it is created. Through those providers, the plug-in has the ability to add its own menu items, change how files are displayed, display its own information in the appropriate panels, etc. Control has been taken from the main application and given to the component doing the specific work; inversion of control.

How does the application know to create those plug-ins and how is the code executed? Dependency injection is your answer. A factory creates the plug-ins that implement a specific interface using configuration information, reflection and the Activator object (in .NET at least). The instance that is created, the dependency, is injected into a reference, or in this case a collection of all of the running plug-ins. A single method is called on each of those plug-ins, Initialize if memory serves, which passes control over to the plug-in. The framework doesn't know what they will do, it just lets them do it.

<EDIT> As I've learned a bit since I answered this question, I realize that a better example are the plugins and service provider. While I don't recommend using the service provider pattern itself, taken simply as a dependency the provider is injected into the plugins through the plugin constructor typed as an interface. The plugins don't know the concrete type that implements the interface, they just have an instance that implements the contract to work against.</EDIT>

The two concepts work together in this way to allow for much more flexible, reusable, and encapsulated code to be written. As such, they are important concepts in designing object oriented solutions.

share|improve this answer

Good article to understand IOC and DI http://martinfowler.com/articles/injection.html

IOC (Inversion of Control)

IOC means

  1. coding to interface (one component should dependent on other component’s interface and not on impl), and e.g

    interface iComp_2 {...}
    
    class Comp_1 {
        iComp_2 c2 = ….;
    }
    
  2. removing the component implementation specific code e.g

    Comp_1 {
        iComp_2 c2 = getComp_2_Impl(); // not new Comp_2_Impl();
    }
    

IOC can be achieved by either of the following:

1. DI (Dependency Injection)

3 types of DI

1.1 Constructor Injection

1.2 Setter Injection

1.3 Interface Injection

2. Service Locator

DI (Dependency Injection) container

Runtime impl determination and not compile time: determines at runtime which concrete implementation of an interface to be used based on some config file (so at compile time we don’t know which impl is going to be used and thus increases configurability of the application). It is an implementation where the concrete relation between different modules is decided at "run time".

Instantiation of impl after dependency injection: after determining the impl, it instantiates that impl by first creating all of its dependencies (specified in config file) and then injecting those dependencies into that impl

Instance Life-cycle management: DI containers usually only keep a reference to objects it needs to manage life cycles for, or that are reused for future injections, like singletons or flyweights. When configured to create new instances of some components for each call to the container, the container usually just forgets about the created object. Otherwise the garbage collector would have a hard time collecting all these objects when no longer used.

share|improve this answer

Inversion of control is a general concept, in functional languages is usually done using continuations. This let's you write an API where both sides are 'caller', and none the 'callee'. In other, more static environments you don't have this facillity, so you need this hack to insert hints into the control flow.

share|improve this answer

I would say "Inversion of Control" is a way to design a system where all the modules are thought of abstract entities.

And, "Dependency Injection" is an implementation where the concrete relation between different modules is decided at "run time".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.