Part 01 - Clean Architecture Overview
Tags: architecture, csharp
Published: 2023-02-06 11:58:40

Clean Architecture: Maintainable, Testable, and Scalable Applications

Originally Posted on: https://www.arunyadav.in/codehacks/blogs/post/20/part-01-clean-architecture-overview

In software development, creating a well-designed and organized application is crucial for success. Clean Architecture is a software design principle that can help you achieve this goal by separating an application into distinct parts. This makes it easier to `maintain`, `test`, and `scale` the application over time.

The main idea behind Clean Architecture is to keep the business logic of an application isolated from external factors, such as presentation, infrastructure, and persistence. This separation of concerns makes it easier to change and maintain the application, as well as test it without affecting other parts of the code.

Clean Architecture consists of several circles or layers, each with a specific responsibility. The `innermost` circle is the business logic, which is the core of the application. This layer is isolated from external factors and communicates with other layers through well-defined interfaces. The business logic should not depend on the outer layers, and instead should rely on abstractions.

The `next layer` is the interface adapters, which are responsible for converting data between the business logic and the outer layers. This layer includes things like presentation logic, data access, and infrastructure.

Finally, the `outer layer` is the frameworks and drivers, which are responsible for providing external services to the application. This layer includes things like databases, web servers, and operating systems.

## Let's understand by Onion View


![Clean Architecture Onion View](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eawafstpf7jghlp43fkz.png)

_The flow of dependencies in the diagram moves towards the center. The centermost point is referred to as the Application Core, which acts as the nucleus of the diagram and is not dependent on any other layers. The entities and interfaces occupy the central position while domain services, which provide implementation to the central interfaces, surround them. The UI and Infrastructure layers, located on the exterior, are both dependent on the Application Core._

## Let's understand by Horizontal View


![Clean Architecture Horizontal View](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3yampttxy1jq5b2xmsd.png)

_In the clean architecture, the direction of solid arrows indicates compile-time dependencies, while the dashed arrow signifies a dependency that only exists at runtime. The UI layer interacts with the interfaces specified in the Application Core during compilation, without having knowledge of the concrete implementations in the Infrastructure layer. At runtime, these implementations become necessary for the operation of the app, and must be connected to the Application Core interfaces through dependency injection._

## Let's understand by ASP.NET Core architecture diagram following Clean Architecture.

![Clean Architecture ASP.NET Core View](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o54pfoyzrn4wnx54rhwz.png)

## Support For Docker

![Clean Architecture Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwi8127n9jzbi7xi8lyl.png)


Here is forked Gihub [https://github.com/engg-aruny/CleanArchitecture](https://github.com/engg-aruny/CleanArchitecture)

## Organizing code in Clean Architecture

**Application Core types**
- Entities (business model classes that are persisted)
- Aggregates (groups of entities)
- Interfaces/Contracts
- Domain Services
- Specifications
- Custom Exceptions and Guard Clauses
- Domain Events and Handlers
- Behaviours
- Mappers

**Infrastructure Types**
- Identity Services
- File Storage
- Queue Storage
- Payment Services
- Third-party services
- Notifications
- Email Services
- Sms Services

**UI Layer**
- Controllers
- Custom Filters
- Custom Middleware
- Views
- ViewModels
- Startup

**Persistence Types**
- Data Context
- Repositories
- Data Seeding
- Data Migrations
- Caching

**Domain Types**
- Domain Entities
- Domain Events
- Value Objects
- Aggregates
- Enums
- Constants

## Here is the diagram of all types into layers


![Clean Architecture View](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/beg2nkwyh70mwmoubj2f.png)

Source from [https://medium.com/dotnet-hub/clean-architecture-with-dotnet-and-dotnet-core-aspnetcore-overview-introduction-getting-started-ec922e53bb97](https://medium.com/dotnet-hub/clean-architecture-with-dotnet-and-dotnet-core-aspnetcore-overview-introduction-getting-started-ec922e53bb97)

## References
- [https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures](https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures)
- [https://medium.com/dotnet-hub/clean-architecture-with-dotnet-and-dotnet-core-aspnetcore-overview-introduction-getting-started-ec922e53bb97](https://medium.com/dotnet-hub/clean-architecture-with-dotnet-and-dotnet-core-aspnetcore-overview-introduction-getting-started-ec922e53bb97)
- [https://martinfowler.com/tags/domain%20driven%20design.html](https://martinfowler.com/tags/domain%20driven%20design.html)
- [https://jeffreypalermo.com/2008/07/the-onion-architecture-part-1/](https://jeffreypalermo.com/2008/07/the-onion-architecture-part-1/)