Let’s start Dependency Injection with simple and literal meaning: “Injecting the dependencies.” The dependency is just another object that your class needs to function.
Suppose you want to make coffee. How would you do it and what do you need? You probably need to boil some water, a clean mug, coffee beans, sugar and milk (optional). In order to make coffee, you need to provide all these ingredients and manage all these yourself.
Let assume you can make coffee not yourself but simply by asking someone else to make it for you. Someone who makes coffee just the way you want. Would that be better than making the coffee yourself? If yes, then probably you already know about dependency injection.
Dependency injection is a software framework that creates objects for us without having to worry about how to create it and what ingredients to use. It pushes the object into the class that needs the object.
Technically dependency injection can be defined as:
Dependency injection is a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. The service is made part of the client’s state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.
With dependency injection, you are not required to write codes explicitly to supply dependencies but the software is responsible for supplying them as per the requirement.
Dependency injection decouples your classes construction with the construction of its dependencies, the reason that it’s so important is the dependency inversion principle. Basically, dependency inversion principle says that the code should depend upon abstractions, this decouples the implementations from each other, that way we can substitute different dependencies as long as they all satisfy the required interface. By using dependency injection we decouple the code from the lower level implementations making our code cleaner, easier to modify, easier to update and easier to reuse.
BENEFITS OF DEPENDENCY INJECTION
After going through about what is and why to use dependency injection, now you probably have a clear idea of how beneficial DI can be for you! Let’s have a look at some of the benefits of DI or dependency injection here:
Reduced component’s Dependencies
With dependency injection, you can easily reduce unnecessary dependencies between the components. As the component is weak to change therefore if a dependency changes, the component has to adapt the change for the sake of it. When you reduce component’s dependencies it makes the component less vulnerable to changes.
When you reduce a component’s dependencies, you may reuse it in different context easily. This is because dependencies are injected hence configures externally while increases the component’s reusability. In other words, components can easily be configured as per the requirements of the implementation whether there is different implementation needed in a different context or only different configuration with the same implementation. In both conditions, you don’t need to change any code.
Dependency injections are said to increase the testability of the components. When dependencies are injected into the components you might possibly inject mock implementation (configurable objects used for testing as a replacement for real implementation) of the dependencies.Mock objects also record of what methods have been invoked on them making the test easy to verify.
Dependency injections make the codes more readable by moving the dependencies to the interface so that you can see the component’s dependencies easily. That simply means all the codes are so visible in the interface that you do not need to look at all the dependencies to satisfy the need of a particular component.