Onion Architecture: Definition, Principles & Benefits

You do have some sense of the arrows are pointing in the right direction. The problem is semantically you’ve violated the layers. That domain layer shouldn’t know about databases. It shouldn’t know about, there’s other data that it might need.

  • The first layer around the Domain Model is typically where we would find interfaces that provide object saving and retrieving behavior, called repository interfaces.
  • Application services also referred to as “Use Cases”, are services responsible for just orchestrating steps for requests and should not have any business logic.
  • Then we saw how the Service layer was created, where we are encapsulating our business logic.
  • Order is an entity and has attributes like OrderId, Address, UserInfo, OrderItems, PricingInfo and behaviour like AddOrderItems, GetPricingInfo, ValidateOrder, etc.
  • Then, it’s going to fetch from the database.
  • That is, deciding how to break down the code we write.

Regarding using repository interfaces as ports, that is one way to go about it, but not the root reason i disagree with Palermo, as i hope u can see by my explanation above. Context really matters, and certain architectural implementations only make sense for a given size and complexity of a codebase. Both overkill and underkill are bad ideas.

What is the motivation for splitting the Service layer?

With onion architecture, there is only an object model at the lowest level, which does not depend on the type of database. The actual type of database and the way of storing data is determined at the upper infrastructure level. Onion architecture consists of several concentric layers interacting with each other towards the core, which is the domain. The architecture does not depend on the data layer, as in a traditional three-tier architecture; it depends on real domain models. There are more examples, but hopefully, you get the idea. We are hiding all the implementation details in the Infrastructure layer because it is at the top of the Onion architecture, while all of the lower layers depend on the interfaces .

onion architecture

An external API may provide WebHooks that call back into the infrastructure layer. Similarly, our own API may have some push functionality, such as WebHooks or SignalR. This is the fundamental principle behind the Ports and Adapters architecture. By inverting that project dependency, the business logic has no dependencies. There aren’t even transitive dependencies to libraries like EntityFramework this way, so we can’t accidentally use them in the business logic layer.

Programming with Palermo

If you listed the steps that the interaction layer took, that becomes like a script. If you do naturally stratify your code into actions at the top, calculations at the bottom, that’s all the onion architecture is.

You might validate it for stuff like, “Is this a valid UTF string?” That’s not part of your domain. If you’re writing accounting software, accounting does not have an idea of UTF, eight strings. Accounting software deals with transactions and balances and amounts of money and stuff like that.

Presentation Layer

Very briefly, the Onion Architecture is a layered architecture, but it’s an onion. The layers are circular instead of stacked vertically. The inner layer is your pure domain logic, and the outer layer is pure, meaning pure functions, immutable data, all that. Aggregates are stored in repositories, which are abstractions for data storage. They are ports that the domain defines and expects to be implemented in the outer layers. Other projects can implement the interfaces by creating adapters.

Application services also referred to as “Use Cases”, are services responsible for just orchestrating steps for requests and should not have any business logic. Application Services interact with other services to fulfil the client’s request. Let’s consider the use case to create an order with a list of items. We first need to calculate the price including tax computation/discounts, etc., save order items and send order confirmation notification to the customer. The application services can be only invoked by Infrastructure services. Data formats/structures may vary from layers.

Why Microservices Are Good for Our Project

You need to pass it all the data it needs. The interaction layer receives this web request. It validates it, which could be a pure function, but it’s not domain logic that is valid. It depends on whether you are validating it in terms of domain concepts. I like to view the whole application layer as this transaction boundary. From outside, we don’t worry about the transaction, we just send a command, and it either succeeds or fails.

  • Instead, we just project our query results straight onto the response object.
  • A lot of software engineering is about drawing boxes.
  • This also comes in line with the preferences expressed by Martin Fowler.
  • We’d most likely see a persistence project here, responsible for implementing our IRepository interfaces.

Then the calculation might determine, “Hey, I need some more data. Can you fetch that for me?” Who fetches that? They can’t fetch it because it’s a calculation. Then the action above it, it has to exit and return that. 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.


Services.Abstractions project does not reference any other project, we have imposed a very strict set of methods that we can call inside of our controllers. We are using a Web API built with ASP.NET Core to create a set of RESTful API endpoints for modifying the domain entities and allowing consumers to get back the data. Contracts project to define the Data Transfer Objects that we are going to consume with the service interfaces. We have already prepared a working project for you and we’re going to be looking at each of the projects in the solution, and talking about how they fit into the Onion architecture. The Onion architecture is a form of layered architecture and we can visualize these layers as concentric circles. The Onion architecture was first introduced by Jeffrey Palermo, to overcome the issues of the traditional N-layered architecture approach.

The money I could have earned if I hadn’t been doing the book. It is at least in its current modern form, hundreds of years old. There’s evidence that…You could find some evidence like, “Oh, this kingdom did it in 2,000, 3,000 years ago.” It was kind of it, but it wasn’t as encoded as we have it today. Then, we’ll pass that data in as an argument to the calculation.

Article was published on: 10/6/22

Author: Viktor Nikolaev

Victor is a professional crypto investor and stockbroker, specializing in such areas as trading on the stock exchange, cryptov currencies, forex, stocks and bonds. In this blog he shares the secrets of trading, current currency indices, crypt currency rates and tells about the best forex brokers. If you have any questions, you can always contact nikolaev@forexaggregator.com

Leave a Reply