What is Dependency Injection

What is Dependency Injection?

Written By
Categories
Published On
What is Dependency Injection

What is Dependency Injection?

Written By
Categories
Published On
Share:

Need of Dependency Injection

Traditional Approach of Creating Application:

We create application with multiple classes and we required to create new object of another class to perform specific operation in class. For example we have application in three layer architecture UI Layer, Business Layer & Data Access Layer and we required to create Data Access Layer class object in business layer class for accessing data with the “New” keyword by this way we create an application and application running smoothly without any issue

Issue with above Approach:

Solution:

Dependency injection Solve this problem, because it remove dependency between object and make object independent.

What is Dependency Injection?

For Ex:

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:

  1. Constructor
  2. Setter (Property)
  3. 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”.

Visual Studio new project window with Console Application (.NET Framework) selected and project named "DIProject" being created.

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

NuGet Package Manager in Visual Studio showing Microsoft Unity Framework package selected for installation in a .NET project.

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

Visual Studio code editor showing creation of a public Order class in the DIProject namespace for managing item orders in C#.

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

Visual Studio editor displaying the creation of IProducts interface in DIProject namespace with a PutOrder method definition.

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.

C# Order class in Visual Studio showing constructor injection of IProducts interface and method putTeaOrder calling PutOrder.

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

Unity container configuration in C# program for resolving dependencies by registering types and creating container object in Main method.

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

C# Unity container resolving dependency to create an Order object instance using Resolve method and assigning it to objOrder variable.

9. Now Calling PutTeaOrder Method of Order class.

C# code showing Unity container resolving Order object and calling PutTeaOrder method to execute logic from Order class.

10. Snapshot of entire configuration & calling method.

Complete Unity container setup in C# showing object registration, resolution, and method call within the Main method of a console app.
11. Now put debugger in Order class constructor & debug the program. you can find that the tea class object is resolve by Unity Container and assign to _products object variable.
Debug view of Order constructor showing Unity container resolving Tea class and assigning it to _products interface variable in C#.
12. When you run the program then you see the result like this.

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.

Visual Studio code showing Unity container registering IProducts type with multiple named implementations using Name Binding in C#.
2. Now make some changes in Order Class as mention below.
Modified Order class in Visual Studio injecting IUnityContainer and using string parameter to resolve IProducts implementation at runtime.
as you see that I declare IUnityContainer Interface to inject UnityContainer which help to Resolve register type IProducts at runtime. Also I added string parameter item in method which pass from program.cs for identify which item going to place order.
3. Also modify in Main method for calling PutTeaOrder with parameter as describe below.
Main method modified to call PutTeaOrder with a parameter to dynamically resolve product type using Unity Container in Visual Studio.
4. Now Debug the Program with putting debugger at PutTeaOrder method in order class.
Debugger set in PutTeaOrder method to dynamically resolve IProducts implementation from parameter using UnityContainer in C#.
Debugger in PutTeaOrder method shows UnityContainer resolving IProducts interface to specific type (Tea) based on string parameter.
5. As display above screen we can see that if we pass item as Tea then it will resolve Tea object same if we pass Coffee then it resolve Coffee object.

Dependency Injection Pros and Cons

Pros
Cons
Loosely Coupling
Increase code complexity
Increase Testability
Complicate debugging of code

Get the expert advice to grow your business digitally

    ×

    Table Of Content