DDD, Hexagonal, Onion, Clean, CQRS, How I put it all together @hgraca

In the final analysis, therefore, the onion architecture is also almost identical to the hexagonal architecture – it differs only in the explicit “domain model” at the center of the application core. The coupling also makes it unnecessarily difficult to upgrade the database or data access layer (e.g., to a new database version or a new version of the O/R mapper). Even though you said application layer doesn’t depend on any other layers, you immediately leak entity Framework and asp.net core into it by the means of dbset and iservicecollection. This is effectively the same as leaking infrastructure and presentation layer into application layer, you’re bound to the technologies and cannot replace them with anything else. This is how you can invert the dependencies to build scalable applications. The biggest and the most important change Clean/Onion forces on you is the injection of your persistence mechanism as a dependency along with other external services.

  • First of all, the term “Immutability” applied to a data structure such as a class means that objects of this class cannot change during their lifetime.
  • It can be confusing, with Layered Architecture being such generic term.
  • We do not have to worry about how it will be implemented.
  • The world view difference is how to handle infrastructure.

Ultimately, the key to success is understanding the principles behind these architectures and applying them appropriately to the problem at hand. I have used CleanArchitecture on roughly 10 projects in the last 5 years, and I’ve had a lot of success with it. It was easy to add new features, and scale the applications when necessary. CleanArchitecture can easily be broken down into multiple modules or services, if performance is suffering and there is a need to scale out. 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.

Build Secure ASP.NET Core API with JWT Authentication – Detailed Guide

Therefore what we do is that we create interfaces in the Application Layer and these interfaces get implemented in the external layers. This is also known as DIP or Dependency Inversion Principle. The Onion architecture, introduced by Jeffrey Palermo, overcomes the issues of layered architecture with great ease. With Onion Architecture, the game-changer is that the Domain Layer is at the Core of the Entire Application. In this approach, we can see that all the Layers are dependent only on the Core Layers.

In the Startup/ConfigureServices of the API project, add these lines to register the Versioning. I have written a detailed article on API Versioning in ASP.NET Core 3.1 WebApi. Feel feel to read it to get a complete idea of this concept. As our ApplicationDbContext is configured, let’s generate the migrations and ultimately create a Database using Ef Core Tools – Code First Approach. And in the Startup class/ ConfigureServices method of the WebApi Just Add the following line.

For example, all entities, repositories, and ORM libraries are also available in the presentation layer. This tempts developers to let the boundaries between the layers weaken, especially when they are pressed for time. This is another variant that I have noticed in many huge solutions.

Key Difference between Onion and Clean Architecture

Here we need to get the amount the user has spent on this month from the purchase module and then with the result we need to apply the discount for the user in the checkout module. Here applyDiscountUseCase calls the purchase module’s controller for the data and then applies the discount in the checkout module. Also, this is the layer that determines which Controller / Gateway to be called for the particular use case. Image by Robert C. MartinWe can see there are four layers in the diagram.

clean architecture vs onion

You might think “well you didn’t do Onion right” but it was projects led by the creator of Onion. Reusable code is written as the need for them is discovered, and not sooner. Starting with vertical slices essentially means you get adherence to YAGNI and KISS.

Hexagonal Architecture vs. Onion Architecture

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. The above components have somewhat independent business rules as well as separate layers for user and onion architecture system interface. Different layers of the clean architecture can be explained by the onion diagram given below. Layered Architecture or Three-tier achitecture consists of layers. On to bottom is database /persistence layer, where data is persisted.

clean architecture vs onion

Note, however, that in the example above, the transactions field is actually an interface reference. In all architectures the goal is allows the most stable things are not dependent on less stable things will change more frequently. If you change from Springboot to Micronaut in Java, Zin to Martini in Golang, WebAPI to Nancy in .NETCore, there should be no change in terms of how you define the core domain. Ideally, the core domain should be independent of the framework being used.


If you wish to learn more about various architecture techniques and when to use them, join us or follow this blog. Infrastructure is any code that is acommodity and does not give your application a competitive advantage. This code is most likely to change frequently as the application goes through years of maintenance. Web services are still fairly new, and the first version in .Net, ASMX, is already deprecated in favor of WCF.

Almost more so as I can directly test against my domain logic, my features, and make sure my controller status codes are working and call it a day. I read books and blogs, checked out the eShop projects, saw jason taylor and steve smith’s example projects, and tried a few POCs myself. I even made a tool that can scaffold out a Clean web api that works pretty nicely. Regardless of the below, I can pretty definitely say that a Clean organization is a solid approach. I guess my biggest question with the article is that I don’t know what this is solving and how it is solving it. I also think this is way overly complex for a solution which most people have addressed through M-V-C or M-V-V-M architectures just fine.

Database migration should be handled by your CI/CD pipeline. Your application code shouldn’t know nor care about such thing as the database schema version. We will do a simple test to ensure that our solution works.

clean architecture vs onion

However, I have covered a few of these topics in other articles in my blog already. You could go through them to understand the core concepts and to learn how everything works. Inside the v1 Folder, add a new empty API Controller named ProductController. Since this is a very basic controller that calls the mediator object, I will not go in deep. However, I have previously written a detailed article on CQRS implementation in ASP.NET Core 3.1 API. You could go through that article which covers the same scenario.

Application Business Rules

How exactly the adapter does this – whether it uses an O/R mapper and which one and in which version – is irrelevant from the application core’s point of view. By architecture, we mean the division of a system into components, the arrangement and characteristics of these components, and the way these components communicate with each other. CQRS is a development principle claiming that a method must be either a command that performs an action or a request that returns data. The main issues we faced were related to maintaining the low connectivity of microservices.

What do architectural patterns teach us?

Running migrations / seeding at startup might be a bit of performance bottleneck. Given the fact that ASP.NET Core has DI and Services, it would not make sense to not have a reference between these layers. Authentication, Response Wrappers, Error Logging and Job Processing is already covered in my other articles.

The contents of this post might make more sense if you read the previous posts in this series. The code samples are taken from an example repository, which you can find on GitHub. The control flow of the system, when the controller will receive an HTTP request will be as follows. Connect and share knowledge within a single location that is structured and easy to search. As we have seen, all three architectural styles share the principles of loose coupling and attempt to minimize moving parts by properly layering the application.

If we need anything from an external system or service, we can just create an interface for it and consume it. We do not have to worry about how it will be implemented. The higher layers of the Onion will take care of implementing that interface transparently. These are the blocks that build our application and are not affected by any external forces, that is, external changes won’t change them.

Having an extendable template for the design is extremely important when having other developers especially juniors working and contributing. This is part of what I’m liking about vertical slice versus clean. Feels like clean starts out a lot more complex, while features in VSA can vary in complexity depending on the feature itself. Project architecture varies widely depending upon the seniority of the devs and the type of project. You’ve very probably saved me countless hours of frustration using clean. I still have a lot to learn with how to do VSA well, but I’m excited to deep dive it.

I would like to point to a possible small oversight on the UML diagram. I have checked how you use repositories in your code and read in the article as well, that your repositories supposed to operate on domain entities. I therefore believe that you have missed a directional arrow pointing from the Repository to the domain Entity. Hexagonal architecture is not about the count of sides in a drawing, it’s about ports and adapters. The author later changed the name to ports and adapters exactly because of these kind of failures to see the point. Yes, you are right, I don’t create interfaces for the application services for the reason you mentioned.

Leave a Reply