Overview Of Dependency Injection

Introduction

This article will help you to understand about dependency injection and also the real usage of making dependency injections. Everyone is following some or other software design patterns for better solutions and also the Unit test has become the more important aspect in software development.

Let’s see how the dependency injection is helping and improving the unit testing.

Dependencies

A software design pattern that allows removing hard-coded dependencies in the application development, it is an evolution of the Factory pattern and can be done by hand or using a framework.

Suppose that you are building a web application that is going to send e-mails to visitors that have entered a form. In object-oriented code, it is important to separate responsibilities. So you'll probably end up with a class that handles the form input (MyHandler) and a class that is responsible for sending the e-mails (SendMail).

dependency

Why to use DI?

Basically it’s like the “Business Layer” in three Tier Architecture. This can be used to control which data set is passed to the control logic (test data vs real database), or it can be used to control which classes used to implement your interfaces & so control behavior.

Dependency Injection (DI, wikipedia) is a design pattern that reduces hard-coded dependencies between your classes by injecting these dependencies at run-time, instead of during design-time. Technically, Dependency Injection is a mechanism that allows the implementation of another, more high-level, design pattern called Inversion of Control (IoC, wikipedia). The purpose of both patterns is to reduce hard-coded dependencies (or 'coupling') between your classes.

What's wrong in having dependencies Code?

You can't use multiple implementations of the class. It makes it easier to write code which creates issues during scaling of application, say deadlock kind of scenarios. It makes unit testing (nearly) impossible, which is 100 percent true.

Acronyms:

  • IoC - Inversion of Control

  • DIP - Dependency Inversion Principle
How to achieve the Dependency Injection?

We have two options for implementing the dependency injections into your software application, and have detailed as follows,
  1. Do it yourself

    • Pros - you have total control over your code & how it works.

    • Cons - you have to manually update & upgrade your code to stay current with the latest features & may not be standard.

    • Conclusion - may not be recommended for large projects.

  2. Use a framework – Ninject , Unity, Spring.NET.

Ninject:

It seems to be a very good candidate if the only thing you need for your project is DI.

Ninject uses a “kernel” with “fluent syntax” to bind classes to the interfaces they implement.

Pros – relatively simple, open source Dependency Injection Standard

  • Cons – weak XML support.

Unity:

Unity uses a “container” & XML data,

  • Pros – Strong XML support, works with PRISM for WPF development & Microsoft Public License (free)
  • Cons – relatively more complicated, obtrusive code.

Spring.NET:

Spring.NET - Probably the largest and most far reaching of the DI frameworks. Spring goes beyond DI and covers several other interesting grounds like.

Aspect Oriented Programming [AOP]

  • Pros - Somewhat simpler syntax, full XML support, very wide variety of features, free Apache license.

  • Cons - needs to be downloaded & installed to run on each system.
Conclusion

Dependency Injection is a difficult concept to grasp if you've never used it before. Just give it a try, and you'll see how flexible it makes your code. Especially when you're writing unit tests you'll quickly see the benefits. In that case, you can easily swap in mock implementations of dependencies. If anything, it makes your code a lot cleaner and sort of forces you to write better code.