c# Onion architecture in net core API
Basically, these 5 Classes would cover our CRUD Operations implementation. Make sure that you have gone through my article about CQRS for ASP.NET Core before proceeding. You can see the clear separation of concerns as we have read earlier.
The Tests and User Interface layer are interesting because they are similarly different. They both use the Application Services in the sense that they are its customers. The tests are fairly one dimensional in the sense that they perform actions and observe and validate the result. I highly recommend you to read the article from Jeffrey Palermo about Onion Architecture. This is just my point of view about the Onion Architecture.
Data Access Layer
We will implement the Mediator pattern later in this tutorial. Tip #2 – While running the application, you would see that it navigated to ../weatherforecast by default. In the WebApi Project, Properties drill down, you can find a launchsettings.json file. This file holds all the configurations required for the app launch. Thus, the swagger will open up by default every time you run the application.

The purpose of the article is to eliminate an uncertainty while answering to “where should I place my commonly reused code? Throughout my developer career, every rails-style application (.net MVC, spring MVC, whatever) has a folder/project onion architecture called “Utils”. Or some other very generic name with unknown purpose – up until I open this project and look onto the code. Sometimes there are two or more projects like that, written by different generations/teams of software developers.
What are the Layers of the Onion Architecture?
With Onion Architecture, the game-changer is that the Domain Layer (Entities and Validation Rules that are common to the business case ) is at the Core of the Entire Application. In this approach, we can see that all the Layers are dependent only on the Core Layers. Let’s look at one of the most popular Architecture in ASP.NET Core Applications.

The architecture does not focus on underlying technology or frameworks but the actual domain models. This means that external changes won’t have any effect on the core. However, the slightest changes in the core will significantly influence all other layers. So, if you decide to apply some new features too late, you are likely to get into a lot of trouble. Onion is the best in terms of separation and coupling problems.
Everyone has tasted onions. In pizzas, hamburgers, salads, etc. But have you ever tasted Onion architecture in your…
The architecture places a particular emphasis on the use of interfaces to decouple components. Different layers of onion architecture have a different set of responsibilities and accordingly, there are different testing strategies. The testing pyramid is a great framework that lays out the different types of tests.

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 “Onion Architecture,” a well-known software design, has a number of benefits for both businesses and developers. Some of the main advantages of onion architecture are listed below.
The Need to Follow an Architecture
Infrastructure implements Core repositories, Commands/Queries(in case of mediator pattern), and repositories are designed in a way to only return Models from the core. Persistence project has the DbContext, EntityConfigurations, and the DBModels(entities). The main idea is to use the access modifiers to ensure the architectural constraints. Simon Brown puts all the «use case» related stuff in one package. He can then reduce the access modifiers for all classes and interfaces within the component to hide them from the outside. This would also apply to the entities, even they are not explicitly shown in the diagram.
- I have written a detailed article on API Versioning in ASP.NET Core 3.1 WebApi.
- Onion architecture comes with very clear rules about priorities, dependence, and layers.
- We only need to focus our effort on the implementations of the Infrastructure layer.
- Data storage, networking, and security are just a few of the specifics that this layer takes care of when connecting with external resources.
One option to address the access problem is to package by component. This technique is described by Simon Brown in the clean architecture book, chapter «the missing chapter», page 318. And that’s why I included it inside my template because it can be useful for anyone. Also, I didn’t want to introduce CQRS and Event Sourcing with difficult examples with different databases and propagating changes from the Write DB to the Read DB. It is just a simple “DataBase” (there is no database access, just creating mocked data inside the Repository implementation).
“ Anxiety arises from not being able to see the whole picture. If you feel anxious but you are not sure why, try…
I just want to emphasize that the database is external. Some people are used to think applications where the center is a database running triggers, stored procedures or jobs. If you follow this Onion Architecture there are no possible database applications. Most of the applications might use a database as a storage service but only though some external infrastructure code that implements an interface that makes sense to the application core.
It holds all the logic related to the Business requirements. Now, every application ideally has its own dedicated Database. 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. Our fare calculation depends on external services such as routing information and fare models.
More from Tiago Martins and The Startup
In this architecture, the Presentation Layer generally holds the part that the User can collaborate with, i.e, WebApi, MVC, Webforms, etc. Business Logic is likely the main piece of this whole arrangement. It holds all the rationale connected with the Business prerequisites. Presently, every application in a perfect world has its own committed Database. To access the Database, we present a Data Access Layer.