Skip to Content
Mengty Learns
Architecture

Layered vs Hexagonal vs Onion vs Clean

Compare the four major application architecture styles side by side using the same TypeScript Order service request.

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

Layered points dependencies downward toward the database. Hexagonal, Onion, and Clean all invert that bottom arrow so dependencies point inward toward the domain—they mainly differ in how they subdivide the core and name the boundaries.

Dependency Rule: Side-by-Side

The single biggest difference between Layered and the three domain-centric styles is which way the import arrows point:

Layered couples Business to Data; Hexagonal, Onion, and Clean invert that dependency so Domain owns the contract.

Flow: Same "Place Order" Request in All 4 Styles

Switch between the four variants below to trace how a POST /orders request travels through each architecture:

Place Order Request Across 4 Architecture Styles

Overview of Place Order Request Across 4 Architecture Styles. 6 steps.
Overview6 steps in this flow

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

▸All steps (6)

Comparison Table

DimensionLayered (N-Tier)Hexagonal (Ports & Adapters)Onion ArchitectureClean Architecture
Where business logic livesBusiness / Service layerInside the Hexagon (Domain + Use cases)Concentric Domain Model & Domain Services ringsEntities (enterprise rules) + Use Cases (application rules)
Dependency directionTop-down (Presentation → Business → Data)Inward (Adapters → Ports ← Core)Strictly inward toward Domain ModelStrictly inward (Dependency Rule)
TestabilityMedium — often needs DB or repository mocksHigh — swap Postgres adapter for InMemory adapterHigh — domain & app rings have zero infra importsHigh — use cases test with gateway fakes
Typical folder layoutpresentation/, business/, data/core/ports/, core/domain/, adapters/domain/, application/, infrastructure/, api/entities/, use-cases/, adapters/, frameworks/
When to useSimple CRUD apps, small teams, fast prototypesApps with multiple entry points or external integrationsRich DDD domains where domain model stability mattersLarge systems with strict use-case boundaries & teams

The Order Service Takeaway

In TypeScript, Hexagonal, Onion, and Clean share the exact same core mechanism: the Order domain defines an interface (OrderRepoPort / IOrderRepository / OrderGateway), and Postgres implements it in an outer folder.

  • Choose Layered when OrderService is mostly CRUD and extra interfaces are pure ceremony.
  • Choose Hexagonal when you want clear driving (REST, CLI, test) vs driven (Postgres, Stripe, InMemory) boundaries.
  • Choose Onion or Clean when your team wants explicit sub-layers inside the core (separating pure Order entity rules from PlaceOrder use-case orchestration).

Check yourself

Check Yourself

1. Which architecture styles enforce an inward Dependency Rule toward the domain?

2. Why might a small CRUD API choose Layered over Clean Architecture?

Sources