Dependency Injection (DI) and Inversion of Control (IoC) are both design principles used in object-oriented programming to decouple the execution of a task from its implementation, making the code more modular, testable, and maintainable.
Dependency Injection (DI)
Definition: Dependency Injection is a design pattern where an object receives its dependencies from an external source rather than creating them itself. Dependencies are provided to a class rather than being hardcoded within the class.
Types of Dependency Injection:
Constructor Injection: Dependencies are provided through a class constructor.
public class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
}
Setter Injection: Dependencies are provided through setter methods.
public class Car {
private Engine engine;
public void setEngine(Engine engine) {
this.engine = engine;
}
}
Interface Injection: The dependency provides an injector method that will inject the dependency into any client passed to it.
public interface EngineInjector {
void injectEngine(Car car);
}
public class Car {
private Engine engine;
public void setEngine(Engine engine) {
this.engine = engine;
}
}
Benefits of DI:
- Decoupling: Reduces the dependency between classes, making them easier to modify and test independently.
- Flexibility: Allows for different implementations of a dependency to be used interchangeably.
- Testability: Makes unit testing easier by allowing mock or stub dependencies to be injected.
Inversion of Control (IoC)
Definition: Inversion of Control is a broader design principle where the control of objects or portions of a program is transferred to a container or framework. IoC is a way to achieve loose coupling in software design.
IoC Containers: IoC containers are frameworks that manage the creation, configuration, and lifecycle of objects. They handle the injection of dependencies and can resolve dependencies automatically.
Examples of IoC Containers:
- Spring Framework (Java)
- Castle Windsor (C#)
- Unity Container (C#)
- Autofac (C#)
Benefits of IoC:
- Configuration Management: Centralizes the configuration of classes and their dependencies.
- Lifecycle Management: Manages the lifecycle of objects, ensuring proper initialization and disposal.
- Modularity: Promotes modular design by separating configuration from usage.
Relationship between DI and IoC:
- Dependency Injection is a specific form of Inversion of Control. While IoC refers to the general principle of giving control to an external entity, DI is a concrete mechanism to achieve IoC by injecting dependencies into objects.
- IoC containers often use DI to inject dependencies, managing the entire lifecycle of objects and their dependencies.
Summary
- Dependency Injection (DI): A design pattern where an object's dependencies are provided by an external source rather than being created internally.
- Inversion of Control (IoC): A broader principle where the control of program flow and dependencies is inverted, often managed by an IoC container.
- DI is a method to achieve IoC, with IoC containers providing the infrastructure to manage dependencies and object lifecycles.

Mariusz PostolPosted May 19, 2024, 7:11 PM
Injection via Service Locator => selection of an instance ( == factory programming pattern)Injection via == assigning to what is the reason to complicate simple and well-known things?
Mariusz PostolPosted May 19, 2024, 7:03 PM
It looks like you haven't mentioned polymorphism. I know it is a difficult word to write, pronounce, and explain. My point is that simplification is not the mother of simplicity !!! I am not sure what is a reason to use a special term to refer to object-oriented programming. What's new?
Mariusz PostolPosted May 19, 2024, 6:54 PM
You write "injects the event object" - why not assign an event instance?
Debra HodgesPosted Apr 18, 2024, 11:30 PM
I love this article! Thank you so much!
Mariusz PostolPosted Apr 29, 2023, 2:30 PM
"the object of the concrete class is passed to the constructor of the dependent class" - what is the difference with the object-oriented programming concept; what's new?
syed aliPosted Jun 28, 2022, 11:50 AM
great article
Amit GuptaPosted Nov 23, 2021, 10:31 AM
Better Explanation
Anil PomarPosted Aug 2, 2020, 1:04 PM
Very Cleanly explained.
Fahimeh BarzegarPosted Apr 10, 2020, 10:43 AM
Wow so nice
jeff finlayPosted Apr 24, 2019, 1:58 PM
Great articulation of the principles, patterns and why in a very concise manner! Thanks, very good! This helps with being able to talk about these patterns with others. I may write code this way, but this helps me to talk to others for how and why to write code like this.
Sagar JaybhayPosted Mar 26, 2019, 11:48 PM
Very Nice Explanation
umakant potdarPosted Mar 14, 2019, 2:31 AM
I like it...explained properly.
shivesh pandeyPosted Jan 16, 2019, 5:24 PM
You killed it...Good job Pradeep
Kaushik HalvadiaPosted Jul 4, 2018, 1:49 AM
Great and very easy explanation with example. a quick query under 4. Injection via Service Locator... first you have created an object of College class using College coll = new College(1); that is fine. and next statement is coll. GetEvents(); My question here is College class doesn't contains GetEvents method so it will give an error.. Please, correct me if I am wrong otherwise please update above example
Nilesh YadavPosted May 9, 2018, 9:48 AM
Nice and Simple explanation.. Loved it.
Vishal TiwatanePosted Jan 23, 2018, 6:14 AM
Very nice article..Thanks..
Kim DongJinPosted Jan 6, 2018, 11:31 AM
Simple and useful article.
Hamid KhanPosted Sep 21, 2017, 12:10 PM
Great.....................................
Khaja MoizuddinPosted Sep 9, 2017, 10:59 AM
Nicely written article for beginners like me.................. :) :)
sheetal kumarPosted Sep 4, 2017, 6:32 AM
Very Good Explanation Method use...Keep it up
babu sPosted Aug 31, 2017, 5:16 AM
Good explanation with example codes.
Armen KhachatryanPosted Aug 8, 2016, 3:47 AM
Thank you raj!
vijay sathePosted Mar 29, 2016, 4:40 AM
Very good article in a very simple words.
Praveen KumarPosted Mar 9, 2016, 11:38 PM
Very Nice!
Abhishek KumarPosted Mar 8, 2016, 10:47 AM
Nice article its very helpful
Sandeep SharmaPosted Dec 21, 2015, 6:41 AM
superb explanation. i was just looking this kind of article which has better way of understanding of DI and IOC. great job pradeep.!!!
Vijendra KushwahaPosted Dec 18, 2015, 2:37 PM
Awsome !! great article
Monishankar ChakrabortyPosted Nov 4, 2015, 4:35 AM
nice job ! Keep up the good work !
manju sainiPosted Apr 22, 2015, 5:26 AM
Good explain to understand ..... Dependency Injection (DI) and Inversion of Control (IOC)
Mohit SharmaPosted Aug 2, 2014, 5:50 AM
Your language and writing style had reduced the complexity of the Article. Now even Level II person can easily understand the concept.
Anupam SinghPosted Jul 16, 2014, 4:57 AM
excellent.. the way you explained Pradeep..
Mayank JainPosted Jul 15, 2014, 5:04 AM
nice article Pradeep
Former memberPosted Jul 15, 2014, 12:42 AM
Thanks.please give article on pros and cons of Dip,inversion of controls.How it affect performance of large projects?
Sukesh MarlaPosted Jul 15, 2014, 12:00 AM
Just a small correction if you don't mind.....Dependency Inversion and Dependency injection are two different things..Dependency Injection is a Technic for implementing IOC. You are talking about Dependency injection...and the beginning you specified that its dependency inversion....Please change it .....Nice article
Sukesh MarlaPosted Jul 14, 2014, 11:58 PM
one of the best article i read.....Nice moving pradeep..Keep up the good work
Akhil MittalPosted Jul 14, 2014, 8:02 AM
Nice article.Can you write an article explaining DI and IOC using Unity and Windsor containers each?
Jatin GadhiyaPosted Jul 14, 2014, 12:43 AM
very nice and awesome artical