Onion Architecture in ASP NET Core

Just like in the stratified design, you’re putting stuff that changes more frequently on an outer layer. Onion Architecture is a method for organizing applications to manage dependencies, simplify the structure, and stand as a defense to technical debt. For over a decade, developers around the world have used Onion Architecture to empower their development teams. The main premise behind onion architecture is the fundamental concept of pushing your code, and having as few dependencies in your code as possible.

Infrastructure Layer– this is the outermost layer of onion architecture which deals with Infrastructure needs and provides the implementation of your repositories interfaces. In other words, this is where we hook up the Data access logic or logging logic or service calls logic. The most prominent classes in the Core are Visitor and VisitorProcessor, members of domain model and application services layers respectively. Now we create the third layer of the onion architecture which is a service layer. To build this layer, we create one more class library project named OA.Service. This project holds interfaces and classes which have an implementation of interfaces.

What is onion architecture

Usually, we write software in an imperative way. We write software that makes decisions as we go. Did I make an assumption here about the database? You want to just move all that out because the database is something you can make a mistake on. It’s https://globalcloudteam.com/ much more likely that you choose the wrong database, than you choose the wrong accounting system, the accounting process. We want to isolate those changes so that for instance, if I made a mistake in database, I don’t have to do a whole rewrite.

You can also add the Entity Framework Core package by running the command Install-Package Microsoft.EntityFrameworkCore on the Package Manager Console window of Visual Studio. Remember to select the Application project from the “Default project” dropdown. Note that we have to change the target framework to .NET Standard 2.1 which is the latest version right now. You do this by right clicking the domain project and select properties, then on the window choose the 2.1 edition as I have shown in the below image. The Infrastructure and Presentation Layers are outermost layers of Onion Architecture.

Onion Architecture in ASP.NET Core

That’s why it was difficult to immediately divide the functionality into the necessary microservices. The system can be quickly tested because the application core is independent. 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. The challenge was to create a cloud software solution for a digital signage hardware manufacturer. Network protocols — microservices interact with each other via network protocols such as HTTP and HTTPS.

What is onion architecture

The code that interacts with the database will implement interfaces in the application core. The core code does not care about the external code, doesn’t need to know what user interface or data base, only the class or form of data. The application core is coupled to those interfaces but not the actual data access code. This way, we have the ability to change code in any outer layer without affecting the application core. I’ve spoken several times about a specific type of architecture I call “Onion Architecture”.

But I found that domain entities are still accessible in the presentation layer . This is because the presentation layer has a project dependency on the application layer and the application layer depends on the domain layer where domain entities are defined as public. We cannot define it as internal because the entities will not be accessible by the application layer.


Services.Abstractions project it will only be able to call methods that are exposed by this project. We are going to see why this is very useful later on when we get to the Presentation layer. Presentation project will be the Presentation layer implementation. The main idea behind the Onion architecture is the flow of dependencies, or rather how the layers interact with each other.

  • Decoupling the application from the database, file system, etc, lowers the cost of maintenance for the life of the application.
  • The flow of dependencies dictates what a certain layer in the Onion architecture can do.
  • Now create a .NET Core class library project inside the infrastructure folder.
  • It provides better maintainability as all the codes depend on layers or the center.
  • I often find it easier to drive out business logic code through tests than I do integrated code.

Imagine if you put the saveObjectToDatabase method in domain object, then you will depend on Infrastructure layer which is aviolationof Onion Architecture. The strategy is to make interfaces defined in the domain layer and put the implementation in Infrastructure layer. Following this principle makes sure we have loose coupling between layers and a real implementation comes only at real time. Onion Architecture sets a clear dependency rule between layers, making it a more restrictive variant of Layered and Hexagonal Architectures.

Recall, we already created CRUD operations on the Application project. Dependency Inversion Principle states that the high-level modules should not depend onion architecture on low-level modules. We create interfaces in the Application Layer and these interfaces get implemented in the external Infrastructure Layer.

Generate the Migrations and the Database

Hi Mukesh, I was new to building API’s with Microservice Architecture using ASP.net Core. I went through your articles and I can say ur aticles are one of the best. Can you please make articles on Logging, Validation and Transactional Behavior.

You must have question like – What are the main layers of Onion Architecture and how they communicate with one another. These questions answers will be answered in this section. Another very good one is “Lean Architecture for agile software development” by James Coplien, and another MUST READ is “The mythical man month” by Frederick P Brooks. Russ Milesdid a presentation about his Life Preserver ideas, based on the Hexagonal architecture, last year. Onion architecture is sometimes called ports and adapters or Hexagonal architecture, but Wade believes these are a superset of the Onion architecture.

Observability-Driven Development (ODD)

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. 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. Let’s understand different layers of the architecture and their responsibilities with an order creation use case. In this project we will setup Entity Framework core which will access the CRUD operations performed by CQRS.

If you have been using Asp.net web forms to develop web application then adopting MVC architecture even fits better, because it provides the required framework to build web apps MVC way. But things doesn’t turn out as planned and leads to very tight coupling between UI and business logic and business logic to database logic. This is because you end up writing all of your logic in server side code (mainly aspx.cs files). This scenario produces very tight coupling over the period of time and system becomes a nightmare to maintain.

E.g. for smaller applications that don’t have a lot of business logic, it might not make sense to have domain services. Regardless of layers, dependencies should always be from outer layers to inner layers. Infrastructure is the outermost layer containing adapters for various technologies such as databases, user interface and external services.


Coupling between components and between layers is a much better quality heuristic, and onion architecture is all about managing that coupling. Database Independent – Since we have a clean separation of data access, it is quite easy to switch between different database providers. This will be an Empty API Controller which will have API Versioning enabled in the Attribute and also a MediatR object. We will not have to re-define the API Versioning route or the Mediator object.

Similar to Onion architecture (

The following is the code snippet for the context class. Now, we define the configuration for the UserProfile entity that will be used when the database table will be created by the entity. The code snippet is mentioned below for the UserProfile mapping entity (UserProfileMap.cs). Now, we define the configuration for the User entity that will be used when the database table will be created by the entity.

The Model is used to pass the data between View and Controller on which the business logic performs any operations. The Controller is used to handle the web request by action methods and returns View accordingly. Hence, it solves the problem of separation of concern while the Controller is still used to database access logic. In essence, MVC solves the separation of concern issue but the tight coupling issue still remains.

Each layer acts as modules/package/namespace within the application. WebApi/WPF don’t have DbModels, but only Models(DTOs/ViewModels). On it’s left side you will see Projects option, select it, then on the middle section you will see the Domin project. Select the checkbox for the domain project and click the OK button. This will add the Domain project’s reference to the Application project. We should be able to test the business rules without the UI, Database, Web Server, or any other external dependency.

It can have access to both the database and UI layers. It develops a loosely coupled application as the outer layer of the application always communicates with the inner layer via interfaces. When a class is dependent on a concrete dependency, it is said to be tightly coupled to that class. A tightly coupled object is dependent on another object; that means changing one object in a tightly coupled application, often requires changes to a number of other objects. It is not difficult when an application is small but in an enterprise-level application, it is too difficult to make the changes. All of the layers interact with each other strictly through the interfaces defined in the layers below.

Deja una respuesta