s

dependency injection design pattern edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 04 September 2020 | 3394


In this article we will learn about Dependency Injection & will follow this up with one more article on how to implement dependency injection using Microsoft Unity Application Block.

Dependency Injection is a design pattern in which the dependency of the object is injected by the framework rather than created by the Object itself. Dependency injection is a software design pattern that implements inversion of control (IOC).

In case you are not familiar with IOC I would suggest you read this article "Inversion of Control (IOC) Pattern", Knowing IOC is a prerequisite for learning Dependency Injection as Dependency Injection is just one of the techniques to implement IOC ( Inversion of Control ).

Problem Statement

What is dependency, You have a class which depends on object of another calss for it to function. In the below example DataAccessLayer Class required an Object of MSSQL class to read data from an MS SQL database. DataAccessLayer Class has a dependency on object of MSSQL class to funtion. Basic question is what and how do I provide to the dependant class to make it to work ?.

What Is Dependency Injection

Literal meaning is to inject dependency. The Ability to supply ( Inject ) an external dependency into a software component.

What Do U Mean By Injecting Dependency?

In the above sample you are creating an instance of In the data access layer class which is wrong. What you are needed to do is to Inject the dependent object ( in this case object of MSSQL class ) to the class ( DataAccessLayer ) using the constructor from the outside.

The injection can be done using a constructor like showing in the above code or via a setter or Method.

Types Of Dependency Injection

    • Constructor
    • Setter
    • Method

Constructor Injection is the best and most widely used..

Advantages & Disadvantages

Advantages

    1. Loosely Coupled
    2. Much Better Testability
    3. Sepration Of Concerns
    4. Allows Use of IOC Container

Disadvantages

    1. Increase Code Complexity
    2. Difficult to Understand
    3. Complicates Debugging & Code Flow. Dependency injection can make code difficult to trace (read) because it separates behavior from construction. This means developers must refer to more files to follow how a system perform
    4. Requires more lines of code to accomplish the same behavior legacy code would
    5. Diminishes encapsulation by requiring users of a system to know how it works and not merely what it does

How To Construct The Dependency?

To construct dependencies of a class we have something called as the Dependency Injection Container. The container is a map of dependencies and logic to create the dependencies which are required by your class. When you ask for dependency the Container looks for a dependency in the collection, see if it's already created if the dependency is already created it will return that dependency, otherwise it will create it stored it, and then return the dependency. Here I am using singleton for the above. The main advantage of using a container is it can resolve complex dependencies transparently.