Software development

Understanding Onion Architecture: An Example Folder Structure by Alessandro Traversi

The basic idea is to have the presentation layer at the top, the business/domain layer in the middle, and the data access layer at the bottom. 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.

Onion architecture in development

The Infrastructure layer can also be easily scaled by adding more servers or resources to handle the increased load. Onion Architecture makes the application more maintainable. 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. Onion architecture is the division of an application into layers. Moreover, there is one independent level, which is in the center of the architecture.

Implementation of Onion Architecture

The Global Cloud Team specializes in different application architectures. We have over 450 specialists with various backgrounds so that we can assemble a team for any project. There are multiple interfaces, making it easy to get lost in the whole mix.

Onion architecture in development

The service layer holds all the business logic of the entity. In this layer services interfaces are kept separate from their implementation for loose coupling and separation of concerns. Building scalable, maintainable, and testable applications can benefit greatly from the Onion architecture.

Implementing Onion Architecture in ASP.NET Core WebApi Project

In the Services.Abstractions project you can find the definitions for the service interfaces that are going to encapsulate the main business logic. Also, we are using the Contracts project to define the Data Transfer Objects (DTO) that we are going to consume with the service interfaces. The first and most important layer is the Domain layer, the inner most one. It represents your app’s domain, the business logic and its functional implementation, everything it can do. In the case of the API presentation layer that presents us the object data from the database using the HTTP request in the form of JSON Object.

Onion architecture in development

And sometimes they want to reference each other, which is no-op due to potential circular dependency. Onion Architecture has great practical value, particularly for creating expansive, intricate software systems. It is simpler to test, maintain, and upgrade the codebase over time when an application is built in layers, which isolates the business logic from the display layer and infrastructure. The domain models and services will be inside this layer, containing all the business rules of the software. It should be purely logical, not performing any IO operations at all. It includes adapters for databases, UI, external services like RESTful, etc.

Result Service

If you are interested in learning more C# while working with the Onion Architecture, visit the TechRepublic Academy. The Infrastructure layer provides the implementation of the services and interfaces defined by the Domain layer. It is responsible for interacting with external systems, such as databases, messaging systems, and other services.

  • Onion Architecture is comprised of multiple concentric layers interfacing each other towards the core that represents the domain.
  • Onion Architecture was introduced by Jeffrey Palermo to provide a better way to build applications in perspective of better testability, maintainability, and dependability.
  • This line of code will find all of the controllers inside of the Presentation project and configure them with the framework.
  • The Infrastructure layer can also be easily scaled by adding more servers or resources to handle the increased load.
  • Onion Architecture is an architectural pattern which proposes that software should be made in layers, each layer with it’s own concern.

In this blog, we will discuss Onion Architecture in detail. Let’s look at one of the most popular Architecture in ASP.NET Core Applications. Here is a simple diagrammatic representation of a variation of the N-Layer Architecture. The presentation Layer usually holds the Part that the User can interact with, i.e, WebApi, MVC, Webforms, and so on. Business Logic is probably the most important part of this entire setup.

Avenue Code Social

In the Build Tab enable the XML Documentation file and give an appropriate file name and location. I have added the XML file to the root of the API Project. Similarly, create another .NET Standard Library Project in the Core Folder. This library provides almost limitless opportunities for setting data validation rules. It is well compatible with CQRS due to pipeline behaviors.

Onion architecture in development

Also, the code is easier to test due to dependency injection, which also contributes to making the software more maintainable. By doing this, your Infrastructure code can expect to receive an object that implements an interface, and the main can create the clients and pass them to the infrastructure. So, when you need to test your infrastructure code, you can make a mock that implements the interface (libs like Python’s MagicMock and Go’s gomock are perfect for this).

The Architecture of a Modern Startup

This separation improves code maintainability and facilitates testing. Naturally, maybe you want to start the development by the database, but it’s a mistake! When you are creating a software that does not deal with business rules, this architecture won’t fit well. It would be really cumbersome to implement, for example, a simple gateway using Onion Architecture. The Infrastructure Layer is the outermost layer of the Onion Architecture. In 1998 the City of Frankfurt approved a new high-rise development plan which specifies where new skyscrapers are allowed to be built in the city.

Besides the domain objects, you also could have domain interfaces. Domain objects are also flat as they should be, without any heavy code or dependencies. Overall, Onion Architecture is a useful pattern for developing software applications that are modular, easy to understand, and maintainable.

Repository Layer

I hope you’ll find my experience useful for your projects. You can check my github repository for technical details. ASP.NET Core offers Health Checks Middleware and libraries for reporting onion architecture the health of app infrastructure components. At times, we had to move a particular functionality into a separate microservice if it appeared in many places in the system.

Leave a Reply

Your email address will not be published. Required fields are marked *