Clean Architecture And CQRS Pattern


In the article, we will see what a clean code architecture is, how it looks, and how it solves the problem of dependency of different layers in a project. On a high level, clean architecture can be defined as a system that strictly follows the principle of separation of concerns. In this architecture, the software is divided into many layers, which will simplify the development and maintenance of the system. This way, the layers which are divided can be reused and can be modified independently at any point in time.

Why Clean Architecture?

  • The business logic can be tested easily without having UI, database, or any external elements.
  • Any changes in UI will not affect the rest of the application as they are loosely coupled.
  • Implementation of any new features will be relatively easy which reduces the complexity of the solutions.
  • Debugging and finding the issues becomes easier for developer
  • Maintenance of the code is effective and it is a better solution for an agile environment.
  • It will be easier to understand the code whenever a new developer starts working on this code.
  • All the layers of the system are loosely coupled and change in one layer should not break the other layer.
Clean Architecture And CQRS Pattern
This is how the clean architecture looks like where the Domain is the heart of the system. If you can observe all the outer layers depend on corresponding internal layers not the other way.

The domain is the base and heart of this architecture. It basically consists of Domain models or Entities and its corresponding Entity configurations. This domain layer consists of repositories to deal with operations with databases. We can use the domain-driven design to design the domain project. Effective designing of domains is essential for the quick performance of the system.
Application Layer
Application is the core of the system which contains all the business logic from the outer layers and it only depends on the domain layer. It generally consists of DTO models, interfaces and command handlers, and query handlers which I will be sharing in the latter part of the article.
Presentation Layer
Presentation Layer is generally the UI part of the system where we get the form data to be saved into database and show all the visible results after the user requests for the data.
WebAPI is another form of the interface through which one can perform the operations on the system. This also depends only on the Application layer.
Infrastructure Layer
This layer consists of any kind of logic which needs to be communicated with 3rd party API and external systems. This layer depends only on the Application Layer.
This can be any third-party app that acts as an interface between two applications and try to do operations on our system.
Now Let have a look on CQRS principle on Application Layer.
CQRS stands for command query responsibility segregation. The name itself says that it is the segregation of commands and queries. Now let's see what the commands and queries are .
Commands mean any operations like Create, Update, and Delete which you define the system to do insert, change, and remove the data of the system. Here the command means telling the system to do these insert/modify/remove operations on the system.
Queries mean the operation like Read, which means you are asking the system to query the data requested by you. The queries helps you fetch data from the database.
Now let's see how the real implementation of clean architecture in our solution:
Clean Architecture And CQRS Pattern
If we can observe the current solution architecture there is a domain project, application layer, and Web API.
The domain project consists of domain models and its DTO’s. It has repositories which are used to handle all the db operations(CRUD). The DevelopmentDBContext is the dbContext which consists of all dbsets.
The application project consists of query handlers and command handlers. Each query and command handler is responsible for doing only one operation and one responsibility. Hence they are not dependent on others and they are independent of its operations.
The Web API is the layer through which we can perform the operations on the system. This API depends only on the Application layer. We can make the APIs independent of command or query handlers by using the Mediator pattern which I will be sharing in the next article. The next article will be an extension to this article through how we can achieve this architecture and how we can design our API’s so that the API’s won’t depend on the business layers. It will have the sample code base attached to it.