Software Development

Onion Architecture allegro tech

Is the database we use or an external dependency not part of our domain model layer? It does so
with ideas similar to Hexagonal Architecture,
Clean Architecture and
other related architecture styles. Application services also referred to as “Use Cases”, are onion architecture software services responsible for just orchestrating steps for requests and should not have any business logic. Application Services interact with other services to fulfil the client’s request. Let’s consider the use case to create an order with a list of items.

The entities defined in the Domain layer are going to capture the information that is important for describing the problem domain. 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.

Benefits of Onion Architecture:

The user interface and user interaction are handled by the presentation layer. It offers the user’s interface for interacting with the application. A web application, a desktop application, or a mobile application can make up this layer. An application written to help manage a Library would most probably have classes like Book,
Reader, Copy and so on. The classes, relations and interactions between them describe the core of the domain of the
application, i.e. what business needs it fulfils and in what way. In the Library, there would be a process of adding new
titles to the catalogue, a process of borrowing and returning copies of a book, charging readers for overdue books, and
many more.

onion architecture software

Developers can take full advantage of the architecture and construction by adhering to best practices. Onion architecture is built on a domain model in which layers are connected through interfaces. The idea is to keep external dependencies as far outward as possible where domain entities and business rules form the core part of the architecture. The fundamental domain models and business logic are found in the domain layer. It serves as the application’s brain and represents the concepts, entities, and business rules. This layer should not have any dependencies on any other layer because it is technology independent.

Application services

If you are looking to test the entire system then that would be an end-to-end test that would be performed. Notice, that the biggest file is the one for the infrastructure layer. The
infrastructure has all the framework — in this case Spring Boot — database driver, and other dependencies, and itself
depends on both domain and application. There’s of course nothing preventing you from declaring extra dependencies,
say Lombok.

It is called “onion” architecture because the design is layered, with the core domain logic at the center and the outer layers representing services and infrastructure. The layers of an onion architecture are typically organized around the business domain, with the core domain logic at the center and the outer layers representing infrastructure and service concerns. Each layer/circle encapsulates or hides internal implementation details and exposes an interface to the outer layer. All layers also need to provide information that is conveniently consumed by inner layers.

Centralized business rules

By doing this, we can ensure that our asynchronous calls that use the CancellationToken will always work. For smaller projects, Onion Architecture might occasionally be over-engineered, adding additional complexity and overhead. The appropriate understanding and application of the architecture may take more effort and training for those who are unfamiliar with the Onion Architecture. Each layer of the design can be scaled independently of the others, making scalability simple. The Onion Architecture encourages concern separation by breaking the program up into several layers. Each layer is accountable for a certain task, thus alterations to one should not have an impact on the others.

onion architecture software

Next, we looked at the Infrastructure layer, where the implementations of the repository interfaces are placed, as well as the EF database context. We are using a Web API built with ASP.NET Core to create a set of RESTful API endpoints for modifying the domain entities and allowing consumers to get back the data. As we can see, it consists of the Web project, which is our ASP.NET Core application, and six class libraries. The Domain project will hold the Domain layer implementation.

The Infrastructure Layer

Low coupling in which one module interacts with another module and does not need to be concerned with the other module’s internals. All the internal layers need not be concerned about internal implementation of external layers. No direction is provided by the Onion Architecture guidelines about how the layers should be implemented. The architect should decide the implementation and is free to choose whatever level of class, package, module, or whatever else is required to add in the solution.

onion architecture software

Changes to one layer should not have an impact on the others because each layer is responsible for a distinct task. This makes it simpler to update and maintain the application over time. There should be no external dependencies or technology-specific code at the domain layer. This guarantees that the domain layer will always be portable and independent. The most bare bones application of the principal is Hexagonal or “ports and adapters” architecture.

A Comprehensive Guide to Object Detection Using Deep Learning

At times, we had to move a particular functionality into a separate microservice if it appeared in many places in the system. On the contrary, if some functionalities were tightly connected, we had to combine microservices into one. And the most challenging task was to find a balance between all these functions. DDD implies that you distinguish a certain bounded context, which is a set of entities tightly connected with each other but minimally connected with other entities in your system. Our customer needed a software system compatible with their hardware so that clients could buy equipment, install software and create and manage content. Java developers may not be as interested in Onion Architecture as C# developers.

  • This separation improves code maintainability and facilitates testing.
  • This Architecture style does have some learning curve for developers in the project, but once mastered, pays back many
  • We are creating a project called Presentation and giving it a reference to the Microsoft.AspNetCore.Mvc.Core NuGet package so that it has access to the ControllerBase class.
  • I will stay in the enterprise system space and all discussion will reside in that context.
  • Also, the code is easier to test due to dependency injection, which also contributes to making the software more maintainable.
  • I’ve spoken several times about a specific type of architecture I call “Onion Architecture”.

Each layer has a distinct responsibility, ensuring that business logic remains decoupled from infrastructure or presentation concerns. This separation improves code maintainability and facilitates testing. The outer layer is reserved for things that change often outside of the applications core business logic that interact with it. In the case of the UI, it is a service in itself, it will have its own logic and tests which validate it behaves as expected.

The Presentation Layer

To demonstrate a common folder structure based on Onion Architecture, let’s consider a hypothetical e-commerce application. The great thing about this approach is that the migrations will be automatically applied when we create new migrations, further down the road. To learn more about migrations and how to seed data with EF Core in both .NET 5 and .NET 6 check out this article Migrations and Seed Data with Entity Framework Core.

Domain-Driven Design: A Walkthrough of Building an Aggregate

If you wanted to switch databases, you could just write a new adapter. User interfaces would also be ports which could be filled with various adapters without modifying the core logic. Domain services are responsible for holding domain logic and business rules. All the business logic should be implemented as a part of domain services. Domain services are orchestrated by application services to serve business use-case. They are NOT typically CRUD services and are usually standalone services.

What is Onion Architecture?

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. We are hiding all the implementation details in the Infrastructure layer because it is at the top of the Onion architecture, while all of the lower layers depend on the interfaces (abstractions). Using Gradle setup as an example, one can define three modules — domain, application, and infrastructure —
in settings.gradle file.

Sobre el autor