How To Build Microservices With Onion Architecture

The LayerProductionPresenter uses the ILayerProductionApplicationService to open a factory and produces layers by using the previously opened factory. Because the application follows the strict layering pattern, the process layer has to translate domain objects into data transfer objects residing in the process layer . The presentation layer can only use these data transfer objects to present information on the views. Data held by the domain objects has to be translated from layer to layer. This architecture pattern is heavily leaning on the infrastructure.

This allows pushing the complexity of the infrastructure as far outwards as possible and therefore, the direction of coupling is toward the center. The biggest offender is the coupling of the UI and business logic to the data access. Did I just say that the UI is coupled to the data access?

Domain Layer

This section will discuss what those different deployment types look like from an architecture perspective. Typically the data that crosses the boundaries is simple data structures. You can use basic structs or simple Data Transfer objects if you like. Or the data can simply be arguments in function calls. Or you can pack it into a hashmap, or construct it into an object. The important thing is that isolated, simple, data structures are passed across the boundaries. We don’t want to cheat and pass Entities or Database rows.

St. Basil’s on Red Square: Symbol of Russian endurance – Russia Beyond

St. Basil’s on Red Square: Symbol of Russian endurance.

Posted: Fri, 10 Dec 2021 13:04:01 GMT [source]

ExceptionHandlingMiddleware with the dependency container, we would get a runtime exception, and we do not want that to happen. Great, we saw how we wired up all of the dependencies of our application. However, there are still a couple of things to take care of.


Nothing in an inner circle can know anything at all about something in an outer circle. In particular, the name of something declared in an outer circle must not be mentioned by the code in the an inner circle.

onion architecture

The onion architecture, introduced by Jeffrey Palermo, overcomes the issues of the layered architecture with great ease. With Onion Architecture, the game changer is that the Domain Layer is at the Core of the Entire Application.

Using Sqlite In A C# Application

The code snippet mentioned below is for the same action method for both GET and Post requests. To pass the data from UI to the controller to add a user, use the same view model named UserViewModel. The AuthorController has an action method named AddUser which returns the view to add a user. As the concept of dependency injection is central to the ASP.NET Core application, we register context, repository, and service to the dependency injection during the application start up. Thus, we register these as a Service in the ConfigureServices method in the StartUp class as per the following code snippet. We create one more interface named IUserProfileService.

Using dependency inversion throughout the project, depending on abstractions and not the implementations, allows us to switch out the implementation at runtime transparently. We are depending on abstractions at compile-time, which gives us strict contracts to work with, and we are being provided with the implementation at runtime. All of the layers interact with each other strictly through the interfaces defined in the layers below. The flow of dependencies is towards the core of the Onion. We will explain why this is important in the next section. 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.

Why Use Onion Architecture?

The first layer around the domain is typically we would place interfaces that provides saving and retrieving behaviors, called repository interfaces. Out on the edges we see UI, Infrastructure and Tests. The outer layer is reserved for things that potentially changes often, these things are intentionally isolated from the application core. I’ve spoken several times about a specific type of architecture I call “Onion Architecture”.

onion architecture

The diagram to the left depicts the Onion Architecture. I’ve never seen an example where the domain layer depends on infrastructure, not in the DDD book nor in any other architectural pattern. Check the link I posted earlier and you’ll see that Vaughn Vernon clearly separates the infrastructure into a “port/adapter” package, which is very similar to onion architecture.

Benefits And Drawbacks Of Onion Architecture

This project holds POCO class and fluent API configuration for this POCO classes. It can have access to both the database and UI layers. Software configuration management It develops a loosely coupled application as the outer layer of the application always communicates with the inner layer via interfaces.

onion architecture

Upon first sight the layer model seems straightforward and easy to adopt. Unfortunately developers often take the layering literally. By now you’ll have noticed that my onion is slightly different from other explanations that can be found online. Some of this is my own interpretation of the rules, and some of it is my pragmatic way of implementing it. The key proposition of Onion Architecture is a good coupling. In simple words, it is a dependency of one thing upon another.

What Is The Motivation For Splitting The Service Layer?

At the center part of the Onion Architecture, the domain layer exists; this layer represents the business and behavior objects. The idea is to have all of your domain objects at this core. Besides the domain objects, you also could have domain interfaces. These domain entities don’t have any dependencies. Domain objects are also flat as they should be, without any heavy code or dependencies.

It is much easier to build a microservice around a bounded context. Bounded context — each microservice is built around some business function and uses bounded context as a design pattern.

The architecture does not depend on the data layer, as in a traditional three-tier architecture; it depends on real domain models. Runs its own local copy of Elasticsearch, which manages cross-cluster search configuration for the deployment. This includes configuration for heavy nodes and search nodes , but not forward nodes . An analyst connects to the manager node from a client workstation to execute queries and retrieve data. Please keep in mind that a dedicated manager node requires separate search nodes. Vertical slice architecture is becoming the preferred method for refactoring legacy systems (“vertical slice” being a cross section of code that runs from the front-end deep down to the data layer).

The unique part of onion architecture pros and cons is – it has zero dependencies over data layers like classic multi-tier architectures. The traditional and most commonly used web application architecture isModel-View-Controller architecture which is one of the most widely adapted and appreciated architecture throughout the industry. If you have been using web forms to develop web application then adopting MVC architecture even fits better, because it provides the required framework to build web apps MVC way. But things doesn’t turn out as planned and leads to very tight coupling between UI and business logic and business logic to database logic. This is because you end up writing all of your logic in server side code (mainly aspx.cs files). This scenario produces very tight coupling over the period of time and system becomes a nightmare to maintain. It is an ASP.NET Core Web application in this sample but it could be a Unit Test or Web API project.

  • One of the most important thing to notice here that service interfaces are kept separate from its implementation, which shows the loose coupling and separation of concerns.
  • We are depending on abstractions at compile-time, which gives us strict contracts to work with, and we are being provided with the implementation at runtime.
  • The relaxed or flexible layering is less restrictive about the relationships between layers.
  • Each layer is coupled to the layers below it, and each layer is often coupled to various infrastructure concerns.

It’s much more likely that you choose the wrong database, than you choose the wrong accounting system, the accounting process. // DB interface sets out the operations allowed on our database. We have now set our expectations for anyone wishing to charge a user for a transaction within our Application Services layer. However, we are not doing anything useful at the moment with the transaction so from this and following the layers of Onion Architecture we need to define our Domain Services layer. I hope that presence of CoreUtils in the solution helps you to avoid an excessive interfaces creation. This might be the way to reconcile between the ‘architecture purists’ and poor developers who just want to get the shit done. However, neither of these two services must become a part of the CoreUtils because both highly depend on the boundaries, our application works within.


Leave your comment