Skip to Content
Mengty Learns
Architecture

Dependency Inversion in Practice

Break compile-time coupling between business logic and infrastructure by having both depend on an interface owned by the domain.

explanationbeginnerTested with typescript 7.0.2Updated Nov 26, 2026
TL;DR

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.

Compile-time dependency is inverted: Infrastructure depends on the Domain interface.

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 (StorageDriver interface → PostgresDriver, CsvDriver) using composition.
  • Dependency Inversion applies that same composition-over-interface idea at architectural module boundaries (PlaceOrderService → OrderRepository ← PostgresOrderRepo).
When to use
  • 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.
When to avoid
  • Creating a 1-to-1 interface for every utility class that has no external I/O or alternate implementation.

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