Onion Architecture DEV Community

Each of these layers represent a specific duty within the overall function of a service. Each layer can be independently tested, allowing for comprehensive unit tests and ensuring that business logic remains isolated from external dependencies. Dependencies flow inward, with inner layers having no knowledge of outer layers. This ensures that high-level modules do not depend on low-level modules directly. Instead, both depend on abstractions, enabling interchangeable implementations and reducing coupling.

  • Infrastructure services also referred to as Infrastructure adapters are the outermost layer in onion architecture.
  • Onion Architecture is a software architecture pattern that separates the application into layers based on their responsibilities.
  • There are two basic approaches to representing the layers in the code.
  • In 3-tier and n-tier architectures, none of the layers are independent; this fact raises a separation of concerns.
  • Application Services interact with other services to fulfil the client’s request.

Like it the example above — the code uses Lombok
annotations, generating the boilerplate which otherwise needs to be written by the programmer. The layer is intended to act as an abstraction layer between an application’s Domain Entities layer and its Business Logic layer. We typically include APIs in this layer that offers object saving and retrieval functionality, usually by utilizing a database. A data access pattern encourages a more loosely coupled approach to data access.

Understanding Onion Architecture: An Example Folder Structure

The main idea behind the Onion architecture is the flow of dependencies, or rather how the layers interact with each other. The deeper the layer resides inside the Onion, the fewer dependencies it has. Testability is very high with the Onion architecture because everything depends on abstractions. The abstractions can be easily mocked with a mocking library such as Moq.

onion software architecture

Developers who are not familiar with these concepts may find it challenging to implement and maintain an Onion Architecture-based application. Onion Architecture separates the application into layers based on their responsibilities. This separation makes the application more modular and easier to understand. Is the database we use or an external dependency not part of our domain model layer? Some companies that have successfully used Onion Architecture include Microsoft, Uber, and BBC iPlayer. They have used Onion Architecture to build scalable and maintainable software systems that can evolve and adapt to changing business requirements.

What is Onion Architecture?​

The modular and decoupled architecture that the Onion Architecture encourages makes it simpler to maintain the application over time. Developers can make changes to one layer without impacting the other levels since each layer has a distinct function and communicates with other layers through clearly defined interfaces. The modular layout favored by Onion Architecture makes it simple to scale the application.

onion software architecture

The rest of your code shouldn’t worry if you are storing your data in a database, in a file, or just in memory. The application layer implements Application rules (sometimes called use cases) instead of Business rules. This is also the layer that “knows” which operations should be performed atomically, thus the transaction-related code
is placed here. Note, however, that in the example above, the transactions field is actually an interface reference. On the other hand, working in a more rigid, but at the same time more expressive, and structured environment of
a well-architected application, was a breeze and a real pleasure. Not to mention that the time required to introduce the
change was smaller, and the estimates were more precise and predictable.

Implementation of Onion Architecture

It’s characterized by a strict layering of concerns, typically consisting of Presentation, Business, and Data Access layers. The Domain layer, which contains the business logic, can be easily scaled by adding more instances of the application. The Infrastructure layer can also be easily scaled by adding more servers or resources to handle the increased load. The clear separation of concerns between the layers makes it easier to modify and maintain the application. Changes in one layer do not affect the other layers, which reduces the risk of introducing bugs into the system.

Repositories used in the domain and external services used in Application Services are implemented at the infrastructure layer. This layer creates an abstraction between the domain entities and business logic of an application. In this layer, we typically add interfaces that provide object saving and retrieving behavior typically by involving a database. This layer consists of the data access pattern, which is a more loosely coupled approach to data access. We also create a generic repository, and add queries to retrieve data from the source, map the data from data source to a business entity, and persist changes in the business entity to the data source.

Getting started with Domain-Driven Design in TypeScript: a practical introduction

When using CQRS, separation of concerns is pushed even further. Traditional layered architecture may struggle to cleanly implement CQRS, as separation of read and write operations could blur the boundaries between layers. The Domain layer, which contains the business logic, can be easily tested without the need for the Infrastructure layer or the User Interface layer. The Application layer can be tested using mock objects, which makes the testing process faster and more efficient.

onion software architecture

RiderFareCalculator is implemented in this layer also, and it depends on the fare repository and route service interfaces declared in the same layer. Note that with this approach, we do not depend on the external service, rather the external service depends on our declared contracts. Onion Architecture solved these problem by defining layers from the core to the Infrastructure.

A cache is a storage between the application’s layer and source data.

By separating the application into layers, the system becomes more testable, maintainable and portable. It helps easy adoption of new frameworks/technologies when old frameworks become obsolete. Similar to other architectural styles like Hexagonal, Layered, Clean Architecture, etc. it provides a solution for common problems.

onion software architecture

The deeper we go, the more we know about the domain and business rules. The outer rings are mechanisms (including different switchable modules), whereas the inside circles are fundamental domain logic. The outer layers rely on the inner layers, and the inner layers are unaffected by any changes being introduced in the outer rings.

Separation of concerns

This makes it easier to reuse components across different applications, reducing development time and costs. The Service layer holds interfaces with common operations, such as what is onion architecture Add, Save, Edit, and Delete. Also, this layer is used to communicate between the UI layer and repository layer. The Service layer also could hold business logic for an entity.

Giving software projects a distinct structure and a separation of concerns, onion architecture can aid in achieving these objectives. Onion architecture might seem hard in beginning but is widely accepted in the industry. It is a powerful architecture and enables easy evolution of software.

Solution Structure

Infrastructure services also referred to as Infrastructure adapters are the outermost layer in onion architecture. These services are responsible for interacting with the external world and do not solve any domain problem. These services just communicate with external resources and don’t have any logic. External notification Service, GRPC Server endpoint, Kafka event stream adapter, database adapters. Application services also referred to as “Use Cases”, are services responsible for just orchestrating steps for requests and should not have any business logic.