Decoupling components is a mighty weapon against cluttered code bases and breaking changes. Load this weapon with a magazine of Dependency Injection (DI).
Therefore, DI enables you the replacement of dependencies without having to change the class that uses them. It can also reduce the risk for a change of a class just because one of its dependencies changed.
Changing components means having an interface that specifies the interaction contract between two components.
Having interfaces means creating objects inferring from the interface.
Rely on interfaces instead of concrete classes and you get interchangeability since both components must fulfill the interface’s contract.
That means DI bases on decoupling concrete classes from dependencies by relying on contracts (interfaces) and not on concrete classes.
The key to success is to ask for contracts (interfaces) without knowing the concrete type to get the interchangeability we are searching for.
To make this possible, the language you are using has to support a powerful type system and lets you use interfaces.
Since ECMAScript 2015 are two keywords usable:
extends. But both of them aren’t a thing compared to real object orientation.
Even if you are going to count this into object orientation, JS got no interfaces at all.
The Solution Is Already There
Dependency Injection solves a problem of object-oriented languages that just only exists because of the usage of a static type system.
Dependency Injection normally builds the fundament for the interchangeability of classes and is going to depict this via interfaces.
That means every passed object to a method has to fulfill the contract of the interface, otherwise, the program would crash.
Interfaces ensure this contract and the completeness of an object.
And we call this duck typing.
When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck. — James Whitcomb Rileys
So you simply pass a matching object to a component that needs, for example, a logger and it doesn’t matter if it formally matches the expected contract as long as it matches the task.
Handcrafted Dependency Injection
A simple approach would be to pass a function of the dependencies it needs as parameters or to register the components in a central location from where they can then be retrieved.
That corresponds then approximately to the beginning of a service locator, which is considered in object-oriented programming frequently as an unappreciated way of coding.
Finally, the much simpler and more pragmatic solution represents the frequently overdeveloped DI containers.
Once again, the KISS principle applies here.
If You Are Eager, Pick a Framework
The Angular framework tries to emulate a similar construct here, but this entails a surprisingly (or frighteningly) high degree of complexity and effort.
They built the entire DI into Angular’s module system. When you start your app, your root module wires up the dependencies you require throughout the app and allows you to defer some of this dependency resolution to feature modules.
For testing them, you do the same thing with a TestBed (this is just a module at a smaller scale), providing only the dependencies you need to run the tests.
If Angular isn’t your thing or you just can’t use it at all, there is another framework called InversifyJS. It provides a different way of configuring your dependencies, but the same level of control.
Using a framework for DI is a good thing since you can reduce the amount of boilerplate you’ll need to write.
A framework additionally supports you to maintain your code with less effort.
Dependency injection solves a problem in statically typed, object-oriented languages that would not exist if they were no static type system.
The concept is not a value being implemented at any cost.
Lessons and examples are booooring? Neither remember what you’ve coded in your last video course nor what you’ve learned? Be together with your favorite heroes and characters of your favorite Games, Movies, and TV Shows in my ArnoldCode Academy.
Adventures instead of dull tutorials in Full Stack Web and C# Development. Diploma Engineer, Freelancer & Founder of ArnoldCode Academy. Writing on Medium (arnoldcode.medium.com) and on Substack (https://arnoldcodefae.substack.com/?r=e07d1&utm_campaign=pub&utm_medium=web&utm_source=copy).