When we design a system we tend to follow some architectural pattern. Architecture patterns help us to design systems that can be easily scalable and maintainable. There are different types of software architecture patterns but for this article, we are going to discuss Clean Code Architecture pattern. In the last few years, clean code architecture has become so popular. Let’s begin with its history. Do you know who introduces Clean code architecture? Robert C. Martin who also know as Uncle Bob introduce Clean Code Architecture. Clean Code Architecture divides the system into different layers which provide the following benefits.
- Testable: Business rules can be easily testable without any dependencies on UI, Database, or any external system.
- Independent UI: UI can be changed at any time without affecting business logic.
- Independent Database: The business rule shouldn’t depend on the database. you can change the database anytime to SQL or Oracle or Mongo DB.
- Independent of the external system: Business rules shouldn’t know about any external system like your payment gateway i.e you can change your payment gateway from one provider to another anytime without changing your business logic.
In Clean Code Architecture divide system into three-layer
- Presentation/Infrastructure Layer
- Application Layer
- Domain Layer
In Clean code architecture, we have a dependency rule which means our code dependencies can only point inwards. Nothing inner circle knows about the outer circle.
Presentation/Infrastructure layer: Presentation layer is one from where the user or other system going to interact with our system like Controls, gRPC services, APIs, Event consumers, CLI command. The presentation layer shouldn’t care about application logic. Its transfers the request to the application layer and covert response from the application layer to the user.
Infrastructure layer contains the actual implementation of the services or class where you are going to connect with an external system like database, adapters, other microservice, etc.
Application layer: Application layer consist of Use cases or Workflow services. Those services orchestrate the steps required to fulfill the commands imposed by the client. Use cases are the list of the action which needs to perform by an application like creating user, submit an order or take payment, etc. You can implement CQRS pattern to the application layer which is basically commands and queries. The application layer performs certain business logic and transfers the request to the domain layer.
Application layer is independent of infrastructure and data access concerns. You can define the interface in the application layer but the actual implementation should be at the infrastructure layer.
Domain layer: Domain layer contains application business rules. The domain layer consists of Entity, Aggregates, and Domain Event. An entity can be an object with methods or can be a set of data structures and functions. Aggregates combine multiple entities into a single. Domain events are used to notify the same domain that something happens to the domain.
Please find https://github.com/nibro7778/CleanCodeArchitecture code which demonstrate Clean Code Architecture
I have used Order domain to demonstrate the Clean Code Architecture.
- Order.Api (Presentation Layer)
- Contains only OrderController
- Only reference to Order.Application
- Order.Application (Application Layer)
- Contains use cases like SubmitOrder, GetOrder
- Each use cases contains indpendent each other. Also, you can notice I have used CQRC Pattern.
- Only reference to Order.Domain
- Order.Application layer has contract IOrderRepository actual implementation is in Order.Infrastructure
- Order.Domain (Domain Layer)
- Contains Order entity, Domain events and business rules
- Domain layer shouln’t reference to any other layer
- Order.Infrastructure (Infrastructure)
- Order repository implementation
- Only reference to Order.Application
The main rule of clean architecture is that code dependencies can only move from the outer levels inward. Code on the inner layers can have no knowledge of functions on the outer layers.