The Onion Architecture : part 1
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 leads to more maintainable applications since it emphasizes separation of concerns throughout the system. I must set the context for the use of this architecture before proceeding. This architecture is not appropriate for small websites. It is appropriate for long-lived business applications as well as applications with complex behavior. It emphasizes the use of interfaces for behavior contracts, and it forces the externalization of infrastructure.
The diagram you see here is a representation of traditional layered architecture. This is the basic architecture I see most frequently used. Each subsequent layer depends on the layers beneath it, and then every layer normally will depend on some common infrastructure and utility services. The big drawback to this top-down layered architecture is the coupling that it creates. Each layer is coupled to the layers below it, and each layer is often coupled to various infrastructure concerns. However, without coupling, our systems wouldn't do anything useful, but this architecture creates unnecessary coupling.
The biggest offender (and most common) is the coupling of UI and business logic to data access. Yes, UI is coupled to data access with this approach. Transitive dependencies are still dependencies. The UI can't function if business logic isn't there. Business logic can't function if data access isn't there. I'm intentionally ignoring infrastructure here because this typically varies from system to system. Data access changes frequently. Historically, the industry has modified data access techniques at least every three years; therefore, we can count on needing to modify data access three years from now for any healthy, long-lived systems that's mission-critical to the business. We often don't keep systems up-to-date because it's impossible to do. If coupling prevents easily upgrading parts of the system, then the business has no choice but to let the system fall behind into a state of disrepair. This is how legacy systems become stale, and eventually they are rewritten.
I propose a new approach to architecture. Honestly, it's not completely new, but I'm proposing it as a named, architectural pattern. Patterns are useful because it gives software professionals a common vocabulary with which to communicate. There are a lot of aspects to the Onion Architecture, and if we have a common term to describe this approach, we can communicate more effectively.

The diagram to the left depicts the Onion Architecture. The main premise is that it controls coupling. The fundamental rule is that all code can depend on layers more central, but code cannot depend on layers further out from the core. In other words, all coupling is toward the center. This architecture is unashamedly biased toward object-oriented programming, and it puts objects before all others.
In the very center we see the Domain Model, which represents the state and behavior combination that models truth for the organization. Around the Domain Model are other layers with more behavior. The number of layers in the application core will vary, but remember that the Domain Model is the very center, and since all coupling is toward the center, the Domain Model is only coupled to itself. The first layer around the Domain Model is typically where we would find interfaces that provide object saving and retrieving behavior, called repository interfaces. The object saving behavior is not in the application core, however, because it typically involves a database. Only the interface is in the application core. Out on the edges we see UI, Infrastructure, and Tests. The outer layer is reserved for things that change often. These things should be intentionally isolated from the application core. Out on the edge, we would find a class that implements a repository interface. This class is coupled to a particular method of data access, and that is why it resides outside the application core. This class implements the repository interface and is thereby coupled to it.
The Onion Architecture relies heavily on the Dependency Inversion principle. The application core needs implementation of core interfaces, and if those implementing classes reside at the edges of the application, we need some mechanism for injecting that code at runtime so the application can do something useful.
The database is not the center. It is external. Externalizing the database can be quite a change for some people used to thinking about applications as "database applications". With Onion Architecture, there are no database applications. There are applications that might use a database as a storage service but only though some external infrastructure code that implements an interface which makes sense to the application core. Decoupling the application from the database, file system, etc, lowers the cost of maintenance for the life of the application.
Alistair Cockburn has written a bit about Hexagonal architecture. Hexagonal architecture and Onion Architecture share the following premise: Externalize infrastructure and write adapter code so that the infrastructure does not become tightly coupled.
I'll be writing more about the Onion Architecture as a default approach for building enterprise applications. I will stay in the enterprise system space and all discussion will reside in that context. This gets even more interesting when there are multiple processes making up a single software system.
Trackbacks
The Onion Architecture Posted on 7.29.2008 at 8:51 AM
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Onion Architecture Posted on 7.29.2008 at 9:15 AM
Onion Architecture
Elegant Code » The Onion Architecture Posted on 7.29.2008 at 12:42 PM
Pingback from Elegant Code » The Onion Architecture
The Inquisitive Coder - Davy Brion’s Blog » Blog Archive » Onion Architecture? Posted on 7.29.2008 at 2:20 PM
Pingback from The Inquisitive Coder - Davy Brion’s Blog » Blog Archive » Onion Architecture?
The Onion Architecture Posted on 7.30.2008 at 1:50 AM
The Onion Architecture
Reflective Perspective - Chris Alcock » The Morning Brew #147 Posted on 7.30.2008 at 2:27 AM
Pingback from Reflective Perspective - Chris Alcock » The Morning Brew #147
Dew Drop - July 30, 2008 | Alvin Ashcraft's Morning Dew Posted on 7.30.2008 at 7:06 AM
Pingback from Dew Drop - July 30, 2008 | Alvin Ashcraft's Morning Dew
The Onion Architecture : part 2 Posted on 7.30.2008 at 8:14 AM
In part 1 , I introduced an architectural pattern that I have named "Onion Architecture". The object-oriented design concepts are not new, but I'm pulling together a lot of techniques and conventions into a single pattern and giving it a name. My hope
So you want to learn NHibernate - Part 1/2, Prerequisites (or NHibernate = Marijuana.NET) « HSI Developer Blog Posted on 7.31.2008 at 12:53 PM
Pingback from So you want to learn NHibernate - Part 1/2, Prerequisites (or NHibernate = Marijuana.NET) « HSI Developer Blog
Links of the Week August 2 2008 Posted on 8.01.2008 at 8:10 PM
Another week of great resources for the .NET developer to take in. Amazing how many are out there. I
Weekly Links #12 | GrantPalin.com Posted on 8.03.2008 at 9:07 PM
Pingback from Weekly Links #12 | GrantPalin.com
The Onion Architecture : part 3 Posted on 8.04.2008 at 9:34 AM
Part 1 - Part 2 - This is part 3 - My RSS feed In my previous installments, I described what has become my approach to defining the architecture for an application. Based on feedback, I've modified my diagrams a bit to reduce ambiguity and emphasize key
Weekly Web Nuggets #23 Posted on 8.04.2008 at 9:36 AM
General The Onion Architecture Part 1 : Jeffrey Palermo proposes a new architectural pattern that is based on how components are coupled to each other. The Onion Architecture Part 2 : Jeffrey Palermo continues his presentation of the Onion Architecture
La arquitectura de la cebolla Posted on 8.07.2008 at 9:02 PM
Pingback from La arquitectura de la cebolla
So you want to learn NHibernate - Part 0.5, Prerequisites (or NHibernate = Marijuana.NET) | The Freak Parade Posted on 8.08.2008 at 5:39 PM
Pingback from So you want to learn NHibernate - Part 0.5, Prerequisites (or NHibernate = Marijuana.NET) | The Freak Parade
Training module - Layering Posted on 8.15.2008 at 1:48 PM
Training module - Layering
Architecture with layers, active records, and onions Posted on 8.16.2008 at 2:39 PM
In the 1990s I coded on a few systems where the architecture was that we attached database functionality
The Onion Architecture | Dev @ Work Posted on 8.26.2008 at 12:43 PM
Pingback from The Onion Architecture | Dev @ Work
Translation Tester, part 1 - The problem « ICoder Posted on 9.10.2008 at 5:01 PM
Pingback from Translation Tester, part 1 - The problem « ICoder
Architektur Roundtripp - Ein Wochen-Review Posted on 9.12.2008 at 5:30 AM
Nachdem ich schon vor einigen Monaten immer mal wieder etwas über DDD gehört und gelesen hatte (meine
Why Test Driven Development is a Hard Sell Posted on 10.06.2008 at 4:41 PM
A Good Presentation Two weekends I went to a San Diego Dot Net User Group meeting that featured two presentations
Why Test Driven Development is a Hard Sell Posted on 10.06.2008 at 6:13 PM
A Good Presentation Two weekends I went to a San Diego Dot Net User Group meeting that featured two presentations
Code Duplication tool on CodePlex Posted on 10.14.2008 at 7:15 AM
Just chatting yesterday about quick wins in code reviews and the subject of de-duping code came up. Even
Logical not Physical, Few not Many, Just Do It Posted on 10.20.2008 at 6:21 PM
In both my “Better Domain Driven Design” and “Want SOA? Throw out your Web Services!”
Code Duplication tool on CodePlex Posted on 10.22.2008 at 4:29 AM
Just chatting yesterday about quick wins in code reviews and the subject of de-duping code came up. Even
Sharon’s Blog » Layering Posted on 11.03.2008 at 11:32 PM
Pingback from Sharon’s Blog » Layering
The Myth of Self-Organizing Teams Posted on 11.11.2008 at 8:29 AM
A hot topic in the agile world is "self-organization". The reaction against tight command and control management structures has swayed the pendulum all the way over to chaos. First, I understand that every team is different, and my views are tainted by
Domain Model Repository != DDD Posted on 12.05.2008 at 1:25 AM
Domain Model Repository != DDD
My current architecture Posted on 12.10.2008 at 12:15 PM
For the six or seven of you in the world that actually read my blog, you've noticed that i've been pretty...
My current architecture Posted on 12.10.2008 at 12:52 PM
For the six or seven of you in the world that actually read my blog, you've noticed that i've
Websites tagged "onion" on Postsaver Posted on 12.31.2008 at 3:02 AM
Pingback from Websites tagged "onion" on Postsaver
Extreme Enthusiasm » Blog Archive » The birthday greetings kata Posted on 1.13.2009 at 9:14 AM
Pingback from Extreme Enthusiasm » Blog Archive » The birthday greetings kata