What is Clean Architecture?
Clean architecture refers to organizing the project so that it’s easy to understand and easy to change as the project grows.
The key concept is separating the files or classes into components layers that can change independently of other components.
Key goals of this architecture are:
- Independent of Frameworks. The architecture does not depend on the existence of some library of feature laden software. This allows you to use such frameworks as tools, rather than having to cram your system into their limited constraints.
- Testable. The business rules can be tested without the UI, Database, Web Server, or any other external element.
- Independent of UI. The UI can change easily, without changing the rest of the system. A Web UI could be replaced with a console UI, for example, without changing the business rules.
- Independent of Database. You can swap out Oracle or SQL Server, for Mongo, BigTable, CouchDB, or something else. Your business rules are not bound to the database.
- Independent of any external agency. In fact your business rules simply don’t know anything at all about the outside world.
Clean Architecture Layers
As you can see from below, layers are progressing in circles. Each flat needs only one inner circle. For example, the dependency of the Application layer is just the Domain layer.
Consist of elements that are among the indispensable elements that the project will definitely need. Domain layer is independent on any layer.
This is the layer where we generally do our logical operations. For example, it is the layer where database records are made, where the requests are processed and the validations are made. The only dependence of this layer should be the domain layer.
This layer contains external things to be added to the system. No layer should be attached to this layer.
Consist of UI or API which interface clients. There should be no logical operations in this layer.
Clean Architecture in a .Net Solution
I would like to share an application of my team which is an SPA application working with a REST Api as its backend. The REST Api is written as a .NET solution consist of 10 projects as below picture.
When map .Net projects to Clean Architecture, it can be mapped as below diagram (map by color).
1. You may see that our infrastructure project (the .EntityFrameworkCore) depend on Domain instead of Application layer. That because we think it use nothing in the Application layer, and putting dependencies like above is better for us.
2. Ideally, Application layer (project .Application) should depend on abstraction of Domain layer (project .Domain.Shared) to align with Dependency Inversion Principle of the SOLID Principle. However we are still using some concrete classes of Domain layer :(