Said's Blog

.NET - Clean Achitecture

August 3, 20235 min read
image

The Clean Architecture is a software design pattern and architectural approach introduced by Robert C. Martin in his book "Clean Architecture: A Craftsman's Guide to Software Structure and Design". It aims to create maintainable, scalable, and testable software by enforcing a clear separation of concerns and decoupling the different layers of an application.

The Clean Architecture emphasizes the independence of the business logic from the external dependencies, such as frameworks, databases, or user interfaces. This separation helps in making the codebase easier to maintain, as changes in one part of the application do not affect other parts, as long as they adhere to the defined boundaries.


PS: Clean Architecture is not a strict and rigid framework but rather a set of guiding principles. The implementation may vary depending on the specific requirements and complexity of your project. In this article, we will explore Clean Architecture as it is implemented by Jason Taylor.

Key principles

Dependency Rule
Dependencies should point inwards, from the outer layers to the inner layers. This means the inner layers (entities and use cases) should not depend on the outer layers (frameworks and drivers). Inversion of control (dependency injection) is used to achieve this.
Separation of Concerns
Each layer should have its specific responsibilities, and changes to one layer should not affect others as long as the boundaries are respected.
Testability
By isolating the business logic from external dependencies, unit testing becomes more manageable as it allows for testing the core application logic in isolation.


PS: The Clean Architecture is not tied to a specific programming language or framework and can be adapted to different technologies. Its primary goal is to create flexible, maintainable, and robust software applications that can evolve over time without being tightly coupled to external details or technologies.

Layers

Domain
The domain layer in the clean architecture contains the enterprise logic:, entities, enums, exceptions, interfaces, types and logic specific to the domain layer. This layer lies in the center of the architecture where we have application entities, which are the application model classes or database model classes. Using the code first approach in the application development using Asp.net core, these entities are used to create the tables in the database.
domain project
Application
The application layer contains the business logic. All the business logic will be written in this layer. It is in this layer that services interfaces are kept, separate from their implementation, for loose coupling and separation of concerns. For example, if the application need to access a notification service, a new interface would be added to application and an implementation would be created within infrastructure.
application project
Infrastructure
This layer contains classes for accessing external resources such as file systems, web services, smtp, and so on. These classes should be based on interfaces defined within the application layer.
infrastructure project
WebUI
This layer depends on both the Application and Infrastructure layers, however, the dependency on Infrastructure is only to support dependency injection. Therefore only Startup.cs should reference Infrastructure.
webUI project

FAQ

  1. What is the difference between entreprise logic and business logic?
  2. The difference is that enterprise logic could be shared across many systems, whereas the business logic will typically only be used within a system.

References