Because it depends on the layers below it in the hierarchy, it can only call the methods that are exposed by the lower layers. The main idea behind the Onion architecture is the flow of dependencies, or rather how the layers interact with each other. The deeper the layer resides inside the Onion, the fewer dependencies it has. Onion Architecture adds additional layers to the application, which increases the complexity of the application.

What are the Layers of the Onion Architecture

In addition, the onion architecture itself introduced certain problems. It took us some time to distribute functional parts between appropriate layers. But eventually, this problem was practically eliminated. The main premise of this architecture is that it controls coupling. All three patterns are aligned on this principle; it emphasizes that source code dependencies should only point inward. The outer layer can only refer to the inner layer and not vice versa.

Introducing fullstackhero – Open Source Boilerplates for Rapid Web Development

Onion architecture uses the concept of the layer but is different from N-layer architecture and 3-Tier architecture. Without registering the 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.

I’ve found that it leads to more maintainable applications since it emphasizes separation of concerns throughout the system. I must set the context for the use of this architecture before proceeding. This architecture is not appropriate for small websites. It is appropriate for long-lived business applications as well as applications with complex behavior.

The Architecture of a Modern Startup

Notice, that the biggest file is the one for the infrastructure layer. The
infrastructure has all the framework — in this case Spring Boot — database driver, and other dependencies, and itself
depends on both domain and application. There’s onion architecture of course nothing preventing you from declaring extra dependencies,
say Lombok. The most important thing to note here is that with this build setup, it will not be possible to reverse the
order of dependencies between the layers.

Architecture patterns are the core of how we design our applications. In my  experience, understanding these rules has helped me to create extendable, testable and comprehensive software systems. Unlike human language, where words may mean different things depending on context, software does not handle ambiguity well. DOMAIN-DRIVEN DESIGNDDD is an approach where the main focus is on real business values more than on technologies, frameworks, etc. Concepts and technological details are important, but they are secondary.

Service Layer

Onion Architecture provides flexibility in the implementation of the application. The implementation of the Infrastructure layer can be easily changed without affecting the Domain layer or the User Interface layer. This provides flexibility in the choice of technologies and platforms used in the implementation of the application. Onion Architecture makes the application more maintainable. The clear separation of concerns between the layers makes it easier to modify and maintain the application. Changes in one layer do not affect the other layers, which reduces the risk of introducing bugs into the system.

What are the Layers of the Onion Architecture

Like the Onion Architecture, the layered approach requires coupling from one layer to another. However, with the layered approach, your database ends up being the base of your application. Let’s take a further look at the coupling in the layered approach. An application written to help manage a Library would most probably have classes like Book,
Reader, Copy and so on.


Also, the code is easier to test due to dependency injection, which also contributes to making the software more maintainable. Repositories, external APIs, Event listeners, and all other code that deal with IO in some way should be implemented in this layer. Your Domain models can have Value objects in their attributes, but the opposite is not allowed. Decoupling the back-end components of an application is certainly valuable, but what about the front end?

Domain and Application Layer will be at the center of the design. In N Layer Architecture, the Database is usually the Core of the Entire Application, i.e It is the only layer that doesn’t have to depend on anything else. Any small change in the Business Logics layer or Data access layer may prove dangerous to the integrity of the entire application.

Benefits of Onion Architecture:

To keep things clean, what we can do is, Create a DependencyInjection static Class for every layer of the solution and only add the corresponding. The outer layer is reserved for things that change often outside of the applications core business logic that interact with it. In the case of the UI, it is a service in itself, it will have its own logic and tests which validate it behaves as expected. If you are looking to test the entire system then that would be an end-to-end test that would be performed. So, like a typical onion, let’s work our way into the core and hopefully avoid any tears along the way.

  • This means that when a higher layer references the Services.Abstractions project it will only be able to call methods that are exposed by this project.
  • This contains the Core Business Logic as part of our project which acts as a layer between the Repositorylayer and Controller.
  • Based on the DDD model, we’ve created onion architecture (aka hexagonal or clean architecture).
  • This layer contains all logic related to UI (User Interface).
  • By now it should be obvious that the Presentation project will only have a reference to the Services.Abstraction project.
  • If we return mutable objects through the API, people using the code could gain access to domain components we might not intend to expose.

This layer will contain operation-specific orchestration and related logic for the application. If we look at them all together, they offer some useful architectural takeaways that are applicable regardless of the design approach you choose to use. We’ll explore them shortly, but first let’s have a look at what each of these patterns are about individually and how they compare to one another.

Taking Care of Database Migrations

The Domain layer is where all the business rules belong. By controlling the domain through the API, and inserting all business logic within the domain, we have a portable application. 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.