Need of Dependency Injection
Traditional Approach of Creating Application:
Issue with above Approach:
- 1. It is difficult to maintain over time (tightly Coupled)Over the time if Data Access class change its constructor at that time it break the Business Layer classes which are dependent on that class & it is hard to manage in every changes.
- 2. Hard to Unit TestIt is hard to mock Data Access layer classes in unit test case of Business layer classes & make it complex.
Solution:
What is Dependency Injection?


In above example we can see that in tightly coupled class are there. And if any changes happen in Item class Constructor then we need to change in Order class as well. And in dependency injection you no need to worry about any changes happen in Item Constructor it responsibility of Dependency Injection Container which create object of item class at time of order object create and so on it also take care if Item class also dependent on another class.
There are three types of dependencies:
- Constructor
- Setter (Property)
- Method
Note: In this blog I am showing Constructor Injection with Unity Framework (that is mostly widely used).
Dependency Injection Container
A Dependency Injection Container is an object that knows how to instantiate and configure objects. And to be able to do its job, it needs to know about the constructor arguments and the relationships between the objects. It is responsible for create new object and return whenever it required or requested.
Implementation of Dependency Injection with example
I am going to create one console application by following steps.
1. Create Console Application in visual studio by creating New Project with name “DIProject”.

2. Adding the Reference of Microsoft Unity Framework using Nuget Package Manager.

3. Now I am adding Order Class which is responsible for putting Order of Item.

4. Create IProducts Interface which is implemented by item classes.

5. Same as add 2 Item Class Tea and Coffee both Implemented IProducts interface.


6. Now In Order Constructor we inject IProduct interface and create putTeaOrder method.

7. Now we need to configure Unity Container so we can it provide object at time of resolve Interface.

8. Now resolve dependency from container by using Resolve method of container as mention below for Order.

9. Now Calling PutTeaOrder Method of Order class.

10. Snapshot of entire configuration & calling method.



Now you have clear idea about Dependency Injection & how it configurable, And Order Class behave as independent class if any one change constructor of Tea class then also no need to change Order Class as all thing manage by Unity Container.
Register Single type with multiple Classes & Runtime Resolve Object
Continue with above example, have you notice that I implement IProducts Interface in two classes Tea and Coffee but I use only one Tea. Now we change requirement that we need to add one more product Coffee required to add in order. For this we already created class Coffee.
1. Register Type single type with two classes as mention below, it also call Name Binding.






Dependency Injection Pros and Cons
Pros | Cons |
---|---|
Loosely Coupling |
Increase code complexity |
Increase Testability |
Complicate debugging of code |