Click here to Skip to main content
16,020,313 members
Articles / Web Development / ASP.NET

An Absolute Beginner's Tutorial on Dependency Inversion Principle, Inversion of Control and Dependency Injection

Rate me:
Please Sign up or sign in to vote.
4.92/5 (425 votes)
8 Jul 2013CPOL9 min read 882K   492   153
In this article we will talk about the Dependency Inversion Principle, Inversion of Control and Dependency Injection.

Introduction

In this article we will talk about the Dependency Inversion Principle, Inversion of Control and Dependency Injection. We will start by looking at the dependency inversion principle. We will then see how we can use inversion of control to implement dependency inversion principle and finally we will look at what dependency injection is and how can it be implemented. 

Background 

Before we start talking about Dependency Injection(DI), we first need to understand the problem that DI solves. To understand the problem, we need to know two things. First dependency Inversion Principle(DIP) and second  Inversion of Controls(IoC). let us start our discussion with DIP, then we will talk about IoC. once we have discussed these two, we will be in a better position to understand Dependency Injection, so we will look at dependency injection in details. Then finally we will discuss see how can we implement Dependency injection.

Dependency Inversion Principle 

Dependency inversion principle is a software design principle which provides us the guidelines to write loosely coupled classes. According to the definition of Dependency inversion principle:

  1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
  2. Abstractions should not depend upon details. Details should depend upon abstractions.

What does this definition mean? What is it trying to convey? let us try to understand the definition by looking at examples. A few years back I was involved in writing a windows service which was supposed to run on a Web server. The sole responsibility of this service was to log messages in event logs whenever there is some problem in the IIS application Pool. So what our team has done initially that we created two classes. One for monitoring the Application Pool and second to write the messages in the event log. Our classes looked like this:

C#
class EventLogWriter
{
    public void Write(string message)
    {
        //Write to event log here
    }
}

class AppPoolWatcher
{
    // Handle to EventLog writer to write to the logs
    EventLogWriter writer = null;

    // This function will be called when the app pool has problem
    public void Notify(string message)
    {
        if (writer == null)
        {
            writer = new EventLogWriter();
        }
        writer.Write(message);
    }
}

From the first look, the above class design seems to be sufficient. It looks perfectly good code. But there is a problem in the above design. This design violates the dependency inversion principle. i.e. the high level module <code>AppPoolWatcher depends on EventLogWriter which is a concrete class and not an abstraction. How is it a problem? Well let me tell you the next requirement we received for this service and the problem will become very clearly visible.

The next requirement we received for this service was to send email to network administrator's email ID for some specific set of error. Now, how will we do that? One idea is to create a class for sending emails and keeping its handle in the AppPoolWatcher but at any moment we will be using only one object either EventLogWriter or EmailSender.

The problem will get even worse when we have more actions to take selectively, like sending SMS. Then we will have to have one more class whose instance will be kept inside the AppPoolWatcher. The dependency inversion principle says that we need to decouple this system in such a way that the higher level modules i.e. the AppPoolWatcher in our case will depend on a simple abstraction and will use it. This abstraction will in turn will be mapped to some concrete class which will perform the actual operation. (Next we will see how this can be done)  

Inversion of Control  

Dependency inversion was a software design principle, it just states that how two modules should depend on each other. Now the question comes, how exactly we are going to do it? The answer is Inversion of control. Inversion of control is the actual mechanism using which we can make the higher level modules to depend on abstractions rather than concrete implementation of lower level modules.

So if I have to implement inversion of control in the above mentioned problem scenario, the first thing we need to do is to create an abstraction that the higher levels will depend on. So let us create an interface that will provide the abstraction to act on the notification received from AppPoolWacther.

C#
public interface INofificationAction
{
    public void ActOnNotification(string message);
}

Now let us change our higher level module i.e. the AppPoolWatcher to use this abstraction rather than the lower level concrete class.

C#
class AppPoolWatcher
{
    // Handle to EventLog writer to write to the logs
    INofificationAction action = null;

    // This function will be called when the app pool has problem
    public void Notify(string message)
    {
        if (action == null)
        {
            // Here we will map the abstraction i.e. interface to concrete class 
        }
        action.ActOnNotification(message);
    }
}

So how will our lower level concrete class will change? how will this class conform to the abstraction i.e. we need to implement the above interface in this class: 

C#
class EventLogWriter : INofificationAction
{   
    public void ActOnNotification(string message)
    {
        // Write to event log here
    }
}

So now if I need to have the concrete classes for sending email and sms, these classes will also implement the same interface. 

C#
class EmailSender : INofificationAction
{
    public void ActOnNotification(string message)
    {
        // Send email from here
    }
}

class SMSSender : INofificationAction
{
    public void ActOnNotification(string message)
    {
        // Send SMS from here
    }
}

So the final class design will look like: 

Image 1

So what we have done here is that, we have inverted the control to conform to dependency inversion principle. Now our high level modules are dependent only on abstractions and not the lower level concrete implementations, which is exactly what dependency inversion principle states.

But there is still one missing piece. When we look at the code of our AppPoolWatcher, we can see that it is using the abstraction i.e. interface but where exactly are we creating the concrete type and assigning it to this abstraction. To solve this problem, we can do something like: 

C#
class AppPoolWatcher
{
    // Handle to EventLog writer to write to the logs
    INofificationAction action = null;

    // This function will be called when the app pool has problem
    public void Notify(string message)
    {
        if (action == null)
        {
            // Here we will map the abstraction i.e. interface to concrete class 
            writer = new EventLogWriter();
        }
        action.ActOnNotification(message);
    }
}

But we are again back to where we have started. The concrete class creation is still inside the higher level class. Can we not make it totally decoupled so that even if we add new classes derived from INotificationAction, we don't have to change this class.

This is exactly where Dependency injection comes in picture. So its time to look at dependency injection in detail now.

Dependency Injection

Now that we know the dependency inversion principle and have seen the inversion of control methodology for implementing the dependency inversion principle, Dependency Injection is mainly for injecting the concrete implementation into a class that is using abstraction i.e. interface inside. The main idea of dependency injection is to reduce the coupling between classes and move the binding of abstraction and concrete implementation out of the dependent class.

Dependency injection can be done in three ways.

  1. Constructor injection
  2. Method injection
  3. Property injection

Constructor Injection 

In this approach we pass the object of the concrete class into the constructor of the dependent class. So what we need to do to implement this is to have a constructor in the dependent class that will take the concrete class object and assign it to the interface handle this class is using. So if we need to implement this for our AppPoolWatcher class:

C#
class AppPoolWatcher
{
    // Handle to EventLog writer to write to the logs
    INofificationAction action = null;

    public AppPoolWatcher(INofificationAction concreteImplementation)
    {
        this.action = concreteImplementation;
    }

    // This function will be called when the app pool has problem
    public void Notify(string message)
    {   
        action.ActOnNotification(message);
    }
}

In the above code, the constructor will take the concrete class object and bind it to the interface handle. So if we need to pass the EventLogWriter's concrete implementation into this class, all we need to do is

C#
EventLogWriter writer = new EventLogWriter();
AppPoolWatcher watcher = new AppPoolWatcher(writer);
watcher.Notify("Sample message to log");

Now if we want this class to send email or sms instead, all we need to do is to pass the object of the respective class in the

AppPoolWatcher
's constructor. This method is useful when we know that the instance of the dependent class will use the same concrete class for its entire lifetime.

Method Injection

In constructor injection we saw that the dependent class will use the same concrete class for its entire lifetime. Now if we need to pass separate concrete class on each invocation of the method, we have to pass the dependency in the method only.

So in method injection approach we pass the object of the concrete class into the method the dependent class which is actually invoking the action. So what we need to do to implement this is to have the action function also accept an argument for the concrete class object and assign it to the interface handle this class is using and invoke the action. So if we need to implement this for our AppPoolWatcher class:

C#
class AppPoolWatcher
{
    // Handle to EventLog writer to write to the logs
    INofificationAction action = null;

    // This function will be called when the app pool has problem
    public void Notify(INofificationAction concreteAction, string message)
    {
        this.action = concreteAction;
        action.ActOnNotification(message);
    }
}

In the above code the action method i.e. Notify will take the concrete class object and bind it to the interface handle. So if we need to pass the EventLogWriter's concrete implementation into this class, all we need to do is

C#
EventLogWriter writer = new EventLogWriter();
AppPoolWatcher watcher = new AppPoolWatcher();
watcher.Notify(writer, "Sample message to log");

Now if we want this class to send email or sms instead, all we need to do is to pass the object of the respective class in the AppPoolWatcher's invocation method i.e. Notify method in the above example.

Property Injection

Now we have discussed two scenarios where in constructor injection we knew that the dependent class will use one concrete class for the entire lifetime. The second approach is to use the method injection where we can pass the concrete class object in the action method itself. But what if the responsibility of selection of concrete class and invocation of method are in separate places. In such cases we need property injection. 

So in this approach we pass the object of the concrete class via a setter property that was exposed by the dependent class. So what we need to do to implement this is to have a Setter property or function in the dependent class that will take the concrete class object and assign it to the interface handle this class is using. So if we need to implement this for our AppPoolWatcher class:

C#
class AppPoolWatcher
{
    // Handle to EventLog writer to write to the logs
    INofificationAction action = null;

    public INofificationAction Action
    {
        get
        {
            return action;
        }
        set
        {
            action = value;
        }
    }

    // This function will be called when the app pool has problem
    public void Notify(string message)
    {   
        action.ActOnNotification(message);
    }
}

In the above code the setter of Action property will take the concrete class object and bind it to the interface handle. So if we need to pass the EventLogWriter's concrete implementation into this class, all we need to do is

C#
EventLogWriter writer = new EventLogWriter();
AppPoolWatcher watcher = new AppPoolWatcher();
// This can be done in some class
watcher.Action = writer;

// This can be done in some other class
watcher.Notify("Sample message to log");

Now if we want this class to send email or sms instead, all we need to do is to pass the object of the respective class in the setter exposed by AppPoolWatcher class. This approach is useful when the responsibility of selecting the concrete implementation and invoking the action are done in separate places/modules.

In languages where properties are not supported, there is a separate function to set the dependency. This approach is also known as setter injection. The important thing to note in this approach is that there is a possibility that someone has created the dependent class but no one has set the concrete class dependency yet. If we try to invoke the action in such cases then we should have either some default dependency mapped to the dependent class or have some mechanism to ensure that application will behave properly.

A Note on IoC Containers

Constructor injection is the mostly used approach when it comes to implementing the dependency injection. If we need to pass different dependencies on every method call then we use method injection. Property injection is used less frequently.

All the three approaches we have discussed for dependency injection are ok if we have only one level of dependency. But what if the concrete classes are also dependent of some other abstractions. So if we have chained and nested dependencies, implementing dependency injection will become quite complicated. That is where we can use IoC containers. IoC containers will help us to map the dependencies easily when we have chained or nested dependencies.

Point of interest

In this article we have discussed about Dependency inversion principle(Which is the D part of SOLID object oriented principle). We have also discussed how inversion of control is used to implement dependency inversion and finally we have seen how dependency injection help in creating decoupled classes and how to implement dependency injection. This article has been written from beginner's perspective. I hope this has been informative.

History

  • 03 July 2013: First version.
  • 09 July 2013: Typos and few grammatical mistakes fixed. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions

 
QuestionWhy not this? Pin
Toon Alfrink30-Jun-22 19:49
Toon Alfrink30-Jun-22 19:49 
BugCorrection: action = new EventLogWriter(); Pin
Ravi Kiran22-Feb-19 6:58
Ravi Kiran22-Feb-19 6:58 
PraiseGreat article, thanks Pin
shouket25-Nov-18 0:58
shouket25-Nov-18 0:58 
QuestionQuestion Pin
girishmeena17-Sep-18 6:56
girishmeena17-Sep-18 6:56 
QuestionI have a question Pin
User 1385261720-Jul-18 4:09
User 1385261720-Jul-18 4:09 
SuggestionProperty Injection Code Suggestion in MVC Controller? Pin
Member 1151033214-Dec-17 0:25
Member 1151033214-Dec-17 0:25 
Thumbs Up | :thumbsup:
Here is a code suggestion showing how to call the three methods.
If anyone can improve this and repost then I will delete this Comment.
The Interface classes should be moved to their own namespace.

For the Property Injection section, it uses the original approach and also adds a separate class into an MVC Index Get Action:

ass AppPoolWatcher
{
// Handle to EventLog writer to write to the logs
INofificationAction action = null;
public INofificationAction Action
{
get
{
return action;
}
set
{
action = value;
}
}
// This function will be called when the app pool has problem
public void Notify(string message)
{
action.ActOnNotification(message);
}
}

class Definer
{

public AppPoolWatcher injectMessageDependency()
{
AppPoolWatcher watcher = new AppPoolWatcher();
int stew = 2; // either passed in or derived!!
if (stew == 1)
{
EventLogWriter logWriter = new EventLogWriter();
watcher.Action = logWriter;
}
else if (stew == 2)
{
EmailSender emailWriter = new EmailSender();
watcher.Action = emailWriter;
}
else if (stew == 3)
{
SMSSender smsWriter = new SMSSender();
watcher.Action = smsWriter;
}

return watcher;

}
}

// GET: TestDeleteMes
public ActionResult Index()
{
// Remember that EventLogWriter, Email Sender an SMS Sender have all inherited
// from INofificationAction interface
//AppPoolWatcher sets the concrete class for an instance of INofificationAction
AppPoolWatcher watcher = new AppPoolWatcher();
Definer def = new Definer();
AppPoolWatcher watcher1 = def.injectMessageDependency();
int stew = 3;
if (stew == 1)
{
EventLogWriter logWriter = new EventLogWriter();
watcher.Action = logWriter;
}
else if (stew == 2)
{
EmailSender emailWriter = new EmailSender();
watcher.Action = emailWriter;
}
else if (stew == 3)
{
SMSSender smsWriter = new SMSSender();
watcher.Action = smsWriter;
}


// This can be done in some class
// watcher.Action = logWriter;
// This can be done in some other class
watcher.Notify("Sample message to log");
watcher1.Notify("Message to send");

return View(db.TestDeleteMes.ToList());
}
Praisescored 5. Suggestion here Pin
Member 1151033213-Dec-17 6:55
Member 1151033213-Dec-17 6:55 
PraiseOne Of The Best DIP Explanations Pin
Member 1350294812-Nov-17 0:05
Member 1350294812-Nov-17 0:05 
PraiseGreat Article Pin
moojjoo11-Oct-17 5:44
moojjoo11-Oct-17 5:44 
QuestionInversion of Control Containers Pin
Member 130315496-Sep-17 0:11
Member 130315496-Sep-17 0:11 
Generalnicely explained Pin
Mobeen Rashid23-Jun-17 23:02
Mobeen Rashid23-Jun-17 23:02 
PraiseBest Explanation Pin
Member 13238911-Jun-17 4:17
Member 13238911-Jun-17 4:17 
GeneralMy vote of 5 Pin
David Kinney4-Jan-17 2:44
David Kinney4-Jan-17 2:44 
QuestionWhat about both Send SMS and Email ? Pin
Member 1291599821-Dec-16 2:41
Member 1291599821-Dec-16 2:41 
AnswerRe: What about both Send SMS and Email ? Pin
Member 1037718727-Mar-17 3:45
Member 1037718727-Mar-17 3:45 
QuestionLucid explanation... Pin
Chandan Kumar Mishra18-Oct-16 19:23
Chandan Kumar Mishra18-Oct-16 19:23 
Questionclean explanation + Typo Pin
Dnto3-Oct-16 16:34
Dnto3-Oct-16 16:34 
QuestionClarify Pin
Nytz18-Sep-16 23:53
Nytz18-Sep-16 23:53 
QuestionThanks Bro...Finally I got it. Pin
Ramjane Khan18-Sep-16 0:54
Ramjane Khan18-Sep-16 0:54 
QuestionThanks Pin
bilal _hassan16-Aug-16 21:05
bilal _hassan16-Aug-16 21:05 
QuestionThanks Pin
Hansiepatil12-Jul-16 4:02
Hansiepatil12-Jul-16 4:02 
PraiseEasy to understand Pin
_eL5-Jul-16 0:04
_eL5-Jul-16 0:04 
QuestionVery Clear Pin
frikrishna12-Apr-16 13:02
frikrishna12-Apr-16 13:02 
Praiseexcellent article Pin
Member 93856177-Apr-16 5:11
Member 93856177-Apr-16 5:11 
GeneralMy vote of 5 Pin
chris lafave4-Apr-16 13:40
chris lafave4-Apr-16 13:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.