Onion Architecture in ASP NET Core

Repositories, external APIs, Event listeners, and all other code that deal with IO in some way should be implemented in this layer. The Infrastructure Layer is the outermost layer of the Onion Architecture. It’s responsible for implementing all the IO operations that are required for the software. The application layer implements Application rules instead of Business rules. A Domain Service contains behavior that is not attached to a specific domain model.

onion architecture

The idea is to have all of your domain objects at this core. Besides the domain objects, you also could have domain interfaces. Domain objects are also flat as they should be, without any heavy code or dependencies. The outer layers of the architecture implement these interfaces.


Not easy to understand for beginners, learning curve involved. Architects onion structure mostly mess up splitting responsibilities between layers.

Onion Architecture uses the concept of layers, but they are different from 3-tier and n-tier architecture layers. Let’s see what each of these layers represents and should contain. One interesting thing that I found about Onion Architecture is that it’s more appealing for C# programmers than Java programmers, at least judging by the blog posts I found. Of course, that’s just a curiosity, I wouldn’t consider that an argument in the discussion on whether to apply the architecture or not. Note, I have chosen to call the most centre layer “Core” rather than Domain Models — a personal preference.

onion architecture

The https://globalcloudteam.com/ is a simple, robust, maintainable and extensible software architecture that fits perfectly in an Agile world. In the late 2000s Jeffrey Palermo, presented an architectural pattern called the Onion Architecture. The main purpose of this architectural pattern was to control coupling in a software system, to improve separation of concerns and to force the externalization of infrastructure. 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 . The Domain layer does not have any direct dependencies on the outside layers.

Value objects

Automation — microservices should be deployed and updated automatically and independently from each other. Manual deployment and updating would be challenging because even the smallest project comprises from five to ten microservices, while large systems may comprise up to 500 microservices. In fact, while there are numerous definitions of microservices, there is no single clear and unified definition. Broadly speaking, microservices are web services that create a type of service-oriented architecture. Honestly, it’s not completely new, but I’m proposing it as a named, architectural pattern. Patterns are useful because it gives software professionals a common vocabulary with which to communicate.

What we have is infrastructure depending on abstraction . A center layer never depends on the outside, always the inverse. In other words, all coupling is toward the center, and this is achived using inversion of control. It relies on dependency injection for doing it’s layer’s abstraction, so you can isolate your business rules from your infrastructure code, like repositories and views. Modifying the view layer should not break any domain logic. Modifying the database modeling should not affect the software’s business rules.


CQRS is a development principle claiming that a method must be either a command that performs an action or a request that returns data. Independence — each microservice should function independently from others. This is just my point of view about the Onion Architecture.

It acts just like a bag of data, while the behavior itself is implemented in a service. No direction is provided by the Onion Architecture guidelines about how the layers should be implemented. The architect should decide the implementation and is free to choose whatever level of class, package, module, or whatever else is required to add in the solution. In my implementation, I intend to demonstrate some of the key layers of this architecture and how they work together. This layer is the bridge between external infrastructure and the domain layers. The domain layers often need information or functionality in order to complete business functionality, however they should not directly depend on these.

Onion Architecture

Each layer/circle encapsulates or hides internal implementation details and exposes an interface to the outer layer. All layers also need to provide information that is conveniently consumed by inner layers. The goal is to minimize coupling between layers and maximize coupling within a vertical slice across layers. We define abstract interfaces at deeper layers and provide their concrete implementation at the outermost layer.

  • The idea is to keep external dependencies as far outward as possible where domain entities and business rules form the core part of the architecture.
  • It refers to the business knowledge that our software is trying to model.
  • This lack of organization at the project level leads to spaghetti code.
  • Implementation of features may be slower, because there are multiple layers to get through.
  • It can be hard to implement a service using Onion Architecture when you have a database-centric background.
  • DDD implies that you distinguish a certain bounded context, which is a set of entities tightly connected with each other but minimally connected with other entities in your system.

DDD implies that you distinguish a certain bounded context, which is a set of entities tightly connected with each other but minimally connected with other entities in your system. To organize business logic for our project, we used Domain-Driven Design . Network protocols — microservices interact with each other via network protocols such as HTTP and HTTPS. Small size — smaller microservices are easier to work with. It can be hard to implement a service using Onion Architecture when you have a database-centric background.

Benefits of Onion Architecture

With layered and hexagonal architectures understood, the time has come to talk about a relative – Onion Architecture, which puts a new spin on layers. The rider selects their destination, then are presented with an estimated price for their trip. Trip estimation is a business use-case, and it’s the one I’ve selected for our implementation. Figure 2 below outlines the domain within the application structure. The contract for IGeoLocation doesn’t mention any details of the underlying infrastructure. The actual implementation, withinSupermarket.Infrastructure.Http.Clients, uses the Google Maps API but you wouldn’t know it from looking at the interface.

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 of external layers. Having created a domain model and a web API, we needed to seamlessly connect them.

An Application Service is a piece of code which implements a use case. The Application Layer is the second most inner layer of the architecture. A Value Object is an object that has no identity, and is immutable.

This layer has an implementation of the dependency injection principle so that the application builds a loosely coupled structure and can communicate to the internal layer via interfaces. However, given the heart of the architecture being a domain model, I would say that you should definitely avoid mixing it with those less important concerns like UI. DR should be on the outer layer and reference the inner layers. By saying DR.Resolve it sounds like service locator anti-pattern.

Three gems worth discovering Taste, the Santa Fe dining scene santafenewmexican.com – Santa Fe New Mexican

Three gems worth discovering Taste, the Santa Fe dining scene santafenewmexican.com.

Posted: Wed, 09 Nov 2022 05:00:00 GMT [source]

Dependency Resolution needs to know where all of the implementations are so that it can resolve them when it needs to. The core consists of the domain layer, repositories layer and services layer. The number of layers in the application core will vary, however, the domain will always be the very center of the onion architecture. If you have very complex business logic, it would make sense to encapsulate it inside of our domain entities. But for most applications, it is usually easier to start with a simpler domain model, and only introduce complexity if it is required by the project.

Understanding Onion Architecture

For example, let’s say you are developing a banking system. Then, you are implementing a use case which lets the user check her or his account balance. The domain layer is the innermost layer of the architecture.

Consequently, the domain layer doesn’t have any dependencies on NuGet packages because it is used purely for business logic. It’s okay to use some NuGet packages in the core but it should be kept to the strict minimum. A key tenet of Onion Architecture is that dependencies flow inwards or laterally toward the core.

Do We Need Every Layer?

The rest of your code shouldn’t worry if you are storing your data in a database, in a file, or just in memory. It’s responsible for dealing with the persistence , and acts like a in-memory collection of domain objects. The Infrastructure Layer should not implement any business logic, as well as any use case flow. DTOs are well suited as objects with really specific formats and data. Example of a simple business ruleSo, for these given examples, if computers did not exist, the Business rules would still be applied.

Great, we have seen how to implement the Presentation layer. 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. Services.Abstractions project it will only be able to call methods that are exposed by this project.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.