Onion Architecture: Definition, Principles & Benefits

The infrastructure layer is a bit more tricky. It is where you would want to add your Infrastructure. Maybe an Entity Framework Core Layer for Accessing the DB, a Layer specifically made to generate JWT Tokens for Authentication or even a Hangfire Layer. You will understand more when we start Implementing Onion Architecture in ASP.NET Core WebApi Project. Then we saw how the Service layer was created, where we are encapsulating our business logic. The main idea behind the Onion architecture is the flow of dependencies, or rather how the layers interact with each other.

You can check my github repository for technical details. CQRS is a development principle claiming that a method must be either a command that performs an action or a request that returns data. At times, we had to move a particular functionality into a separate microservice if it appeared in many places in the system. On the contrary, if some functionalities were tightly connected, we had to combine microservices into one. And the most challenging task was to find a balance between all these functions. Based on the DDD model, we’ve created onion architecture . Bounded context is a good fit for a microservices architecture.


Business logic can’t function if data access isn’t there. 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. The circles represent different layers of responsibility. In general, the deeper we dive, the closer we get to the domain and business rules.

onion architecture

But eventually, this problem was practically eliminated. These days we have a different idea of what should go in a layer than 30 years ago, and we are more careful about where we put domain logic, presentation logic, and so on. But the architectural principles are all essentially the same. “The entire database” is not an abstraction. This will be an Empty API Controller which will have API Versioning enabled in the Attribute and also a MediatR object. What is the aim of this Base Controller? It is just to reduce the lines of code.

Generate the Migrations and the Database

These architectural approaches are just variations of the same theme. Visualisation — use tools that allow you to visualise the collected data. It consists of algorithms that are essential to its purpose and implement the use cases that are the heart of the application. Layers are not tightly coupled and have a separation of concerns. Onion Architecture is widely accepted in the industry.

  • Tip #2 – While running the application, you would see that it navigated to ../weatherforecast by default.
  • We could create an initialization script, connect to the Docker container while it is running the database server, and execute the script.
  • I am promoting your framework as a solution.
  • In general, the deeper we dive, the closer we get to the domain and business rules.
  • Required services to the corresponding Class.

How much money do you charge for an overdraft on an account? How much is the monthly fee on an account? How long do you let an account stay open before you close it if there’s no activity on it? These are rules that businesses like banks and accounting firms have that aren’t really accounting, but they’re part of their business. These are things that change a lot, a lot more than accounting, that is. The second layer is where you put your business rules. This is part two of a three-part series I’m doing on functional architecture.

What is the Onion Architecture?

Improves overall code testability as unit tests can be created for separate layers without impacting other modules. No dependencies of the Internal layer with external layers. Another of my question is, What about Cross cutting concerns like logging, caching, … and something will use in all layers. After Layered and Hexagonalarchitectures, the time has come to talk about their close cousin – the Onion Architecture initially introduced in aseriesofposts by Jeffrey Palermo. It’s actually dumb, and that’s a good thing because you want the business rules to decide what happens.

  • CQRS gives us the power to scale the two concerns independently.
  • Every page is hand‑laid out by me so that it is optimally visually using every page so that you can get all sorts of cool diagrams in there and really understand stuff visually.
  • However, there are still a couple of things to take care of.
  • Always, these dependencies should be inward and never outward.
  • This ensures we focus on the domain model without worrying too much about implementation details.
  • This overall helps to improve the performance, maintenance and testability of the system.
  • We have prepared a project that follows the Onion architecture which we are going to use in the rest of the article.

Leave a Reply

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