Dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service). It Increases code reusability and improves code maintainability. It allows us to develop loosely coupled code and reduce tight coupling between software components.

DI is providing an object what is required at runtime. The Dependency Injection pattern uses a builder object to initialize objects and provide the required dependencies to the object means it allows you to “inject” a dependency from outside the class.  

Related to “Inversion of Control” (IOC). Flow of control is “inverted” by dependency injection because you have effectively delegated dependancies to some external system   

Benefits of using DI

  1. Helps in Unit testing.
  2. Boiler plate code is reduced, as initializing of dependencies is done by the injector component.
  3. Extending the application becomes easier.
  4. Helps to enable loose coupling, which is important in application programming.

Disadvantages of DI

  1. It’s a bit complex to learn, and if overused can lead to management issues and other problems.
  2. Many compile time errors are pushed to run-time.
  3. Dependency injection frameworks are implemented with reflection or dynamic programming. This can hinder use of IDE automation, such as “find references”, “show call hierarchy” and safe refactoring.

There are basically three types of dependency injection:

  1. constructor injection: the dependencies are provided through a class constructor.
  2. setter injection: the client exposes a setter method that the injector uses to inject the dependency.
  3. interface injection: the dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency.

Libraries and Frameworks that implement DI

Sources:

https://medium.freecodecamp.org/a-quick-intro-to-dependency-injection-what-it-is-and-when-to-use-it-7578c84fa88f

https://stackoverflow.com/questions/130794/what-is-dependency-injection

Last modified: March 22, 2019

Author

Comments

Write a Reply or Comment