Skip to content

Dependency Inversion

Medium — good to knowArchitecture

ELI5 — The Vibe Check

Dependency Inversion says high-level code shouldn't depend on low-level code — both should depend on abstractions. Your UserService shouldn't import PostgresDatabase directly. It should depend on a DatabaseInterface, and Postgres is just one thing that satisfies that interface.

Real Talk

The Dependency Inversion Principle states that high-level modules should not depend on low-level modules — both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions. Implemented through interfaces and Dependency Injection.

Show Me The Code

// Without DI: tightly coupled
class UserService {
  private db = new PostgresDatabase(); // hard dependency
}

// With DI: depends on abstraction
class UserService {
  constructor(private db: IDatabase) {} // injected
}

When You'll Hear This

"Apply Dependency Inversion so we can mock the database in tests." / "DIP is why we inject repositories instead of instantiating them."

Made with passive-aggressive love by manoga.digital. Powered by Claude.