Dependency injection is the polite way to stop your objects from being clingy. Instead of each object building its own sidekicks it receives required collaborators from the outside. That simple change buys you decoupling easier unit testing and a cleaner separation of responsibilities that even your future self might thank you for.
Think of dependency injection or DI as inversion of control with better manners. Rather than hiding how a dependency is created inside a class you move that responsibility to a single place often called a composition root. The result is code that is easier to swap mock or extend without rewriting the business logic.
This is the most common pattern. Required collaborators are passed into the constructor which tends to yield immutable dependencies and clear intent. If a class cannot be constructed without a dependency the constructor signature shouts that fact.
Use setters for optional collaborators or when you need to change the dependency after creation. Useful but it can make the object state less obvious at first glance.
Less common but handy when an abstraction needs to accept a dependency through an agreed contract. It is mostly a niche tool when plain constructor injection would be awkward.
class Logger {
log(message) {
console.log(message)
}
}
class UserService {
constructor(logger) {
this.logger = logger
}
createUser(name) {
this.logger.log('create ' + name)
}
}
const logger = new Logger()
const userService = new UserService(logger)
Here the composition root creates and wires the instances so UserService never has to know how to construct a Logger. That makes it trivial to inject a fake logger during unit testing or a more advanced logger in production.
Unit tests love dependency injection. Inject a spy when you want to assert behavior or a lightweight fake when you want to isolate logic. No need to touch production wiring for tests which keeps both code and pipelines sane.
Dependency injection is not a magic lamp. It will not fix bad design or lazy tests. What it will do is make decoupling explicit reduce friction when swapping implementations and make unit testing a lot less of a guessing game. If you want more testable modular software then DI is a tool worth learning and using with a little discipline.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.