Inversion of Control Basics
Understand why and when to invert dependencies.
Hard-Coded Dependencies
When a class creates its own collaborators with new, it is tightly coupled to those concrete types. This makes the class hard to reconfigure and hard to test.
A Tightly Coupled Example
Here the service builds its own logger. There is no way to substitute a different logger without editing the class.
class ConsoleLogger { log(m: string) { console.log(m); } }
class OrderService {
private logger = new ConsoleLogger(); // hard-coded
place() { this.logger.log("order placed"); }
}
new OrderService().place();All lessons in this course
- Inversion of Control Basics
- Constructor Injection
- DI Containers with InversifyJS
- Type-Safe Service Tokens