Friday, August 24, 2012

What is Inversion of control and why we need it?

Most of programmer need inversion of control pattern in today’s complex real time application world. So I have decided to write a blog post about it. This blog post will explain what is Inversion of control and why we need it. We are going to take a real world example so it would be better to understand.

The problem- Why we need inversion of control?


Before giving definition of Inversion of control let’s take a simple real word example to see why we need inversion of control. Please have look on the following code.

public class class1
{
    private class2 _class2;

    public class1()
    {
       _class2=new class2();
    }

}
public class class2
{
     //Some implementation of class2
}

I have two classes “Class1” and “Class2”.  If you see the code in that I have created a instance of class2 class in the class1 class constructor. So the “class1” class is dependent on “class2”. I think that is the biggest issue in real world scenario as if we change the “class2” class then we might need to change the “class1” class also. Here there is one type of dependency between this two classes that is called Tight Coupling. Tight coupling will have lots of problem in real world applications as things are tends to be change in future so we have to change all the tight couple classes that are dependent of each other. To avoid this kind of issue we need Inversion of control.

What is Inversion of Control?


According to the wikipedia following is a definition of Inversion of control.

“In software engineering, Inversion of Control (IoC) is an object-oriented programming practice where the object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.”

So if you read the it carefully it says that we should have object coupling at run time not compile time where it know what object it will create, what method it will call or what feature it will going to use for that. We need to use same classes in such way so that it will not tight couple with each other.

There are multiple way to implement Inversion of control. You can refer wikipedia link for knowing multiple ways of implementing Inversion of control. In future posts we are going to see all the different way of implementing Inversion of control.

Shout it

kick it on DotNetKicks.com
Share:

2 comments:

  1. Have a look at here for detailed concept

    http://rizviof0.wordpress.com/2012/09/06/an-idea-of-a-three-tire-application-using-iocinversion-of-control-didependency-injection-and-perfectly-decoupled-layers/
    and
    http://rizviof0.wordpress.com/2012/09/06/a-simple-container-used-in-mvc-web-applications/

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews