How to Build Microservices with Onion Architecture: Hands-On Experience

How to Build Microservices with Onion Architecture: Hands-On Experience

The user data is displayed in a tabular format and on this view, it has linked to add a new user, edit a user and delete a user. 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.

According to traditional architecture, all the layers are interconnected and significantly dependent on one another. For example, the UI layer communicates with business logic, which communicates with the data layer. There should be a separation of concerns because none of the layers in 3-tier and n-tier structures are independent. Such systems are complicated to comprehend and keep up with.

Hexagonal architecture (software)

We have already discussed the separation of concerns as one of the principles in Onion Architecture, but we must understand the differences in couplings. There are two kinds of couplings, i.e., Tight Coupling and Loose Coupling. Layers have a separation of concerns and are not tightly coupled. In addition, the onion architecture itself introduced certain problems.

The initial inspiration came from the OSI 7-layer Model defined by the International Standardization Organization. The obvious advantage of the Onion architecture is that our controller’s methods become very thin. We moved all of the important business logic into the Service layer. To pass data from controller to view, create named UserViewModel view model, as per the code snippet, mentioned below.

Each layer is coupled to the layers below it, and each layer is often coupled to various infrastructure concerns. However, without coupling, our systems wouldn’t do anything useful, but this architecture creates unnecessary coupling. 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. Now we create the third layer of the onion architecture which is a service layer.

This layer contains application entities and DB model classes which work independently. The Onion architecture was first introduced by Jeffrey Palermo, to overcome the issues of the traditional N-layered architecture approach which we discussed above. One other problem with n-tier architecture is its nature to create tightly coupled layers and this may lead to deployment complexity. It is one of the most popular Architectures in ASP.NET Core Applications. In this architecture, the Presentation Layer generally holds the part that the User can collaborate with, i.e, WebApi, MVC, Webforms, etc. Business Logic is likely the main piece of this whole arrangement.

Tag: software architecture

For example, this means that testing database accesses can be done thoroughly without a real database. The onion layer solves the problem of tight onion architecture coupling that we faced with n-tier architecture. This architecture has Domain Layer, Repository Layer, Service Layer and Presentation Layer.

onion software architecture

When I observe these patterns, I feel all three patterns are trying to advocate similar ideas. They all define a loosely coupled testable system that avoids any direct dependencies in terms of implementation, yet do so using their own terminology and each with specific nuances. They all suggest approaches to make software architectures more manageable and testable, but do so in their own way. The Model is used to move data between the View and the Controller, where the business logic executes any operations.

The Dependency Rule

Now, we define the configuration for the UserProfile entity that will be used when the database table will be created by the entity. The code snippet is mentioned below for the UserProfile mapping entity (UserProfileMap.cs). Now, we define the configuration for the User entity that will be used when the database table will be created by the entity. The following is a code snippet for the User mapping entity (UserMap.cs). I’ll be writing more about the Onion Architecture as a default approach for building enterprise applications.

I hope you’ll find my experience useful for your projects. You can check my github repository for technical details. The main issues we faced were related to maintaining the low connectivity of microservices.

onion software architecture

Especially this layer should know close to nothing about infrastructure. Infrastructure is only a plumbing support to the business layer, not the other way around. Development efforts should start from designing the domain-code and not the data-access, the necessary plumbing should be an implementation detail. Now we create a second layer of the onion architecture which is a repository layer.

What is Onion Architecture?

When a class is dependent on a concrete dependency, it is said to be tightly coupled to that class. A tightly coupled object is dependent on another object; that means changing one object in a tightly coupled application, often requires changes to a number of other objects. It is not difficult when an application is small but in an enterprise-level application, it is too difficult to make the changes. Overall, both Onion Architecture and Clean Architecture are powerful software design patterns that can be used to create modular, scalable, and maintainable software systems. While they share some similarities, they also have significant differences, and developers should choose the architecture that best suits their needs.

  • I.e. the inner circle shouldn’t depend on anything in the outer circle.
  • He wanted to develop a design approach for complex business applications by emphasizing the separation of concerns throughout the system.
  • It is based on the concept of layers, each representing a distinct set of responsibilities.
  • I’ve found that it leads to more maintainable applications since it emphasizes separation of concerns throughout the system.
  • The primary proposition of this architecture is good coupling.

It achieves this through interacting with the final layer, the Domain Model layer which is the representation of the high level data objects we use. Onion Architecture builds on the Ports & Adapters Architecture to add some internal organisation to the business logic of the application based on a few Domain Driven Design concepts. Adapters are the glue between components and the outside world. They tailor the exchanges between the external world and the ports that represent the requirements of the inside of the application component. There can be several adapters for one port, for example, data can be provided by a user through a GUI or a command-line interface, by an automated data source, or by test scripts.

Enterprise Business Rules

Next, we looked at the Infrastructure layer, where the implementations of the repository interfaces are placed, as well as the EF database context. This article introduced Onion Architecture in ASP.NET Core, using Entity Framework Core with the “code first” development approach. We used Bootstrap, CSS, and JavaScript for the user interface design in this application.

Principles

Building a monolith has always been the default Architectural Style. MCV came into play to solve those problems, by promoting “separation of concerns” between front-end and back-end. In other words, these are the design decisions that are more difficult to change as the system evolves, it’s the foundations that give support to feature development. // DB interface sets out the operations allowed on our database. Now we have the account, deduct the cost of the transaction from Andres balance and update it within our database.If the update fails, we need to return an error stating so.

This makes it a bad choice, for more technical-oriented services, e.g. a high-throughput proxy written in a reactive framework. On the other hand, working in a more rigid, but at the same time more expressive, and structured environment of a well-architected application, was a breeze and a real pleasure. Not to mention that the time required to introduce the change was smaller, and the estimates were more precise and predictable. Again, both Clean and Onion Architecture point in similar directions; they suggest that there should be a layer where you manage application specific logic sitting next to enterprise rules. The application is separated into layers, each with its own duties and concerns.

But not good for large and complex software applications. Also, if we will work with Microservice architecture it will get more complex to work with N-Layer architecture, this is one of the major problems with n-tier architecture. According to some authors, the hexagonal architecture is at the origin of the microservices architecture. Each component is connected to the others through a number of exposed “ports”.

Implementation of Onion Architecture

There are a lot of aspects to the Onion Architecture, and if we have a common term to describe this approach, we can communicate more effectively. There are two basic approaches to representing the layers in the code. The one that we used in our most recent project was to use a package naming convention. This is the layer where you place classes describing the core of your business.

Interfaces for these are defined in the Domain Services layer — IFareRepostory and IRouteService. RiderFareCalculator is implemented in this layer also, and it depends on the fare repository and route service interfaces declared in the same layer. Note that with this approach, we do not depend on the external service, rather the external service depends on our declared contracts. Application services also referred to as “Use Cases”, are services responsible for just orchestrating steps for requests and should not have any business logic.