Dependency Inversion
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."
Related Terms
Dependency Injection
Instead of your UserService creating its own DatabaseConnection (tight coupling), you pass the database in from outside: new UserService(db).
Interface Segregation
Interface Segregation means don't force classes to implement methods they don't need.
Inversion of Control (IoC)
Inversion of Control is when a framework calls YOUR code instead of you calling the framework. You don't control the flow anymore — the framework does.
SOLID (SOLID)
SOLID is five rules for writing code that doesn't turn into a nightmare over time. Each letter stands for a different rule.