Design Pattern – Dependency Injection

Nowadays Design Patter is not only the task of software architects but also the necessary knowledge of programmers. Only by having a thorough grasp of design pattern the programmers can implement the complex software without causing a lot of bugs which will require a lot of time to fix at the maintaining period. Dependency Injection is a well-known pattern used frequently in developing environment. I think you did use this pattern already just don’t know its name. You can find a lot of books out there discussing about it, for example this Dependency Injection book. In this small blog I do not intend to write a long, loong, looong research about this pattern. I just give some code to demonstrate what Dependency Injection is.

Dependency injection (DI) in computer programming refers to the process of supplying an external dependency to a software component. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency. Do you understand its definition? I do not. So let’s take a small example. We have an interface MyHelper_Interface

  interface MyHelper_Interface
    {
        void foo();
    }

A class MyHelper will implement this interface and it will be used in Example class

    class MyHelper : MyHelper_Interface
    {

        #region MyHelper_Interface Members

        public void foo()
        {
            Console.WriteLine("foo");
        }

        #endregion
    }

    class Example
    {
        MyHelper helper;
        public Example()
        {
            helper = new MyHelper();
        }

        public void DoSomething()
        {
            helper.foo();
        }
    }

It is completely clear that Example class depends on MyHelper class. Now let’s image that case, on a beautiful day of one year later, the MyHelper class is now not supported anymore. But there is another class which implements MyHelper_Interface and what is important thing is that class is now being supported. What should we do now. Search our code again and replace MyHelper class with that new one. I am sure that is a hard job and fixing bugs after replacing must be the most terrible job in this world. So man must find out the way to minimize the dependency between classes and he call it Dependency Injection. There are 3 methods (maybe more, tell me if you know) to reach this goal.

  • Constructor injection

It is very simple, we just change our Constructor in Example class by giving it an argument whose type is MyHelper_Interface.

class Example
{
MyHelper_Interface helper;
public Example(MyHelper_Interface miHelper)
{
helper = miHelper;
}

public void DoSomething()
{
helper.foo();
}
}
  • Setter injection

Instead of using Constructor, why don’t we use a Set-Function. It reaches the same goal but in the other way.

 class Example
    {
        MyHelper_Interface helper;

        public void setHelper(MyHelper_Interface miHelper)
        {
            helper = miHelper;
        }

        public void DoSomething()
        {
            helper.foo();
        }
    }

  • Interface injection

This is the last method that I know. When the injection occurs usually, that means the class MyHelper will be used in many classes. Then we just need to create a new interface InjMyHelper_Interface and the class Example implements this new interface.

  interface InjectMyHelper_Interface
    {
        public void InjectHelper(MyHelper_Interface mhi);
    }

 class Example : InjectMyHelper_Interface
    {
        MyHelper_Interface helper;

        public void DoSomething()
        {
            helper.foo();
        }

        #region InjectMyHelper_Interface Members

        public void InjectHelper(MyHelper_Interface mhi)
        {
            helper = mhi;
        }

        #endregion
    }

I think it is pretty simple to understand 3 methods above. Their goal is to throw the dependency between Example and MyHelper class out of our code. That means we have more time to play game instead of searching, replacing code and fixing bugs. If you want to know more about DI, take a book, read it and share what you find out with me.

Leave a Reply

Your email address will not be published. Required fields are marked *