Onion Architecture
The fundamental rule is that all code can depend on layers more central, but code cannot depend on layers further out from the core. This rule is the Dependency Rule.
Onion Architecture is a software architectural pattern that organizes code into concentric layers with the Domain Model at the core, surrounded by Domain Services, Application Services, and Infrastructure at the outermost ring. Its governing rule is absolute: all source code dependencies must point inward — outer layers depend on inner layers, never the reverse. Unlike traditional N-Tier architecture where the database drives design, Onion Architecture makes the domain model king and treats persistence, UI, and external services as swappable implementation details.
“I'm building an e-commerce API in C#. Right now my service classes directly call Entity Framework DbContext, which feels wrong but I'm not sure how…”
Put business logic at the center — make infrastructure a detail, not a foundation
Onion Architecture structures applications as four concentric rings: the Domain Model at the center (entities, value objects — zero external dependencies), Domain Services surrounding it (domain logic that spans multiple entities, domain interfaces), Application Services outside that (use-case orchestration, IRepository calls), and Infrastructure/UI/Tests at the outermost ring (database implementations, controllers, IoC container wiring). The pattern's single governing constraint — the Dependency Rule — states that source code dependencies can only point inward. Infrastructure implements interfaces defined by inner layers (Dependency Inversion Principle), meaning a repository interface lives in the domain, while its SQL implementation lives at the edge. This makes inner layers fully unit-testable without a database, framework, or external service, and makes infrastructure components independently swappable. The pattern contrasts sharply with traditional N-Tier/Layered Architecture, which places the database at the center and causes business logic to be contaminated by persistence concerns.
Traditional N-Tier applications place the database at the center of design, causing business logic to depend on persistence frameworks, ORM types, and infrastructure specifics. This tight coupling makes business rules hard to test without a real database, infrastructure changes break domain logic, and the application cannot be reasoned about without understanding the data model. Developers building with Onion Architecture recognize this as the foundational mistake: infrastructure should serve the domain, not define it.
Stop letting your database dictate your domain model. Apply Palermo's Dependency Rule and every layer of your application becomes independently testable, infrastructure becomes swappable, and your business logic finally lives at the center where it belongs.
- A description or diagram of your application's domain and use cases
- Existing code snippets or project structure you want reviewed
- Your target language or ecosystem (C#/.NET, Java, Python, etc.)
- Questions about where a specific class, concern, or dependency belongs
- A correctly layered folder/namespace/project structure aligned to Palermo's four-ring model
- A dependency audit identifying where your codebase violates the inward-only rule
- Interface and implementation split decisions for every infrastructure concern
- A refactoring sequence to migrate a database-centric layered app toward Onion Architecture
Watch the methodology work.
Three specimens from a single real session: the same situation, unaided and calibrated, the full transcript, and the skill answering live in the channel where the work happens.
“Your `OrderService` directly instantiates `AppDbContext`, your Domain entities have `[Column]` EF annotations on them, and your data access layer is referenced by half the projects in the solution. Unit testing any business logic requires spinning up an in-memory database, and the last time the team switched connection pooling libraries it broke three service classes.”
“Your Domain Model has zero project references — entities are plain C# with no ORM annotations. `IOrderRepository` is defined in Domain Services, implemented in Infrastructure, wired in `Program.cs`. The entire business logic layer is unit-tested with mock repositories in milliseconds. When the team migrates from EF Core to Dapper, only one Infrastructure class changes — no domain or application logic is touched.”
The same skill, where the work happens.
No new app to learn. The methodology runs over the WhatsApp Business API, so the answer lands as a reply in the thread you’re already in — same rigour, zero context-switch.
What it does, specifically.
Each capability is a distinct move drawn straight from the source methodology — not a generic assistant guessing.
Layer Classification
Given any class, interface, module, or concern, determines which of Palermo's four concentric layers it belongs in and explains the reasoning based on the Dependency Rule. Catches common misplacements like repository implementations in the domain layer or domain logic leaking into Application Services.
Dependency Rule Audit
Reviews code snippets, project references, or architectural descriptions and identifies every place where an inner layer depends on an outer layer — the primary violation Onion Architecture exists to prevent. Produces a prioritized list of violations with explanations.
Interface/Implementation Split
For any infrastructure concern (persistence, email, payment, file storage, external API), specifies exactly where the interface should be defined (inner layer) and where the implementation should live (outer layer), including naming conventions and the IoC wiring location.
Project Scaffold Generator
Generates a complete folder structure, project/namespace layout, and starter files for a new application adhering to Onion Architecture — tailored to the described domain, use cases, and target ecosystem (C#, Java, Python, etc.).
Architecture Comparison
Provides precise, side-by-side analysis of Onion Architecture versus N-Tier Layered Architecture, Hexagonal Architecture (Ports & Adapters), and Robert Martin's Clean Architecture — including where they agree, where Palermo's framing differs, and which pattern fits a given context.
Refactoring Roadmap
Given a description of an existing layered or database-centric application, produces an incremental refactoring sequence to migrate toward Onion Architecture — identifying which violations to fix first, which seams to extract interfaces at, and how to introduce the IoC container boundary.
Graded before it shipped.
Every skill is scored against independent scenarios for methodology fidelity before it goes live — not vibes, a rubric.
Onion Layer Map
A labeled diagram of your application's four concentric layers — Domain Model, Domain Services, Application Services, Infrastructure — with your specific classes, interfaces, and concerns placed correctly in each ring. Reveals where current code violates the dependency rule at a glance.
Dependency Violation Report
A structured list of every inward-dependency violation found in your codebase: the offending class, what it depends on, which layer rule it breaks, and the recommended fix. Prioritized by architectural impact.
Project Scaffold
A ready-to-use folder structure and project/namespace layout for your application, with starter files showing where Domain entities, repository interfaces, application services, and infrastructure implementations go — including IoC wiring at the entry point.
Interface/Implementation Split Spec
A complete specification for each infrastructure concern in your application: the interface name, its location in the inner layer, the implementation class name, its location in the infrastructure layer, and the constructor injection or IoC registration pattern.
Grounded in the original work.
Every answer traces back to a real source and the practitioner who wrote it — not a secondhand summary. Here is the source of record.
Jeffrey Palermo
Jeffrey Palermo is a software architect and Microsoft MVP who introduced Onion Architecture in a canonical 4-part blog series in 2008 on jeffreypalermo.com. He is a frequent conference speaker at events including NDC and CodeMash, and his architectural pattern is widely cited in Domain-Driven Design and Clean Architecture literature. Palermo has spent his career in .NET enterprise development and is a recognized authority on application architecture and software design principles.
Onion Architecture (4-part blog series, 2008)
Creator of Onion Architecture; Microsoft MVP; conference speaker at NDC and CodeMash; originator cited in DDD and Clean Architecture literature.
Be first to run it.
Onion Architecture is being built right now. Leave your email and we’ll tell you the moment it goes live.