{"id":"6b08bd3c80884893657cc9e7","slug":"onion-architecture","title":"Onion architecture: the domain model in the middle, infrastructure outside","summary":"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.","servedLang":"en","availableLangs":["pl","en"],"topics":["onion-architecture","clean-architecture","hexagonal-architecture"],"techs":["dotnet","java","typescript","go","python"],"tags":["architecture","onion","domain-services","application-services","dependency-inversion"],"level":"intermediate","kind":"article","access":"members","status":"published","cover":null,"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"readingMinutes":11,"stats":{"views":3,"likes":0,"bookmarks":0,"comments":0},"publishedAt":"2026-11-26T23:14:04.385Z","updatedAt":"2026-11-27T13:14:37.232Z","requestedLang":"en","theme":{"hero":"classic","heroMedia":null,"accent":"teal","background":"tint","weaveResources":true,"toc":"right"},"seoDescription":"Onion Architecture: Jeffrey Palermo's 2008 tenets, domain model, domain and application services, infrastructure on the outside, examples in C#, TypeScript, Java, Go and Python, comparison with hexagonal and Clean Architecture.","wordCount":2114,"doc":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"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 "},{"type":"text","text":"onion architecture","marks":[{"type":"bold"}]},{"type":"text","text":": 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."}]},{"type":"callout","attrs":{"variant":"tip","title":"In short"},"content":[{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"The core is the "},{"type":"text","text":"domain model","marks":[{"type":"bold"}]},{"type":"text","text":" surrounded by layers of "},{"type":"text","text":"domain services","marks":[{"type":"bold"}]},{"type":"text","text":" and "},{"type":"text","text":"application services","marks":[{"type":"bold"}]},{"type":"text","text":"; infrastructure, UI and tests sit outside."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Inner layers define interfaces, outer layers implement them","marks":[{"type":"bold"}]},{"type":"text","text":" – dependencies point inwards."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"The whole core can be "},{"type":"text","text":"compiled and run without infrastructure","marks":[{"type":"bold"}]},{"type":"text","text":" – a practical test of whether the onion is real."}]}]}]}]},{"type":"stats","attrs":{"items":[{"value":"2008","label":"Jeffrey Palermo's series of posts","hint":"\"The Onion Architecture\", parts 1–4"},{"value":"4","label":"tenets of onion architecture","hint":"according to Palermo's first post"},{"value":"0","label":"core references to infrastructure","hint":"EF Core, JPA, HTTP – only on the outside"}]}},{"type":"heading","attrs":{"level":2,"id":"context-and-forces"},"content":[{"type":"text","text":"Context and forces"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Rules are valuable, technology is temporary","marks":[{"type":"bold"}]},{"type":"text","text":" – the way flour demand is calculated will outlive three database changes and two wholesalers."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Classic layers hurt","marks":[{"type":"bold"}]},{"type":"text","text":" – in the layered application, the logic depended on data access, and tests needed a database."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Many integrations","marks":[{"type":"bold"}]},{"type":"text","text":" – the wholesaler takes orders by email, the mill through an API, and forecasts come from the orders module."}]}]}]},{"type":"paragraph","content":[{"type":"text","text":"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."}]},{"type":"heading","attrs":{"level":2,"id":"four-tenets"},"content":[{"type":"text","text":"Four tenets"}]},{"type":"paragraph","content":[{"type":"text","text":"Palermo summarised onion architecture in four tenets:"}]},{"type":"orderedList","attrs":{"start":1},"content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"The application is built around an "},{"type":"text","text":"independent object model","marks":[{"type":"bold"}]},{"type":"text","text":" (the domain)."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"Inner layers define interfaces, outer layers implement them.","marks":[{"type":"bold"}]}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"The direction of coupling (dependencies) is "},{"type":"text","text":"toward the centre","marks":[{"type":"bold"}]},{"type":"text","text":"."}]}]},{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"All application core code can be "},{"type":"text","text":"compiled and run separately from infrastructure","marks":[{"type":"bold"}]},{"type":"text","text":"."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"structure"},"content":[{"type":"text","text":"Structure"}]},{"type":"diagram","attrs":{"kind":"mermaid","code":"flowchart TB\n  subgraph outer[\"Outer layer: UI · infrastructure · tests\"]\n    subgraph app[\"Application services: PlaceWeeklyOrder\"]\n      subgraph ds[\"Domain services: FlourDemandCalculator\"]\n        dm([\"Domain model: PurchaseOrder, Supplier, Kilograms\"])\n      end\n    end\n  end\n  infra[\"EF Core / JPA / Prisma\"] -.-> outer\n  mail[\"Wholesaler email and API\"] -.-> outer\n  web[\"HTTP, purchasing panel\"] -.-> outer","caption":"The layers of the onion – from the skin to the centre"}},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Layer"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"What it contains"}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Example in purchasing"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Domain model"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Entities and value objects with behaviour"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"PurchaseOrder.ForFlour","marks":[{"type":"code"}]},{"type":"text","text":" rounds up to 25 kg sacks"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Domain services"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Logic spanning many objects, stateless and without I/O"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"FlourDemandCalculator","marks":[{"type":"code"}]},{"type":"text","text":" – how much flour the forecast pierogi need"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Application services"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Use case orchestration, transactions, interfaces the core needs"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"PlaceWeeklyOrder","marks":[{"type":"code"}]},{"type":"text","text":" – fetch the forecast, calculate, save, send"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Outer layer"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Interface implementations, UI, tests"}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"EF Core repository, email sending, controller, tests"}]}]}]}]},{"type":"paragraph","content":[{"type":"text","text":"Domain service or application service?","marks":[{"type":"bold"}]},{"type":"text","text":" 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 "},{"type":"text","text":"if","marks":[{"type":"code"}]},{"type":"text","text":" with business logic shows up in an application service, it probably belongs in the domain."}]},{"type":"heading","attrs":{"level":2,"id":"what-it-looks-like-in-code"},"content":[{"type":"text","text":"What it looks like in code"}]},{"type":"codeGroup","attrs":{"title":"Structure in five stacks"},"content":[{"type":"codeBlock","attrs":{"language":"plaintext","filename":"dotnet/solution-tree.txt"},"content":[{"type":"text","text":"src/\n├── Purchasing.Core/                 ← the whole core in one project (as in Palermo's posts)\n│   ├── DomainModel/                 ← PurchaseOrder, Supplier, Flour (kg)       – innermost ring\n│   ├── DomainServices/              ← FlourDemandCalculator, ISupplierCatalog   – pure domain logic\n│   └── ApplicationServices/         ← PlaceWeeklyOrder + IPurchaseOrders, ISupplierGateway\n├── Purchasing.Infrastructure/       ← outer ring: EF Core, supplier e-mail/API adapter\n├── Purchasing.Web/                  ← outer ring: UI / HTTP, composition root\n└── Purchasing.Tests/                ← outer ring too: tests are just another client of the core"}]},{"type":"codeBlock","attrs":{"language":"plaintext","filename":"java/package-tree.txt"},"content":[{"type":"text","text":"pl.pierogarnia.purchasing\n├── core/\n│   ├── model/                       ← entities and value objects\n│   ├── domainservice/               ← FlourDemandCalculator\n│   └── application/                 ← PlaceWeeklyOrder + ports\n├── infrastructure/                  ← JPA repositories, SupplierHttpGateway\n└── web/                             ← @RestController, Spring configuration"}]},{"type":"codeBlock","attrs":{"language":"plaintext","filename":"node/src-tree.txt"},"content":[{"type":"text","text":"src/\n├── core/\n│   ├── model/                       ← purchase-order.ts, supplier.ts\n│   ├── domain-services/             ← flour-demand.ts\n│   └── application/                 ← place-weekly-order.ts, ports.ts\n├── infrastructure/                  ← prisma-purchase-orders.ts, supplier-api.ts\n└── web/                             ← routes.ts, main.ts"}]},{"type":"codeBlock","attrs":{"language":"plaintext","filename":"go/module-tree.txt"},"content":[{"type":"text","text":"purchasing/\n├── core/\n│   ├── model/                       ← package model\n│   ├── demand/                      ← package demand: domain service\n│   └── app/                         ← package app: application services + interfaces\n├── infrastructure/                  ← postgres, supplierapi\n└── cmd/purchasing/main.go"}]},{"type":"codeBlock","attrs":{"language":"plaintext","filename":"python/package-tree.txt"},"content":[{"type":"text","text":"purchasing/\n├── core/\n│   ├── model.py                     ← dataclasses\n│   ├── domain_services.py           ← flour demand calculation\n│   └── application.py               ← use cases + Protocol ports\n├── infrastructure/                  ← sqlalchemy_repo.py, supplier_api.py\n└── web.py                           ← FastAPI routes"}]}]}]},"toc":[{"id":"context-and-forces","text":"Context and forces","level":2,"locked":false},{"id":"four-tenets","text":"Four tenets","level":2,"locked":false},{"id":"structure","text":"Structure","level":2,"locked":false},{"id":"what-it-looks-like-in-code","text":"What it looks like in code","level":2,"locked":false},{"id":"onion-hexagon-and-clean-architecture","text":"Onion, hexagon and Clean Architecture","level":2,"locked":true},{"id":"trade-offs","text":"Trade-offs","level":2,"locked":true},{"id":"migration-path-from-a-layered-application","text":"Migration path from a layered application","level":2,"locked":true},{"id":"in-practice","text":"In practice","level":2,"locked":true},{"id":"anti-patterns","text":"Anti-patterns","level":2,"locked":true},{"id":"related-topics","text":"Related topics","level":2,"locked":true},{"id":"test-yourself","text":"Test yourself","level":2,"locked":true}],"locked":true,"paywall":null,"previewRatio":0.3,"totalBlocks":38,"visibleBlocks":15,"resources":[{"id":"6b0857be3c96f2d34d0b41df","type":"article","provider":"web","url":"https://jeffreypalermo.com/2008/07/the-onion-architecture-part-1","embedUrl":null,"title":"The Onion Architecture : part 1","description":"This is part 1. part 2. part 3. part 4. My feed (rss). I’ve spoken several times about a specific type of architecture I call “Onion Architecture”. I’ve found that it lea…","author":"Jeffrey Palermo","thumbnailUrl":"https://jeffreypalermo.com/wp-content/uploads/2018/06/image257b0257d255b61255d1.png","durationSec":null,"lang":"en","topics":["onion-architecture"],"techs":["general"],"tags":["software-architecture"],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"manual","publishedAt":"2008-07-29T08:08:44.000Z","popularity":80,"verifiedAt":"2026-11-26T11:25:57.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:00:59.965Z","updatedAt":"2026-11-26T16:00:59.965Z"},{"id":"6b0857a73c96f2d34d0b322c","type":"article","provider":"web","url":"https://herbertograca.com/2017/11/16/explicit-architecture-01-ddd-hexagonal-onion-clean-cqrs-how-i-put-it-all-together/","embedUrl":null,"title":"DDD, Hexagonal, Onion, Clean, CQRS, … How I put it all together","description":"","author":"Herberto Graça","thumbnailUrl":"https://i0.wp.com/herbertograca.com/wp-content/uploads/2018/11/100-explicit-architecture-svg.png?fit=1200%2C821&ssl=1","durationSec":null,"lang":"en","topics":["hexagonal-architecture","onion-architecture","clean-architecture","cqrs"],"techs":["general"],"tags":[],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"seed","publishedAt":null,"popularity":0,"verifiedAt":"2026-11-26T00:00:00.000Z","curated":true,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:00:38.995Z","updatedAt":"2026-11-26T16:00:38.995Z"},{"id":"6b0857b33c96f2d34d0b36a3","type":"repo","provider":"github","url":"https://github.com/iammukeshm/OnionArchitecture","embedUrl":null,"title":"iammukeshm/OnionArchitecture","description":"The onion architecture, introduced by Jeffrey Palermo, overcomes the issues of the layered architecture with great ease. With Onion Architecture, the game-changer is that the Domain Layer (Entities and Validation Rules that are common to the business case ) is at the Core of the Entire…","author":"iammukeshm","thumbnailUrl":"https://opengraph.githubassets.com/1/iammukeshm/OnionArchitecture","durationSec":null,"lang":"en","topics":["onion-architecture","clean-architecture","cqrs"],"techs":["dotnet"],"tags":["asp-net-core","clean-architecture","cqrs-pattern","onion-architecture","webapi","software-architecture"],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"github","publishedAt":"2020-06-17T18:27:14.000Z","popularity":53,"verifiedAt":"2026-11-26T10:57:56.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:00:51.220Z","updatedAt":"2026-11-26T16:00:51.220Z"},{"id":"6b0857c03c96f2d34d0b4415","type":"presentation","provider":"web","url":"https://speakerdeck.com/olivergierke/domain-centric-why-hexagonal-onion-and-clean-architecture-are-answers-to-the-wrong-question","embedUrl":null,"title":"Domain-centric? Why Hexagonal and Onion Architecture Are Answers to the Wrong Question","description":"To separate technical from domain code, architectural approaches like Hexagonal or Onion Architecture are currently all the rage. However, discussions a…","author":"Oliver Drotbohm","thumbnailUrl":"https://files.speakerdeck.com/presentations/d67b0337797346c6b0319a5025c172bc/slide_0.jpg?41411159","durationSec":null,"lang":"en","topics":["hexagonal-architecture","onion-architecture"],"techs":["general"],"tags":["slides","software-architecture"],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"manual","publishedAt":null,"popularity":50,"verifiedAt":"2026-11-26T11:26:58.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:01:02.417Z","updatedAt":"2026-11-26T16:01:02.417Z"},{"id":"6b0857b23c96f2d34d0b35ee","type":"repo","provider":"github","url":"https://github.com/xmolecules/jmolecules","embedUrl":null,"title":"xmolecules/jmolecules","description":"Libraries to help developers express architectural abstractions in Java code","author":"xmolecules","thumbnailUrl":"https://opengraph.githubassets.com/1/xmolecules/jmolecules","durationSec":null,"lang":"en","topics":["ddd-strategic-design","hexagonal-architecture"],"techs":["java"],"tags":["architecture","domain-driven-design","java","software-architecture"],"price":"free","featured":false,"featuredOrder":0,"packageName":null,"source":"github","publishedAt":"2020-02-03T13:08:24.000Z","popularity":62,"verifiedAt":"2026-11-26T10:58:11.000Z","curated":false,"addedBy":"6b0857a15a2625e44ecaabe6","createdAt":"2026-11-26T16:00:50.215Z","updatedAt":"2026-11-26T16:00:50.215Z"}],"challenges":[],"attachments":[],"hasCode":true,"repoUrl":null,"series":[{"slug":"architecture-path","title":"From monolith to microservices","items":[{"slug":"monolith","title":"The Monolith: one application, one deployment, and why it is often the right start","current":false},{"slug":"modular-monolith","title":"The Modular Monolith: hard boundaries without the cost of the network","current":false},{"slug":"layered-architecture","title":"Layered architecture: presentation, logic, data – and where it breaks","current":false},{"slug":"hexagonal-architecture","title":"Pierogi in the Hexagon","current":false},{"slug":"clean-architecture","title":"Clean Architecture: the dependency rule and use cases at the centre","current":false},{"slug":"onion-architecture","title":"Onion architecture: the domain model in the middle, infrastructure outside","current":true},{"slug":"vertical-slice-architecture","title":"Vertical slice architecture: code by feature, not by layer","current":false},{"slug":"cqrs","title":"CQRS: one model for changing, another for reading","current":false},{"slug":"event-sourcing","title":"Event sourcing: store the facts, not just the latest state","current":false},{"slug":"event-driven-architecture","title":"Event-driven architecture: who publishes, who listens and what can go wrong","current":false},{"slug":"microservices","title":"Microservices: independent deployments, real costs and when it pays off","current":false},{"slug":"ddd-strategic-design","title":"DDD strategic design: subdomains, bounded contexts and context maps","current":false}]}],"prev":{"slug":"clean-architecture","title":"Clean Architecture: the dependency rule and use cases at the centre"},"next":{"slug":"vertical-slice-architecture","title":"Vertical slice architecture: code by feature, not by layer"},"featured":false,"authorCard":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","headline":"Wzorce projektowe · Architektura · AI i LLM","community":false,"reputation":{"points":3900,"level":{"key":"mentor","label":"Mentor"}},"articles":68,"skills":[{"key":"typescript","title":"TypeScript","level":5,"levelLabel":"Authority","articles":63},{"key":"dotnet","title":".NET","level":5,"levelLabel":"Authority","articles":53},{"key":"python","title":"Python","level":5,"levelLabel":"Authority","articles":52},{"key":"java","title":"Java","level":5,"levelLabel":"Authority","articles":38}]}}