Skip to Content
Mengty Learns
Architecture

Onion Architecture

How Onion Architecture organizes code into concentric rings around the Domain Model so all dependencies point inward.

explanationintermediateTested with typescript 7.0.2Updated Nov 26, 2026

TL;DR

Onion Architecture places the Domain Model at the center, surrounded by Domain Services, Application Services, and an outer ring of Infrastructure and UI. Source-code dependencies point strictly inward, so database drivers and HTTP frameworks depend on your core—never the other way around.

Intent

TL;DR

Keep business rules and domain state independent of databases, ORMs, and web frameworks by wrapping the core in concentric rings with an inward-only dependency rule.

Problem

In a traditional database-centric layered architecture, the domain and business logic depend directly on the data-access layer. Changing an ORM, upgrading a database driver, or testing order validation without a live database becomes slow and fragile because infrastructure concerns leak into core business rules.

The Dependency Rule: outer rings depend on inner rings; the Domain Model at the center depends on nothing.

Solution

Jeffrey Palermo introduced Onion Architecture around four key tenets:

  1. Domain Model (center): Entities (Order), value objects (OrderItem), and invariants live here with zero external imports.
  2. Domain Services: Interfaces such as OrderRepository and domain-level coordination that operate on domain objects.
  3. Application Services: Use-case orchestration (PlaceOrderAppService) that coordinates domain objects and calls repository interfaces.
  4. Infrastructure / UI (outermost ring): HTTP controllers, PostgreSQL repositories, message brokers, and test harnesses implement the inner interfaces and wire everything at startup.

Real-world analogy

▶ Flow: watch it work

Onion Architecture: Placing an Order

Overview of Onion Architecture: Placing an Order. 8 steps.
Overview8 steps in this flow

Press Start or tap any step dot to trace the path.

▸All steps (8)

Code

Using the shared Order service in TypeScript—notice how Order and OrderRepository have zero imports from Postgres or HTTP:

// --- Ring 1: Domain Model (src/domain/order.ts) ---
export interface OrderItem {
  sku: string;
  priceCents: number;
  qty: number;
}

export class Order {
  constructor(
    public readonly id: string,
    public readonly customerId: string,
    public readonly totalCents: number,
  ) {}

  static create(customerId: string, items: OrderItem[]): Order {
    if (items.length === 0) throw new Error('Order must contain at least one item');
    const totalCents = items.reduce((sum, i) => sum + i.priceCents * i.qty, 0);
    return new Order(crypto.randomUUID(), customerId, totalCents);
  }
}

// --- Ring 2: Domain Services / Interfaces (src/domain/order-repository.ts) ---
export interface OrderRepository {
  save(order: Order): Promise<Order>;
}

// --- Ring 3: Application Services (src/application/place-order.ts) ---
export class PlaceOrderAppService {
  constructor(private readonly repo: OrderRepository) {}

  async execute(customerId: string, items: OrderItem[]): Promise<Order> {
    const order = Order.create(customerId, items);
    return this.repo.save(order);
  }
}

// --- Ring 4: Infrastructure (src/infrastructure/postgres-order-repo.ts) ---
export class PostgresOrderRepository implements OrderRepository {
  async save(order: Order): Promise<Order> {
    // Runs SQL INSERT INTO orders ...
    return order;
  }
}

When to use / when to avoid

When to use
  • You are building a domain-rich application (DDD) where business invariants should outlive frameworks and databases.
  • You want fast unit tests on Application and Domain layers without booting a database.
When to avoid
  • Your app is simple CRUD with minimal business logic—Layered architecture is simpler with fewer indirections.
  • You are writing a quick script or prototype where ring boundaries add unnecessary boilerplate.

How to implement

Model the center first

Define entities and value objects (Order, OrderItem) with pure TypeScript and zero external imports.

Declare repository interfaces in Domain Services

Keep OrderRepository inside the core so Application Services never import SQL or ORM packages.

Implement Infrastructure in the outer ring

Write PostgresOrderRepository and HTTP controllers in outer folders, and wire them together at the composition root.

Pros / Cons

Pros
  • Business rules are completely isolated from databases, ORMs, and HTTP frameworks.
  • Easy to swap Postgres for an in-memory fake in unit tests via Dependency Inversion.
Cons
  • More files, interfaces, and DTO mapping than a straightforward 3-layer app.
  • Teams new to DDD sometimes blur Domain Services and Application Services.

Relations

Check yourself

Check Yourself

1. In Onion Architecture, where should the OrderRepository interface be defined?

2. What is allowed to import what under the Onion dependency rule?

Sources