Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Dependency inversion principle

Принцип инверсии зависимостей

Business rules should rely on a contract that says what they need, not on a specific database, library, or external service. The code that does the technical work follows that contract.

Also known as: DIP

What the principle means

The dependency inversion principle (DIP) is the D in SOLID. Take a checkout service that saves an order. The application decides whether the order is valid and when to save it. A database adapter knows how to write it. If checkout imports the adapter directly, the code that defines the business rules is tied to a particular way of storing data.

DIP has two parts: high-level modules and low-level modules should both depend on abstractions, and those abstractions should not depend on implementation details. Instead, the details should depend on the abstractions. In checkout, that means defining a contract around what the application needs: save an order, not execute a SQL statement or return a driver-specific result.

What gets inverted?

Without DIP, the checkout module depends on the PostgreSQL module. With DIP, checkout depends on an order storage contract defined on the application side of the boundary. The PostgreSQL adapter imports and implements that contract. The adapter still does the work at runtime, but the source-code dependency points inward: infrastructure knows about the application's contract, while the application doesn't know about the adapter. A diagram of method calls and a diagram of module imports can therefore show arrows pointing in different directions.

Example in TypeScript

// application/checkout.ts
export type Order = { id: string };
export interface OrderStore {
  save(order: Order): Promise<void>;
}

export class Checkout {
  constructor(private readonly store: OrderStore) {}

  async place(order: Order): Promise<void> {
    if (!order.id) throw new Error('Order ID is required');
    await this.store.save(order);
  }
}

At startup, the code that wires the application together can pass a PostgresOrderStore to Checkout. In a unit test, it can pass an in-memory store instead. That lets you check that an order without an ID is never saved without starting a database. You still need an integration test to verify that the PostgreSQL adapter writes the data correctly.

Where it helps and what to avoid

DIP is most useful at boundaries where business rules meet infrastructure, such as storage, payment providers, or message brokers. Clean and hexagonal architectures apply this idea to whole layers or modules. You don't need an interface for every helper class: add a contract where it keeps a meaningful technical detail out of the application. An interface that exposes SQL queries or driver-specific types has not achieved that separation.

Dependency injection (DI) is a way to supply a dependency; DIP is about what the code depends on. Passing a PostgresOrderStore into a constructor typed as PostgresOrderStore is injection, but checkout still depends on the concrete adapter. A DI container can wire up objects, but it cannot fix that dependency by itself.

How it comes up in interviews

An interviewer might show you a service that creates its own repository and ask you to refactor it. Describe the contract from the service's point of view, show which module owns it, and identify where the concrete adapter is selected. Then explain what you can test without the real database and what still needs an integration test.