Demystifying software architecture patterns

Demystifying software architecture patterns

These architectural approaches are just variations of the same theme. This is my summary of this great video Hexagonal, Onion & Clean Architecture. I love this video because to me it really captures the essence of the hexagonal architecture in a fun and entertaining way. 8.Screaming Architecture • The blueprint of a church/library/hospital emphasizes different considerations that of a family residence, as the “use cases“ are different. Considerations on implementation detail (building material, color, etc.) should be secondary.

Upgrading infrastructure components (e.g., upgrading the database server or ORM library to a new version). Having created a domain model and a web API, we needed to seamlessly connect them. This approach makes it possible to create a universal business logic that is not tied to anything. Based on the DDD model, we’ve created onion architecture . Bounded context is a good fit for a microservices architecture. It is much easier to build a microservice around a bounded context.

Primary or Driving Adapters

The first image is a very high-level conceptual abstraction of how you should think about the various layers of your application or service. It introduces class concepts like Repository, UseCase, and Presenter. Note that inside a region, inputs and outputs flow in any direction. However, once a boundary is reached, green and red arrows only flow in one direction.

clean architecture vs onion

@amon Personally I’ve never once seen a layered architecture that could actually replace its database layer with a different technology after the fact without large amounts of effort and refactoring. Turns out having a direct reference to the DB specific code virtually guarantees that someone is going to rely on a leaky abstraction. But yeah might be doable for small applications with maybe 100k LOC.

Enterprise Business Rules

Tests sit at the outskirts because the application core doesn’t couple to them, but the tests are coupled to the application core. We could also have another layer of tests around the entire outside when we test the UI and infrastructure code. To the left here I have attempted to represent traditionally layered architecture using concentric circles. With Hexagonal Architecture you design a decoupled software that allows major decisions to be deferred, all business rules will be isolated from peripheral concerns. And you have the option to try different adapters with less effort.

  • Onion-architecture-ASP.NET-Core is a C# library typically used in Architecture, Swagger applications.
  • The internal flow of data between business domains isn’t as clear as it could be.
  • The secondary ports can be connected to test doubles, e.g., in the form of stubs to answer queries from the application or spies to record events sent by the application.
  • The best place I can see this is happening is actually on the ViewModel, each ViewModel may need to map it’s own data, so let’s change the implementation of the service again.
  • The use of an interface ensures that our code is testable and also that our domain layer is independent of any implementation of the repository.

Onion Architecture explicitly separates technical concerns from business logic by placing them in the outer layers of the application. In Onion Architecture, dependencies flow from the outer layers toward the innermost core layer. This means the core layer is entirely decoupled from the outside world and can be tested independently of other components. CleanArchitecture also promotes loose coupling between the various components of the system. This means that it’s easier to swap out external dependencies or make other modifications without affecting the core business logic. This can be especially useful when it comes to upgrading or replacing technology.

Onion Architecture / Clean Architecture

As far as it gives what the application asks for, the application doesn’t even know the change in DB or API. Image by authorNow we can see that no inner layer depends on any outer layer. Simply by having an Interface between these 2 layers, we could invert the dependency.

One other problem with n-tier architecture is its nature to create tightly coupled layers and this may lead to deployment complexity. Clean Architecture places a particular emphasis on the use of dependency inversion to decouple components from technical concerns. Clean Architecture places a particular emphasis on using interfaces to decouple components, allowing components to be easily swapped out or replaced. With Hexagonal, all dependency is filtered out of your Logic like a sieve, leaving only pristine dependency-free Logic code left over. You now want to put as much code as you can in the Logic module, because just by putting it there you remove its dependencies and make it more reusable. By following the principles of CleanArchitecture, you can create a flexible and maintainable codebase that is well-suited to evolving requirements and technology.

clean architecture vs onion

What I do is to go directly from UI to Infrastructure, but this is discussed with the team. Knowing and understanding all of these concepts will help us plan for a healthy architecture, a healthy application. The goal, as always, is to have a codebase that is loosely coupled and high cohesive, so that changes are easy, fast and safe to make.


I have already written a detailed article on MediatR and CQRS patterns in ASP.NET Core 3.1 WebApi Project. You can follow that article and add the Required Commands and Handlers to the Application Layer. You can see the clear separation of concerns as we have read earlier.

clean architecture vs onion

Presentation Layer – This layer presents the UI of the application. Repository Layer – This layer works as a middle layer between Domain and Service layers. In this article, we will see Onion architecture in detail. One more point, this article is very important from an interview perspective. Before we proceed, we should understand what was the concern with prior architecture i.e.

Keynotes Clean Architecture

I have tried to make it as simple as possible to understand how the entire architecture is designed. But I guess there is a lot of room for optimizing the content. However, you could also check the code on my GitHub to get a detailed understanding.

Coordinating deployments across 37,000 stores and 500,000 database instances for 6+ years with zero incidents? But it’s the reality for one Fortune 100 fast-food empire who chose a NoSQL point-of-sale database designed to smartly scale.Learn how RavenDB excels as a point-of-sale DB. We could create an initialization script, connect to the Docker container while it is running the database server, and execute the script. But this is a lot of manual work, and it is error-prone.

From N-Tier to Clean Architecture with .NET

To organize business logic for our project, we used Domain-Driven Design . Bounded context — each microservice is built around some business function and uses bounded context as a design pattern. In fact, while there are numerous definitions of microservices, there is no single clear and unified definition.

On the left we implement the driving actors and on the right we implement the secondary actors. 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. Implementation of the application hexagon according to tactical design, i.e., with entities, value objects, aggregates, services, etc. The hexagon is also great for drawing in two primary ports on the left and two secondary ports on the right. Cockburn says he’s never encountered a project that required more than four ports for its schematic representation. Some libraries, such as Hibernate, allow the technical instructions to be defined in an XML file instead of using annotations in the model class.

Advantages of the Hexagonal Architecture

We had several procedure documents just devoted to documenting the crazy steps we had to go through to do some things – like adding a new database table. Speaking of domain logic, VSA still accommodates a domain based approach. I can establish a bounded context and abstract out domain logic as needed.

It just has a few less-than-ideal architecture choices (for long-lived solutions) that prevent me from recommending it as-is. Also validation clutters the code of controllers, should be moved to a separate class (because of OOP+SRP which are the foundational principles behind CQRS). Adding a layer of abstraction for ORM or cloud provider are the best example of faux abstraction, the one that add no good but unnecessarily complicates the codebase. There are special cases when you need but again in most cases it makes no sense. You can move the composition logic in a separate project but in most cases it makes no difference, nor sense.

So many service consumers _coincidentally_ need the same functionality… And with our CA architecture it was impossible to decouple and reconcile the different functionality, because the injected service was itself coupled to all of it’s underlying services. Just because you move the controller logic to a mediatr request, doesn’t mean it’s VSA.

Reader’s Choice

With onion architecture, there is only an object model at the lowest level, which does not depend on the type of database. The actual type of database and the way of storing data is determined at the upper infrastructure level. The main problem with onion architecture this architecture is that all layers are built on top of the Data Access Layer and are, in fact, tied to a certain type of data storage. The Entity Framework partially solves this problem, but it supports a limited number of database types.

No Comments

Post A Comment