Onion Architecture on Domain Driven Design

Notes from Domain Driven Design: Complete Software Architecture Course.

Layers

Layers are represented as concentric circles. Each of the outer layers can depend on the inner layer. However the inner layers cannot depend or reference to outer layers.

Service

  • It holds API setup logic (for API components) or the user interface logic (for UI components).
  • It could contain the code that starts up the API and HTTP controllers.

Application

  • It holds application level logic that is called by the Service layer.
  • It could contain services to which other interfaces (like API controllers or user interfaces) are connected.

Domain

  • It holds domain objects and related domain logic.
  • It could contain Entities, Value Objects, Aggregates, etc. and repositories, services, etc.

Infrastructure

  • It holds cross-cutting concerns.
  • It could contain repositories and services that require interaction with some database or external service and/or platform dependant code.

Patterns

When implementing the Onion Architecture you will make use of the following design patterns:

Factory method

  • Recommended to encapsulate logic of creating objects such as entities and aggrgates.
  • Prevents client from being aware of the inner workings of the object manipulation.

Repository

  • Adds abstraction layer on top of the data access layer. Application logic is not aware of data storage related methods (retrieve, store, etc.), which are delegated to a dedicated repository.
  • Alternative storage implementations may be interchanged without changing application or domain logic.

Service

  • Objects that manipulate domain objects without being part of the domain themselved (operations that don’t conceptually form part of an Entity, Value or Aggregate ).
  • The service and its operations should be named using terms from the Ubiquitous Language.
  • Services should be stateless.

Dependency Injection

  • Makes a class independent of its dependencies.
  • Decouples the usage of an object from its creation.

Leave a comment