Click here to Skip to main content
15,351,370 members
Articles / Programming Languages / C#
Technical Blog
Posted 13 May 2013


96 bookmarked

Dependency Injection (DI) vs. Inversion of Control (IOC)

Rate me:
Please Sign up or sign in to vote.
4.85/5 (73 votes)
31 Dec 2013CPOL3 min read
Dependency Injection (DI) vs. Inversion of Control (IOC).

The main goal of Inversion of control and Dependency Injection is to remove dependencies of an application. This makes the system more decoupled and maintainable.

First let’s try to understand IOC (Inversion of control). If you go back to old computer programming days, program flow used to run in its own control. For instance let’s consider a simple chat application flow as shown in the below flow diagram.

  1. End user sends chat message.
  2. Application waits for the message from the other end.
  3. If no message is found it goes to Step 2 or else moves to Step 4.
  4. Displays the message.
  5. User continues with his work ahead.

Image 1

Now if you analyze the program flow closely, it’s sequential. The program is in control of himself. Inversion of control means the program delegates control to someone else who will drive the flow. For instance if we make the chat application event based then the flow of the program will go something as below:-

  1. End user sends chat message.
  2. User continues with his work ahead.
  3. Application listens to events. If a message arrives event is activated and message is received and displayed.

Image 2

If you see the program flow it’s not sequential, its event based. So now the control is inverted. So rather than the internal program controlling the flow, events drive the program flow. Event flow approach is more flexible as their no direct invocation which leads to more flexibility.

A word of caution here, do not conclude that IOC are implemented by only events. You can delegate the control flow by callback delegates, observer pattern, events, DI (Dependency injection) and lot of other ways.

IOC (Inversion of control) is a general parent term while DI (Dependency injection) is a subset of IOC. IOC is a concept where the flow of application is inverted. So for example rather than the caller calling the method.


Will get replaced with an event based approach as shown below.

SomeObject.WhenEvent += Call();

In the above code the caller is exposing an event and when that event occurs he is taking action. It’s based on the Hollywood principle “Don’t call us we will call you”. In Hollywood when artists used to give auditions the judges would say them “Don’t call us we will call you”.

The above approach makes code more flexible as the caller is not aware of the object methods and the object is not aware of caller program flow.

Image 3

DI provides objects that an object needs. So rather than the dependencies construct themselves they are injected by some external means. For instance let’s say we have the following below class “Customer” who uses a “Logger” class to log errors. So rather than creating the “Logger” from within the class, you can inject the same via a constructor as shown in the below code snippet.

Image 4

The biggest benefit achieved by the above approach is “Decoupling”. You can now invoke the customer object and pass any kind of “Logger” object as shown in the below code.

Customer obj = new Customer(new EmailLogger());
Customer obj1 = new Customer(new EventViewerLogger());

So summarizing the differences. 

Inversion of control :- It’s a generic term and implemented in several ways (events, delegates etc).

Dependency injection :- DI is a subtype of IOC and is implemented by constructor injection, setter injection or method injection.

Via this blog i would like to inform all my friends i have started a series called as Learn c# and .NET in 60 days in youtube. So if you have some fresher friends who want to learn c# please talk about this initiative. 

Below is a nice video which demonstrates IOC ( Inversion of control) and how its is different from DI ( Dependency injection)

Image 5

For further reading do watch the below interview preparation videos and step by step video series.


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


About the Author

Comments and Discussions

PraiseApplause Pin
Akshay Kumar PY17-Mar-19 11:04
MemberAkshay Kumar PY17-Mar-19 11:04 
QuestionExcellent Pin
Apurv Prakash Purohit3-Jan-18 22:59
MemberApurv Prakash Purohit3-Jan-18 22:59 
GeneralMy vote of 5 Pin
Umesh Parshad14-Mar-17 4:22
MemberUmesh Parshad14-Mar-17 4:22 
QuestionSomeObject.WhenEvent += Call(); Pin
Member 1296224922-Jan-17 4:36
MemberMember 1296224922-Jan-17 4:36 
GeneralMy vote of 4 Pin
Manas_Kumar5-Dec-15 18:55
professionalManas_Kumar5-Dec-15 18:55 
PraiseNice artical Pin
陈金26-Oct-15 21:43
Member陈金26-Oct-15 21:43 
QuestionYou are gem!! Pin
bishtmanishsingh@gmail.com7-Dec-14 20:18
Memberbishtmanishsingh@gmail.com7-Dec-14 20:18 
GeneralSimple and neatly explained! Pin
S.Prateek8-Oct-14 3:49
MemberS.Prateek8-Oct-14 3:49 
GeneralMy vote of 1 Pin
singhswat17-Jun-14 14:33
Membersinghswat17-Jun-14 14:33 
GeneralMy vote of 5 Pin
DavidEKeller8-Jun-14 23:25
MemberDavidEKeller8-Jun-14 23:25 
Generalvery nice. Pin
mohd_muneer4-Jun-14 3:56
Membermohd_muneer4-Jun-14 3:56 
QuestionIoC vs DI vs SL Pin
Swab.Jat28-May-14 4:51
MemberSwab.Jat28-May-14 4:51 
QuestionIOC does not equal event driven programming Pin
Member 103011302-Jan-14 16:02
MemberMember 103011302-Jan-14 16:02 
AnswerRe: IOC does not equal event driven programming Pin
Coding Ninja18-Nov-14 17:09
MemberCoding Ninja18-Nov-14 17:09 
GeneralMy vote of 5 Pin
M Rayhan2-Jan-14 2:21
MemberM Rayhan2-Jan-14 2:21 
QuestionDependency Injection (DI) vs. Inversion of Control (IOC) Pin
HarithaB18-Oct-13 3:58
MemberHarithaB18-Oct-13 3:58 
SuggestionDependency Injection (DI) vs. Inversion of Control (IOC) Pin
RaviBattula13-May-13 17:30
professionalRaviBattula13-May-13 17:30 

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.