Dependency Inversion in Practice
Break compile-time coupling between business logic and infrastructure by having both depend on an interface owned by the domain.
The Dependency Inversion Principle (DIP) is the mechanism behind Hexagonal, Onion, and Clean architectures: high-level business rules (PlaceOrderService) do not import low-level drivers (PostgresOrderRepo). Instead, both depend on an abstraction (OrderRepository) owned by the business layer.
The Problem: Concrete Imports Lock You In
In a standard layered setup, PlaceOrderService directly imports PostgresOrderRepo. Even if you pass it via constructor injection, the import statement at the top of the file still creates a compile-time dependency on Postgres, its types, and its error classes.
The Solution: Invert the Arrow Across the Boundary
Move the OrderRepository interface into the domain/business module, and make PostgresOrderRepo implement it from the infrastructure module. Source code imports now point from infrastructure toward the domain, while runtime control still flows from domain to Postgres.
Code: Before vs After DIP
// --- domain/order.ts (owns the abstraction) ---
export interface Order {
id: string;
totalCents: number;
}
export interface OrderRepository {
save(order: Order): Promise<void>;
}
export class PlaceOrderService {
constructor(private readonly repo: OrderRepository) {}
async execute(totalCents: number): Promise<Order> {
const order = { id: crypto.randomUUID(), totalCents };
await this.repo.save(order);
return order;
}
}
// --- infra/postgres-order-repo.ts (depends on domain/) ---
export class PostgresOrderRepo implements OrderRepository {
async save(order: Order): Promise<void> {
// INSERT INTO orders ...
void order;
}
}How DIP Relates to the Bridge Pattern
Notice how identical this structure is to the Bridge Pattern:
- Bridge separates an Abstraction hierarchy (
OrderExporter) from an Implementation hierarchy (StorageDriverinterface →PostgresDriver,CsvDriver) using composition. - Dependency Inversion applies that same composition-over-interface idea at architectural module boundaries (
PlaceOrderService→OrderRepository←PostgresOrderRepo).
- You want domain modules to compile and pass unit tests without importing database or HTTP packages.
- You need to swap Postgres for an in-memory fake in tests or migrate storage engines later.
- Creating a 1-to-1 interface for every utility class that has no external I/O or alternate implementation.
The OOP structural pattern that separates Abstraction from Implementation via an interface.
Applies DIP to every driving and driven boundary around the application core.
Shows what happens when dependencies point downward instead of being inverted.
See how Layered, Hexagonal, Onion, and Clean apply (or skip) DIP.
Check yourself
Check Yourself
1. If PlaceOrderService (in domain/) imports OrderRepository from infra/order-repository.ts, have you achieved Dependency Inversion?
2. After applying DIP, which direction does runtime control flow vs compile-time source dependency?
Sources
Layered vs Hexagonal vs Onion vs Clean
Compare the four major application architecture styles side by side using the same TypeScript Order service request.
Vertical Slice Architecture
Organize code by feature slice (request → handler → data) instead of horizontal technical layers, minimizing cross-feature coupling.