0Pricing
TypeScript Academy · Lesson

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

  1. Inversion of Control Basics
  2. Constructor Injection
  3. DI Containers with InversifyJS
  4. Type-Safe Service Tokens
← Back to TypeScript Academy