Clean Architecture: quick setup in dotnet core 8
Published: 2023-12-30 18:04:47

> Bootstrapping Your .NET 8 Application with Clean Code Principles
Hello, fellow developers! Here, we're diving straight into setting up a .NET 8 API application emphasizing Clean Code principles. Clean Code is about readability, maintainability, and simplicity. Here's a quick guide to bootstrap your application structure effectively.


## Project Layout

- **Core**: Contains domain models, interfaces, and business logic.
- **Infrastructure**: Houses data access, third-party services, and any infrastructure concerns.
- **Application**: This layer contains application logic and ties the core to the infrastructure.
- **Presentation**: The entry point of the application, such as API or web UI.


## Why This Structure?
**Separation of Concerns**: Each layer has a distinct responsibility, making it easier to manage and understand.
**Flexibility**: Swap out infrastructure components with minimal impact on the core business logic.
**Maintainability**: Isolating business logic from external concerns makes it easier to maintain and test.


##  Pros and Cons:
_**Pros:**_

- Improved Readability: New team members can understand the architecture quickly.

- Ease of Testing: Each layer can be tested independently, allowing for more thorough unit tests.

- Scalability: Easier to scale and maintain applications when concerns are well separated.

_**Cons:**_

- Initial Overhead: Setting up layers and abstractions can take more time initially.

- Complexity in Simple Apps: For very small applications, this structure might be overkill and introduce unnecessary complexity.


## Now let's code this

Install .NET 8 SDK: Make sure you have the latest .NET 8 SDK installed on your machine. You can check by running `dotnet --version` in your terminal.

Create Solution File: Start by creating a solution file which will encompass all the projects.


```
dotnet new sln -n CleanCodeApp
```
### Create Projects: Create individual projects for each layer.

Core Project (Class Library):

```
dotnet new classlib -n CleanCodeApp.Core
```
Infrastructure Project (Class Library):

```
dotnet new classlib -n CleanCodeApp.Infrastructure
```
Application Project (Class Library):

```
dotnet new classlib -n CleanCodeApp.Application
```
Presentation Project (Web API or MVC, depending on your need):

```
dotnet new webapi -n CleanCodeApp.Presentation
```
### Add Projects to Solution:


```
dotnet sln add CleanCodeApp.Core CleanCodeApp.Infrastructure CleanCodeApp.Application CleanCodeApp.Presentation
```
### Set Up Dependencies:

- _Core_ should not have any dependencies.
- _Infrastructure_ depends on _Core_ for domain models.
- _Application_ depends on _Core_ and might reference Infrastructure for interfaces.
- _Presentation_ depends on _Application_ for processing requests.
Use the dotnet add reference command to add project references accordingly. For example:


```
dotnet add CleanCodeApp.Infrastructure reference CleanCodeApp.Core

```


**Wrapping up**
Adopting Clean Code principles in your .NET 8 application structure is an investment in the maintainability and scalability of your code. By separating concerns and adhering to principles of simplicity and clarity, you'll build a robust foundation that can grow and adapt with your needs. Keep it simple, clean, and maintainable! 

**_What next?_**
In a next post will dive into the dataflow in Clean Architecture