Article · Onion Architecture · Clean Architecture
Onion architecture: the domain model in the middle, infrastructure outside
Jeffrey Palermo's Onion Architecture: four tenets, the layers of the core (model, domain services, application services), the difference between a domain service and an application service, and code in C#, TypeScript, Java, Go and Python built around ordering flour.
Every Monday, someone at Halina's pierogi place worked out on a piece of paper how much flour to order from the wholesaler. After the third restaurant opened, the piece of paper stopped being enough. The new purchasing module is built with onion architecture: the domain model and the "how much flour for how many pierogi" rule in the middle, application services around them, and on the very outside the database, the email to the wholesaler and the web interface – all replaceable, like the skins of an onion.
- 2008
- Jeffrey Palermo's series of posts – "The Onion Architecture", parts 1–4
- 4
- tenets of onion architecture – according to Palermo's first post
- 0
- core references to infrastructure – EF Core, JPA, HTTP – only on the outside
Context and forces
- Rules are valuable, technology is temporary – the way flour demand is calculated will outlive three database changes and two wholesalers.
- Classic layers hurt – in the layered application, the logic depended on data access, and tests needed a database.
- Many integrations – the wholesaler takes orders by email, the mill through an API, and forecasts come from the orders module.
Jeffrey Palermo described onion architecture in 2008 precisely as an answer to the dependency problem of traditional layers. He didn't invent new techniques – he combined dependency inversion, the repository pattern and the DDD domain model into one clear picture.
Four tenets
Palermo summarised onion architecture in four tenets:
- The application is built around an independent object model (the domain).
- Inner layers define interfaces, outer layers implement them.
- The direction of coupling (dependencies) is toward the centre.
- All application core code can be compiled and run separately from infrastructure.
Structure
| Layer | What it contains | Example in purchasing |
|---|---|---|
| Domain model | Entities and value objects with behaviour | PurchaseOrder.ForFlour rounds up to 25 kg sacks |
| Domain services | Logic spanning many objects, stateless and without I/O | FlourDemandCalculator – how much flour the forecast pierogi need |
| Application services | Use case orchestration, transactions, interfaces the core needs | PlaceWeeklyOrder – fetch the forecast, calculate, save, send |
| Outer layer | Interface implementations, UI, tests | EF Core repository, email sending, controller, tests |
Domain service or application service? That's the most common question with the onion. A domain service contains a business rule that can't be assigned to a single entity ("how much flour does this forecast need") and does no I/O. An application service contains no rules – it coordinates: it fetches data through interfaces, calls the domain, saves the result and triggers side effects. If an if with business logic shows up in an application service, it probably belongs in the domain.
What it looks like in code
Structure in five stacks
src/
├── Purchasing.Core/ ← the whole core in one project (as in Palermo's posts)
│ ├── DomainModel/ ← PurchaseOrder, Supplier, Flour (kg) – innermost ring
│ ├── DomainServices/ ← FlourDemandCalculator, ISupplierCatalog – pure domain logic
│ └── ApplicationServices/ ← PlaceWeeklyOrder + IPurchaseOrders, ISupplierGateway
├── Purchasing.Infrastructure/ ← outer ring: EF Core, supplier e-mail/API adapter
├── Purchasing.Web/ ← outer ring: UI / HTTP, composition root
└── Purchasing.Tests/ ← outer ring too: tests are just another client of the corepl.pierogarnia.purchasing
├── core/
│ ├── model/ ← entities and value objects
│ ├── domainservice/ ← FlourDemandCalculator
│ └── application/ ← PlaceWeeklyOrder + ports
├── infrastructure/ ← JPA repositories, SupplierHttpGateway
└── web/ ← @RestController, Spring configurationsrc/
├── core/
│ ├── model/ ← purchase-order.ts, supplier.ts
│ ├── domain-services/ ← flour-demand.ts
│ └── application/ ← place-weekly-order.ts, ports.ts
├── infrastructure/ ← prisma-purchase-orders.ts, supplier-api.ts
└── web/ ← routes.ts, main.tspurchasing/
├── core/
│ ├── model/ ← package model
│ ├── demand/ ← package demand: domain service
│ └── app/ ← package app: application services + interfaces
├── infrastructure/ ← postgres, supplierapi
└── cmd/purchasing/main.gopurchasing/
├── core/
│ ├── model.py ← dataclasses
│ ├── domain_services.py ← flour demand calculation
│ └── application.py ← use cases + Protocol ports
├── infrastructure/ ← sqlalchemy_repo.py, supplier_api.py
└── web.py ← FastAPI routesThe rest of this article is for members
Create a free account or sign in to read it all. Still ahead:
- Onion, hexagon and Clean Architecture
- Trade-offs
- Migration path from a layered application
- In practice
- Anti-patterns
- Related topics
- Test yourself