Home
Do you know the layers of the onion architecture?
  v10.0 Posted at 3/11/2015 10:43 PM by Tiago Araujo
​​​Onion Architecture
Figure: The layers of the onion architecture

Application Core (the grey stuff)

This should be the big meaty part of the application where the domain logic resides.

Domain Model

In the very centre we see the Domain Model, which represents the state and behaviour combination that models truth for the organization and is only coupled to itself.

Repository Interfaces

The first layer around the Domain Model is typically where we find interfaces that provide object saving and retrieving behaviour. 
The object saving behaviour is not in the application core, however, because it typically involves a database.  Only the interface is in the application core.  The actual implementation is a dependency which is injected.

Business Logic Interfaces

Business logic is also exposed via interfaces to provide decoupling of business logic.
Examples of where this is useful include substituting a FacebookNotificationService for an EmailNotificationService or a FedExShippingCalculator for a DHLShippingCalculator

Clients (the red stuff)

The outer layer is reserved for things that change often.  E.g. UI and the other applications that consume the Application Core. 
This includes the MVC project.
Any interface dependencies in factories, services, repositories, etc, are injected into the domain by the controller.
This means any constructor-injected interfaces in domain classes are resolved automatically by the IoC container.

Dependencies

Dependencies are implementations of interfaces defined in Repository and Business Logic Interfaces and Domain.
These classes are specific implementations and can be coupled to a particular method of data access, or specific service technology.
e.g. this is where the EF DbContext is implemented, as well as things like logging, email sending, etc.
These dependencies are injected into the application core.
Because the Application core only relies on abstractions of the dependencies, it is easy to update them.
The Onion Architecture relies heavily on the Dependency Inversion principleand other SOLID principles.

References:

Use SSW Data Onion to Generate your Code

To help make this process pain free, we've developed the SSW Data Onion  to get you going and take away the boilerplate code you would normally need to write. Check out this cool video to see how it works:

 



Further Reading:Do You Use a Dependency Injection Centric Architecture?

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.