IOC Containers Demystified

When I first came across this big buzz called IOC Containers, I found it very close to a factory class in its purpose and philosophy. As I dig deeper and deeper into the concept, I find myself more and more convinced that an IOC Container is nothing more than a Fancy Factory.

Amid all the mumbo-jumbo around it, these IOC Containers actually quite simple at their hearts. IOC Containers are essentially a tool for Dependency Injection. The whole idea being able to invert the flow of program control in a manner that maximum amount loose coupling can be achieved. Hence the name IOC (Inversion Of Control) Container.

Sounds vague? Let us jump into some code in order to understand this. Allow me to use the example of my favorite ‘PizzaStore’ class.


Public class PizzaStore
{
    Public ChickenPizza Prepare()
    {
        ChickenPizza pizza = new ChickenPizza();
        return pizza.Prepare();
    }
}

In the light of Dependency Inversion Principle (DIP) , this is not the right way of doing it. For every new Pizza class that is created in the system there needs to be a new PizzaStore class. This is because the PizzaStore class depends upon the Pizza class. A desirable design goal (according to DIP) here would be that the Pizza and the PizzaStore class should be decoupled in a manner that same PizzaStore class should be able to work with different types of Pizza. In other words, the dependency between Pizza and PizzaStore should be so inverted that PizzaStore should not directly depend on Pizza class. As a matter of fact, according to DIP, it should depend on an abstraction (IPizza interface in this case).


Public class PizzaStore
{
    Public IPizza Prepare(IPizza  pizza)
    {
        return pizza.Prepare();
    }
}

The client program must take pass the right dependency (instance of pizza) to the IPizza. This tecnique of injecting the dependency from outside is called Dependency Injection.

If we separate out the piece of code that assumes the responsibility of inverting the flow of control of the system in comparison to procedural programming using the technique of Dependency Injection, we would call this piece of code an IOC Container.

An IOC container, at the very basic should be able to do two things
• Register a Dependency
• Resolve a Dependency

For the ease of understanding, let us start with the Resolve part. Take a look at the following code,


public class PizzaStore
{
    Public IPizza Prepare()
    {
        var pizza = Container.Resolve<IPizza>();
        return pizza.Prepare();
    }
}

Here the Container determines and returns the right instance of the Pizza. Hence, whenever an instance of the type Pizza is required by the PizzaStore class, it will request the Container to resolve and return the right instance for the Pizza. This means that it is the responsibility of the Container to instantiate a class. This also means that the Container needs to have the information about what instance should it return for a given type. This is when the Register part comes into picture. The Register method tells the Container what object should be returned for a given type.


ChikenPizza chkPizza = new ChikenPizza();
Container.Register<IPizza>(chkPizza);

Or sometimes,


Container.Register<IPizza>(ChikenPizza);

Most of the existing IOC containers allow an ability to Register through configuration also. A typically full-blown IOC Container also provide with other sophistications like managing the scope and lifetime of the instance. For example, would you like your object to be instantiated on per call basis, or per thread basis or should your instance be a singleton instance.

In a nutshell, all that an IOC Container essentially does is know what instance to be retuned for a given type and then return that instance through an abstraction (generally interface) using dependency Injection.

2 thoughts on “IOC Containers Demystified

  1. sorry, can you explain please what is the purpose of such block of code:
    Public ChickenPizza Prepare(IPizza pizza)
    {
    pizza.Prepare();
    }

    You are specifying that return object should be ChickenPizza but don’t return it. I’m confused because even you meant something like the following
    Public ChickenPizza Prepare(IPizza pizza){ return pizza.Prepare();}

    It doesn’t have any logic either because it’s not obvious that when any pizza will be prepared it must be ChickenPizza.

    Also
    public class PizzaStore
    {
    Public IPizza Prepare()
    {
    Return Container.Resolve();
    }
    }
    as you specified below doesn’t make sense regarding to the example above cause Prepare was moved to PizzaStore

    1. Thanks for pointing out the errors. I appreciate that and I have updated the code accordingly. My focus has always been the concepts and few errors here in there in code syntax is something I don’t worry about as long as the point is communicated. But, you are right in bringing up that it was creating confusion as opposed to helping communicate the concept and needed to be addressed.
      Hence, my thanks again for helping correct the errors and making it more useful for others too.

Leave a reply to Anshuman Cancel reply