Let say we need to make some Coffee. How would we do it and what do we require? We have to heat up some water, a mug, coffee sachets, sugar, and milk. With a specific end goal to make espresso we have to get all these ingredients and we have to handle all these ourselves in specific order. We need to fill the pot with water and heat it thoroughly and get the rest done. Now assume that we can make espresso, by just approaching another person to do it for us. Somebody that knows how to do the espresso simply the way we need it. Would that be better than making by us? If yes, then welcome to dependency injection. Yes, in technical term instead of creating or finding an object or service in your class, it’s handed to you as a dependency and reduces the load and improving performance of your web application.
Dependency injection is a technique that deals with creating objects for us without worrying about giving the right ingredients so to say. It maintains the dependencies between objects by creating the dependent objects outside the object which uses that dependency. Particularly, instead of objects configuring themselves they are configured by an external substance.
The principle of DI states that high-level or low-level modules should not rely on each other, rather they should rely on abstractions. For an instance, Class A can have attributes B and methods. Those characteristics are again occurrences of another Class C. If Class A wants to work and execute its objective, attributes B should be initiated. There are diverse approaches to initiating an object. A simple and direct way is to use the “new” operator and call the constructor of Class C where we require that instance in Class A. The Class A has absolute control over making of attribute B. That decides when and where to call Class C.
Dependency Injection is principally for injecting the concrete execution into a class that is using abstraction. The major concept of dependency injection is to reduce the coupling amongst classes and substitute the concrete implementation and abstraction and taking them out of the dependent class. Due to this specific quality it is widely used in modern Web & enterprise software development. Dependency injection can be done in three different ways.
In this methodology, you can call the object of the concrete class into the dependent class’s constructor. So what you need to do to have a constructor, it is to implement Constructor injection in the dependent class which will take the concrete class object and allocate it to the interface handle which the class is using.
In Interface injection mode you can pass the object of the concrete class into the method of the dependent class which is requesting the action. So for having action function, you have to execute Interface injection into a concrete class object and allocate it to the interface handle which the class is using and request the action.
You can pass the object of the concrete class through a setter property which is called by the dependent class. If you need to implement Setter injection in your project you need Setter property or function in the dependent class that will take the concrete class object and allocate it to the interface handle which the class is using.
Why Dependency Injection is important
Each dependency can be produced exclusively; the main limitation is the interface it must implement, which has to be agreed before. This permits isolating the work between developers, maintaining a strategic distance from issues at integration time.
With Dependency Injection, you target promptly the piece of affected code for changes, and can make sure that the changes will be done only in that affected code.
Dependency Injection makes you design unique pieces of code of for a particular dependency which can be reused as many times as you need.
Without DI, if we are testing a part of the program that is utilizing another segment, and which is not being injected than it gets time consuming. In the majority of the cases, it will happen such that we would be testing same things several times. With Dependency Injection, you can easily simulate the dependencies to test in better and faster way.
Sample example of Dependency injection in PHP
We can exhibit the concept with a basic, yet precise example.
Without dependency injection
You want to use the OpenStreetMap rather than Maps, how would you do? You need to change the code of Store, and the various classes that use Maps.
The classic way of doing things is:
Without dependency injection, your classes are tightly coupled to their dependencies.
By using dependency injection:
The Store now uses dependency injection as shown
And the services are defined using an interface:
Now, it is for the user of the Store to decide which implementation to use. And it can be changed anytime, without having to rewrite the Store.
The Store is no longer tightly coupled to its dependency.
Instead of writing:
You can write:
And configure which GeolocationService DI should automatically inject in Store through configuration:
Dependency Injection usage is currently pretty much standard in all cutting edge application environments aimed to support the quick development of web applications and data frameworks. Support of Dependency Injection is most widespread in frameworks available these days for developers using PHP platform. The developers can effectively use it in support for the creation of reusable segments of application layer within the web framework’s architecture. Regardless of higher computing demands, the utilization of Dependency Injection implies a significant movement in the quality of the object code of web solutions.