DEV Community

Cover image for Clean Architecture: quick setup in dotnet core 8
Mohamed M.
Mohamed M.

Posted on

Clean Architecture: quick setup in dotnet core 8

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
Enter fullscreen mode Exit fullscreen mode

Create Projects: Create individual projects for each layer.

Core Project (Class Library):

dotnet new classlib -n CleanCodeApp.Core
Enter fullscreen mode Exit fullscreen mode

Infrastructure Project (Class Library):

dotnet new classlib -n CleanCodeApp.Infrastructure
Enter fullscreen mode Exit fullscreen mode

Application Project (Class Library):

dotnet new classlib -n CleanCodeApp.Application
Enter fullscreen mode Exit fullscreen mode

Presentation Project (Web API or MVC, depending on your need):

dotnet new webapi -n CleanCodeApp.Presentation
Enter fullscreen mode Exit fullscreen mode

Add Projects to Solution:

dotnet sln add CleanCodeApp.Core CleanCodeApp.Infrastructure CleanCodeApp.Application CleanCodeApp.Presentation
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)