Design Pattern, OOPS

Dependency Injection

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:    

  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 Test

    It is hard to mock Data Access layer classes in unit test case of Business layer classes & make it complex.

Solution:

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

What is Dependency Injection?

Dependency Injection is technique (Design Pattern) which helps to inject dependent object of a class. And make class independent

For Ex:

Dependency Injection
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:
  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”

Implementation of Dependency Injection

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

Implementation of Dependency Injection

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

Implementation of Dependency Injection

4. Create IProducts Interface which is implemented by item classes

Implementation of Dependency Injection

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

Implementation of Dependency Injection
Implementation of Dependency Injection

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

Implementation of Dependency Injection

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

Implementation of Dependency Injection

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

Implementation of Dependency Injection

9. Now Calling PutTeaOrder Method of Order class

Implementation of Dependency Injection

10. Snapshot of entire configuration & calling method

Implementation of Dependency Injection

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

Implementation of Dependency Injection

12. When you run the program then you see the result like this

Implementation of Dependency Injection

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

Register Single type with multiple Classes & Runtime Resolve Object

2. Now make some changes in Order Class as mention below

Register Single type with multiple Classes & Runtime Resolve Object

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

Register Single type with multiple Classes & Runtime Resolve Object

4. Now Debug the Program with putting debugger at PutTeaOrder method in order class

Register Single type with multiple Classes & Runtime Resolve Object
Register Single type with multiple Classes & Runtime Resolve Object
Register Single type with multiple Classes & Runtime Resolve Object

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

Register Single type with multiple Classes & Runtime Resolve Object
Register Single type with multiple Classes & Runtime Resolve Object
Register Single type with multiple Classes & Runtime Resolve Object
Register Single type with multiple Classes & Runtime Resolve Object
Dependency Injection Pros and Cons
Pros
  • Loosely Coupling
  • Increase Testability
Cons
  • Increase code complexity
  • Complicate debugging of code.
About Triveni

Triveni is such strong and trustworthy name in the field of customised IT solution provider which is earnest name in Indian IT sector.

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

Privacy Settings
We use cookies to enhance your experience while using our website. If you are using our Services via a browser you can restrict, block or remove cookies through your web browser settings. We also use content and scripts from third parties that may use tracking technologies. You can selectively provide your consent below to allow such third party embeds. For complete information about the cookies we use, data we collect and how we process them, please check our Privacy Policy
Youtube
Consent to display content from Youtube
Vimeo
Consent to display content from Vimeo
Google Maps
Consent to display content from Google
Spotify
Consent to display content from Spotify
Sound Cloud
Consent to display content from Sound