Onion Architecture Lets slice it like a Pro by Ritesh Kapoor Expedia Group Technology

The Onion Architecture relies heavily on the Dependency Inversion principle. The Onion Architecture was first introduced by Jeffrey Palermo around 2008. At its very essence, the architecture focused on modeling business logic and entities at the application’s core, without any notion of the outside world. 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. By separating the application into layers, the system becomes more testable, maintainable and portable.

Fun Fact – Microsoft themselves recommend this kind of architecture for complex solutions. Few of the solutions developed and maintained by Microsoft MVPs like eShopOnWeb and eShopOnContainers also follow a similar (slightly more complicated variant of this approach). In the Startup/ConfigureServices of the API project, add these lines to register the Versioning. I have written a detailed article on API Versioning in ASP.NET Core 3.1 WebApi. Feel feel to read it to get a complete idea of this concept.

Infrastructure Layer

Repositories used in the domain and external services used in Application Services are implemented at the infrastructure layer. Onion Architecture is a software design pattern that Jeffrey Palermo introduced in 2008 in the post. It is based on the concept of layers, each representing a distinct set of responsibilities.

  • We are also using Docker Compose to group our Web application container with a container running the PostgreSQL database image.
  • Aliaksandr is a Senior .NET developer at SaM Solutions with 13 years of experience.
  • Based on the DDD model, we’ve created onion architecture (aka hexagonal or clean architecture).
  • The architecture does not depend on the data layer as in classic multi-tier architectures, but on the actual domain models.
  • The diagram you see here is a representation of traditional layered architecture.

This anti pattern has a lot of problems which are well described in Fowler’s article. The parts of your code that expose your application to the outside world are also part of the Infrastructure Layer, as they deal with IO. Usually it’s not a good idea to try to use a single repository for more than one aggregate, because maybe you will end up having a Generic Repository.

Onion Architecture explained — Building maintainable software

In this article, I will tell you about my experience of using with a harmonized combination of DDD, ASP.NET Core Web API and CQRS for building microservices. Onion Architecture explicitly separates technical concerns from business logic by placing them in the outer layers of the application. Domain Entities are the fundamental building block of Domain-Driven Design and they’re used to model concepts of your Ubiquitous Language in code. Entities are Domain concepts that have a unique identity in the problem domain. Domain entities encapsulate attributes and entity behaviour. It is supposed to be independent of specific technologies like databases or web APIs.

onion architecture

Add the Data in the domain that is used to add the database context class. The database context class is used to maintain the session with the underlying database using which you can perform the CRUD operation. First, you need to create the Core web API project using visual studio. After creating the project, we will add our layer to the project. After adding all the layers our project structure will look like this. 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.

Repository Layer

We define abstract interfaces at deeper layers and provide their concrete implementation at the outermost layer. This ensures we focus on the domain model without worrying too much about implementation details. We can also use dependency injection frameworks, like Spring, to connect interfaces with implementation at runtime.

onion architecture

In order to access the Database, we introduce a Data Access Layer. This layer usually holds ORMs for ASP.NET to fetch/write to the database. When there is just a logical separation in your application, we can term it as layers or N Layers. In cases where there is both a physical and logical separation of concerns, it is often referred to as n-tiered application where n is the number of separations. In this article, we will deal with Layered Architecture. Now we need to add the student controller that will interact will our service layer and display the data to the users.

Observability services

To learn more about unit testing your projects in ASP.NET Core check out this article Testing MVC Controllers in ASP.NET Core. A Repository is a pattern for a collection of domain objects. The former are rules that are executed to implement a use case of your application. The latter are rules that belong to the business itself.

Onion Architecture was introduced by Jeffrey Palermo to provide a better way to build applications in perspective of better testability, maintainability, and dependability. Onion Architecture addresses the challenges faced with 3-tier and n-tier architectures, and to provide a solution for common problems. Onion architecture layers interact to each other by using the Interfaces. C# programmers are drawn to Onion Architecture due to the dependency flows. If you are interested in learning more C# while working with the Onion Architecture, visit the TechRepublic Academy.