Onion Architecture In ASP NET Core With CQRS Detailed

It depends on the use cases and the complexity of the application. It is also possible to create more layers of abstractions depending on application needs. 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.

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.

Result Service

Aliaksandr is a Senior .NET developer at SaM Solutions with 13 years of experience. Being a Microsoft certified engineer, he specializes in web development and has experience in creating desktop and mobile solutions. Aliaksandr is fond of learning new technologies, conducting meetups and teaching newbies at internal company courses. Instead of each module being responsible of instantiating it’s own dependencies, it has its dependencies injected during it’s initialization. This way, when you want to test it, you can just inject a mock that implements the interface your code is expecting to. Also, the code is easier to test due to dependency injection, which also contributes to making the software more maintainable.

onion architecture

You can find the source code of this implementation on my GitHub. One outer layer which may surprise many is Infrastructure. Is the database we use or an external dependency not part of our domain model layer?

Onion Architecture In .Net 5

You can see that we are adding the API Versioning data to the route attribute and also creating an IMediator object. Just to make our solution a bit clean, let’s also add API Versioning to the WebAPI. 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.

But in the case of front-end applications, we present the data using the UI by consuming the APIS. The obvious advantage of the Onion architecture is that our controller’s methods become very thin. We moved all of the important business logic into the Service layer. There are more examples, but hopefully, you get the idea. In the Services.Abstractions project you can find the definitions for the service interfaces that are going to encapsulate the main business logic.

Advantages of Onion Architecture

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 onion architecture of external layers. Application is divided into layers where each layer has a set of responsibilities and addresses separate concerns. Each layer acts as modules/package/namespace within the application.

onion architecture

In the Application layer, the FareRepository is able to retrieve data from external sources and transform it into meaningful Business Entities. Business Logic behaviour is declared as contracts with the use of interfaces in a Object-Oriented context. The primary proposition of this architecture is good coupling. It is the level of dependency of one thing upon another. The higher the coupling, the lower the ability to change and evolve the system. This library provides almost limitless opportunities for setting data validation rules.

Create and Configure Azure Network Watcher

I’m intentionally ignoring infrastructure here because this typically varies from system to system. We often don’t keep systems up-to-date because it’s impossible to do. If coupling prevents easily upgrading parts of the system, then the business has no choice but to let the system fall behind into a state of disrepair. This is how legacy systems become stale, and eventually they are rewritten.

onion architecture

We can use lower layers of the https://www.globalcloudteam.com/ to define contracts or interfaces. The outer layers of the architecture implement these interfaces. This means that in the Domain layer, we are not concerning ourselves with infrastructure details such as the database or external services.


Add the library project in your application and give a name to that project Repository layer. However, since the Web application and the database server will be running inside of containers, how are we going to create the actual database for the application to use? We could create an initialization script, connect to the Docker container while it is running the database server, and execute the script. But this is a lot of manual work, and it is error-prone. Notice that we create a switch expression around the exception instance and then perform a pattern matching based on the exception type.

  • The idea is to have all of your domain objects at this core.
  • I have added the XML file to the root of the API Project.
  • Around the domain layer are other layers, with more behaviors.
  • All the business logic should be implemented as a part of domain services.
  • We could create an initialization script, connect to the Docker container while it is running the database server, and execute the script.
  • In this article, I will tell you about my experience of using onion architecture with a harmonized combination of DDD, ASP.NET Core Web API and CQRS for building microservices.

I have already written a detailed article on MediatR and CQRS patterns in ASP.NET Core 3.1 WebApi Project. You can follow that article and add the Required Commands and Handlers to the Application Layer. A curated list of awesome articles, videos, and other resources to learn and practice software architecture, patterns, and principles. This is a simple use-case but the real question being asked is why.

Introducing fullstackhero – .NET 6 WebAPI Boilerplate

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. In core, you will have repositories talking to DAL and domain models, and services (not web services) dealing with repositories and domain models. However, this architecture pattern is not a silver bullet to every problem. As with all software problems, we need to evaluate whether or not we need this additional abstraction as it is more suited for larger applications with many engineers working on them.

Join The Discussion

Compare listings