Software Architecture The Onion Architecture by Shivendra Odean

IAPIDateFormatter implementation must know how API wants to receive the date objects. Would it be UTC timestamp, or should it be sent with user’s timezone offset? With the offset, set in the global runtime configuration? It totally depends on a logic of the application and on a logic of the web server.

onion architecture

We will implement the Mediator pattern later in this tutorial. Now add a Product Class that inherits the Id from the BaseEntity. Create a new class Entities/Product.cs in the Domain Project. To keep things simple but demonstrate the architecture to the fullest, we will build an ASP.NET Core Web API that is quite scalable. For this article, Let’s have a WebApi that has just one entity, Product. We will perform CRUD Operations on it while using the Onion architecture.


We can implement this layer in many ways, for example creating a REST API, gRPC, etc. These are just some of the examples of what we could define in the Domain layer. We have to realize that everything is a tradeoff in software engineering. Conceptually, we can consider that the Infrastructure and Presentation layers are on the same level of the hierarchy.

The Bundesautobahn 66 (A66) connects Frankfurt with Wiesbaden in the west and Fulda in the east. The Bundesautobahn 661 (A661) is mainly a commuter motorway that starts in the south (Egelsbach), runs through the eastern part and ends in the north (Oberursel). The Bundesautobahn 648 (A648) is a very short motorway in the western part which primarily serves as a fast connection between the A 66 and the Frankfurt Trade Fair. Yes, onion architecture is well-known in the programming world, and many applications use its principles. You could say that it’s another approach to solving a business problem.

Domain Model/Entities

This layer creates an abstraction between the domain entities and business logic of an application. In this layer, we typically add interfaces that provide object saving and retrieving behavior typically by involving a database. This layer consists of the data access pattern, which is a more loosely coupled approach to data access. We also create a generic repository, and add queries to retrieve data from the source, map the data from data source to a business entity, and persist changes in the business entity to the data source. I’ve spoken several times about a specific type of architecture I call “Onion Architecture”.

onion architecture

The new building complex was designed to accommodate up to 2,300 ECB personnel. The end of the war marked Frankfurt’s comeback as Germany’s leading financial hub, mainly because Berlin, now a city divided into four onion architecture sectors, could no longer rival it. In 1948, the Allies founded the Bank deutscher Länder, the forerunner of Deutsche Bundesbank. Following this decision, more financial institutions were re-established, e.g.

Onion Architecture explained — Building maintainable software

These services just communicate with external resources and don’t have any logic. External notification Service, GRPC Server endpoint, Kafka event stream adapter, database adapters. This layer lies in the center of the architecture where we have application entities which are the application model classes or database model classes. Using the code first approach in the application development using core these entities are used to create the tables in the database.

  • 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.
  • We often don’t keep systems up-to-date because it’s impossible to do.
  • Famous singer Namika was born in Frankfurt to Moroccan parents.
  • The Gothic building was constructed in the 14th and 15th centuries on the foundation of an earlier church from the Merovingian time.
  • Note, that infrastructure is still present as a fragment of external onion circle.
  • In a microservice architecture, modularisation may or may not make sense depending upon the complexity and use-case.

Russisch-Orthodoxe Kirche (or St. Elizabeth’s Church) atop the Neroberg Hill in Wiesbaden is truly a stunner. You don’t need to be an architecture expert in order to be mesmerized by its five golden, onion-shaped domes, especially when they dazzle in the sun. Next to this gorgeous sandstone church, you’ll find the biggest Russian cemetery outside Russia.


By organizing the codebase according to this folder structure, developers can easily navigate and modify different components of the application. The folder structure promotes separation of concerns, with dependencies flowing inward, adhering to the dependency rule of Onion Architecture. Dependencies flow inward, with inner layers having no knowledge of outer layers.

We will explain why this is important in the next section. The modular design facilitates the introduction of new technologies or frameworks without affecting the core business logic, enhancing the scalability and future-proofing of the application. To demonstrate a common folder structure based on, let’s consider a hypothetical e-commerce application. It can be hard to implement a service using Onion Architecture when you have a database-centric background.

Setting Up EF Core on the Persistence Project

Onion Architecture solved these problem by defining layers from the core to the Infrastructure. It applies the fundamental rule by moving all coupling towards the center. This architecture is undoubtedly biased toward object-oriented programming, and it puts objects before all others.

We have over 450 specialists with various backgrounds so that we can assemble a team for any project. There are multiple interfaces, making it easy to get lost in the whole mix. As a result, you’ll waste a lot of time figuring which interface to use. This is another thing I wanted to promote with my team (and also more teams). When designing a distributed architecture I think that CQRS (Command Query Responsibility Segregation) and ES (Event Sourcing) can help a lot. Of course, they are not a Silver Bullet, they are just patterns that you can follow if your microservice needs it or not.


By doing dependency injection in all the code, everything becomes easier to test. The inner layers shouldn’t know if your application is being exposed through an API, through a CLI, or whatever. 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, each domain aggregate has its own repository (if it should be persisted), so you could have a repository for Accounts, another for Customers, and so on. For example, let’s say you are developing a banking system.






Leave a Reply

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