Every beginner probably freaks out when they hear the word dependency injection. Truth is dependency injection brings the thoughts of needles and the experiences in the injection room. Most people i know don't like needles and the whole thought of being injected.
In these few episodes, i will make Dependency Injection less scary.
Dependency in English sense of the word means: to need (someone or something) for support, help, etc. : to depend on (someone or something)
We know what dependency is, we know what injecting is...check.
In Programming, dependency is another object that your class needs to function so if you have a sodaBottle class that fetches data from a database object we can say that your sodaBottle class has a dependency of that database object.
It is important to understand that Dependency Injection is a software design pattern.
Dependency Injection means to inject dependencies to our classes. This is a pattern, not a framework. Being a pattern, it is easier to adopt when starting out your App and not when you are in the middle of developing your app.(I mean you can, BUT it will be such a painful experience you may even give up doing it)
QUESTION: What does it mean to inject dependencies? Answer: To push the dependency into the class from the outside.
Let us digress a little to drive this point home in a visual aspect:
We have all been to restaurants(doesn't matter the size) and have a clue of what is there. I love cooking! I am planning on starting a restaurant because it is a dream i have had of starting one. To start it, I will not need to go to learn woodworking or be an electrician. All i will need to do is assemble the various components: think of a location for my restaurant, what kind of tables and chairs i will have, the cutlery(knives and spoons etc) and the amazing staff! I will get these from the various suppliers and people who provide these services. As i mentioned earlier: All i have to know is what i want, when i want it and where i want it.
Photo by Shawn Ang on Unsplash
Another example closer home to Tech: If you are building a custom desktop computer, the RAM, Hard Drive, Monitor, CPU case, Mother board...will be from different companies. You will combine all of them to make a cool functioning PC. The only skills you need to have here are : Searching for the components and Assembling the desktop. You do not need to manufacture or make the RAM then the Hard Drive....etc.
Photo by Andre Tan on Unsplash
That's exactly what dependency injection is. A method of resolving dependencies by creating an instance by an external independent object, not the used object, and passing it.
When we talk of Dependency Injection, we have to talk about decoupling: breaking down your code into various portions. That is: decoupling the construction of your classes from the construction of its dependencies.
It is important to note that Dependency Injection being a Design Pattern has the principle of dependency inversion at the root of it. Dependency Inversion is the principal that code should depend upon abstractions. By depending upon abstractions we're decoupling our implementations from each other.
In Object Oriented Programming we are always making classes and objects. And these objects are dependent on each other. The use of dependency injection makes our our code to be decoupled from the lower-level implementations hence making our code cleaner easier to modify and easier to reuse.
Hope that this general explanation on dependencies is clear!
Want to learn more about Dependency Injection: read on this series:
In the Next Episode we look at why Dependency Injection:(Technical difficulty increases here, But i got you! ;)
This is part of a series: Dependency Injection: The Lost Chronicles