Layered vs Hexagonal vs Onion vs Clean
Compare the four major application architecture styles side by side using the same TypeScript Order service request.
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:
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
Press Start or tap any step dot to trace the path.
▸All steps (6)
Comparison Table
| Dimension | Layered (N-Tier) | Hexagonal (Ports & Adapters) | Onion Architecture | Clean Architecture |
|---|---|---|---|---|
| Where business logic lives | Business / Service layer | Inside the Hexagon (Domain + Use cases) | Concentric Domain Model & Domain Services rings | Entities (enterprise rules) + Use Cases (application rules) |
| Dependency direction | Top-down (Presentation → Business → Data) | Inward (Adapters → Ports ← Core) | Strictly inward toward Domain Model | Strictly inward (Dependency Rule) |
| Testability | Medium — often needs DB or repository mocks | High — swap Postgres adapter for InMemory adapter | High — domain & app rings have zero infra imports | High — use cases test with gateway fakes |
| Typical folder layout | presentation/, business/, data/ | core/ports/, core/domain/, adapters/ | domain/, application/, infrastructure/, api/ | entities/, use-cases/, adapters/, frameworks/ |
| When to use | Simple CRUD apps, small teams, fast prototypes | Apps with multiple entry points or external integrations | Rich DDD domains where domain model stability matters | Large 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
OrderServiceis 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
Orderentity rules fromPlaceOrderuse-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
Related Notes
Clean Architecture
How Robert C. Martin's Clean Architecture separates Entities, Use Cases, Interface Adapters, and Frameworks using the Dependency Rule.
Dependency Inversion in Practice
Break compile-time coupling between business logic and infrastructure by having both depend on an interface owned by the domain.