top of page
Search
Writer's pictureAnvita Bajpai

Dependency Injection

Updated: Nov 28, 2020

Based on "the Hollywood Principle"



Dependency Injection in Real World Scenario Scenario 1


You work in an organization where you and your colleagues tend to travel a lot. Generally you travel by air and every time you need to catch a flight,


Things to do :

  1. Decide the destination, and desired arrival date and time

  2. Call up the airline agency and convey the necessary information to obtain a flight booking.

  3. Call up the cab agency, request for a cab to be able to catch a particular flight from say your residence

  4. Pickup the tickets, catch the cab and be on your way.

A Little Change Affects a Lot…

If the company suddenly changes the preferred agencies and their contact mechanisms.

Substantial amount of time getting spent in the readjustment process.


Scenario 2


Protocol changed : (Dependency Injected in real life)

An Administration Department takes complete care of your travel bookings.


The administration department did all the necessary adaptation in a manner that you do not need to do anything differently.


Changes do not affect all much :


No relearning required.

How Dependency Injection Makes a Difference ?


Impacts substantially for a larger chunk (Real Life Analogy)


That’s dependency injection in “real life”.


Imagine the cost with respect to :

A Single Person Vs. A Large Organization


The savings are likely to be substantial for the latter.



Dependency Injection in Software Context


The Conventional Approach (without DI)

In many scenarios, software components need to know :

  1. “which” components to communicate with,

  2. “where” to locate them, and

  3. “how” to communicate with them.

The mechanism to “locate” the services was often left to the clients and parts of the software also needed :

  1. To be aware of the dependencies between the various services themselves

  2. To implicitly work out the appropriate sequencing of component initialization

  3. To track and manage their life cycles.


DI Implementation


For implementing DI the way to structure the code is to :

  1. Have the clients declare their dependency on services

  2. Have some "external" piece of code assume the responsibility of locating and/or instantiating the services

  3. Simply supplying the relevant service references to the clients when needed.


A Few Popular DI Containers


The "external" piece of code referred to earlier is likely to be either hand coded or implemented using one of a variety of DI frameworks or ‘Containers’.


JAVA .NET


Google Guice S2Container.NET

Spring Spring.NET

PicoContainer PicoContainer.NET

NanoContainer StructureMap

Jice Ninject

LightDi Autofac

Soto Unity

Gravity ObjectBuilder

Excalibur Castle Windsor

Dagger SimpleInjector


Dependency Injection != using a DI container


Spaghetti Code (No DI & no DI container used):

The system is highly coupled, and it’s difficult to change a single unit of code without changes rippling throughout the system.


Service Locator Anti-Pattern (DI container used without DI):

With Dependency Injection, a component “asks” for its dependencies (typically by requiring them in the constructor) rather than “looking” for them (such as by querying a Service Locator / Registry / Context).


Manual Dependency Injection (DI used without a DI container):

DI at its core is about creating loosely coupled code by separating construction logic from application logic. ‘Manual’ in the name means that dependency creation isn’t automatically handled.


Enhanced Dependency Injection (DI using a DI container):

Doing Manual Dependency Injection or Enhanced Dependency Injection, almost all of your code looks *exactly* the same, differing only at the entry point(s).


Advantage of DI


Usage of Dependency Injection :

  1. requires just the declaration of the dependencies,

  2. lets the framework or the container work out the complexities of service

  3. takes care of instantiation, initialization, sequencing

  4. supplies the service references to the clients as required.


Another look at DI


Without DI :


With DI :


Dependencies at a Very High Level



Example of a Dependency



What problems do dependencies create?


  1. Code is tightly coupled

  2. Difficult to isolate when testing

  3. Difficult to test

  4. Difficult to maintain : Questions it arises

  • If I change Component X how do I know what else it will affect?

  • Did I break anything?


Types of Dependency Injection?


  1. Constructor (Most popular)

  2. Setter

  3. Method

Constructor Injection


Injecting a ICustomerRepository and a ICustomerDTOMapper through the constructor.

* Note: This is the most popular type of injection.

Setter Injection


Injecting a ICustomerRepository through the setter

Method Injection


Injecting a ICustomerRepository as well as an integer dependency through a method.

Dependency Injection Pros & Cons


Pros :

  1. Loosely Coupled

  2. Increases Testability (A LOT!)

  3. Separates components cleanly

  4. Allows for use of Inversion of Control Container


Cons :

  1. Increases code complexity

  2. Some Jr. Developers find it difficult to understand at First

  3. Can Complicate Debugging at First

  4. Complicates following Code Flow


References





491 views2 comments

2 Comments


Anvita Bajpai
Anvita Bajpai
Nov 27, 2020

Thanks a lot for your feedback and encouraging words Kunal. Would definitely think about incorporating suggestions from your feedback in my next blog.

Like

Kunal Chadha
Kunal Chadha
Nov 27, 2020

Good job Anvita! This is great content. You explained it quite clearly. I like the use of pictures that you did here to make your blog more visually engaging and easier to understand the concepts. I would suggest you to may be talk a little about the related concept of ‘Inversion of Control’.

Like

Copyright © 2020 TechTalksByAnvita

bottom of page