The Onion Architecture

The Problem

Often we develop complex business driven applications. While we design our application, it’s architecture and it’s layers, we often feel to use a traditional approach to define the layers. Here is a short structure of what i mean:

traditionalpackaging

The problem with this structure is that the packaging is infrastructure driven. We have a user interface which uses the business logic. The business logic uses the data access layer to access data. There are also some cross cutting concerns like transaction control or security. This packaging was often motivated by classic J2EE patterns. But the problems are the dependencies.

Our ui depends on business logic. And the business logic depends on infrastructure code. Transitive dependencies are still dependencies, therefore the user interface depends on infrastructure, and the business logic as well. Those structures are often hard to validate. Due to the nature of tools like Maven we cannot express something like “business logic depends indirectly on data access”, and we don’t want any Hibernate classes used there. We have to use test frameworks like JDepend to check for unwanted imports. No compile time check.

This is probably not what we want to design.

The Onion Architecture

So what could be a solution? Jeffrey Palermo posted his approach of an Onion Architecture. Instead of putting the database and the data access logic into the middle, it put’s the business logic into the middle. Everything else adds more functionality like a Decorator around the business logic, like transaction support, security or logging. This packaging greatly fits together with Domain driven design. The onion architecture would look like this:

onionarchitecture

Maven module dependencies can be directly derived from the Onion Architecture. There is a core “Domain Model” module. There is also a “Domain Services” module which depends on the “Domain Model” and so on. This structure can be greatly verified by compile time dependencies.Fur further reading i suggest the Domain-driven Design Example Architecture.

The Onion Architecture is also known as Hexagonal Architecture coined by Alistair Cockburn. Visit his site to see this concept from a different point of view.

For further information, please visit Jeffrey Palermo: The Onion Architecture .

comments powered by Disqus