DEV Community

Cover image for How To Approach Clean Architecture Folder Structure
Milan Jovanović
Milan Jovanović

Posted on • Originally published at milanjovanovic.tech on

How To Approach Clean Architecture Folder Structure

Clean Architecture is a popular approach to structuring your application.

It's a layered architecture and splits into four layers:

  • Domain
  • Application
  • Infrastructure
  • Presentation

Each of the layers is typically one project in your solution.

Here's a visual representation of the Clean Architecture :

Clean architecture diagram

How do we create this in our .NET solutions?

Domain Layer

The Domain layer sits at the core of the Clean Architecture. Here we define things like: entities, value objects, aggregates, domain events, exceptions, repository interfaces, etc.

Here is the folder structure I like to use:

πŸ“ Domain
|__ πŸ“ DomainEvents
|__ πŸ“ Entities
|__ πŸ“ Exceptions
|__ πŸ“ Repositories
|__ πŸ“ Shared
|__ πŸ“ ValueObjects
Enter fullscreen mode Exit fullscreen mode

You can introduce more things here if you think it's required.

One thing to note is that the Domain layer is not allowed to reference other projects in your solution.

Application Layer

The Application layer sits right above the Domain layer. It acts as an orchestrator for the Domain layer , containing the most important use cases in your application.

You can structure your use cases using services or using commands and queries.

I'm a big fan of the CQRS pattern, so I like to use the command and query approach.

Here is the folder structure I like to use:

πŸ“ Application
|__ πŸ“ Abstractions
    |__ πŸ“ Data
    |__ πŸ“ Email
    |__ πŸ“ Messaging
|__ πŸ“ Behaviors
|__ πŸ“ Contracts
|__ πŸ“ Entity1
    |__ πŸ“ Commands
    |__ πŸ“ Events
    |__ πŸ“ Queries
|__ πŸ“ Entity2
    |__ πŸ“ Commands
    |__ πŸ“ Events
    |__ πŸ“ Queries
Enter fullscreen mode Exit fullscreen mode

In the Abstractions folder, I define the interfaces required for the Application layer. The implementations for these interfaces are in one of the upper layers.

For every entity in the Domain layer , I create one folder with the commands, queries, and events definitions.

Infrastructure Layer

The Infrastructure layer contains implementations for external-facing services.

What would fall into this category?

  • Databases - PostgreSQL, MongoDB
  • Identity providers - Auth0, Keycloak
  • Emails providers
  • Storage services - AWS S3, Azure Blob Storage
  • Message queues - Rabbit MQ

Here is the folder structure I like to use:

πŸ“ Infrastructure
|__ πŸ“ BackgroundJobs
|__ πŸ“ Services
    |__ πŸ“ Email
    |__ πŸ“ Messaging
|__ πŸ“ Persistence
    |__ πŸ“ EntityConfigurations
    |__ πŸ“ Migrations
    |__ πŸ“ Repositories
    |__ #️⃣ ApplicationDbContext.cs
|__ πŸ“ ...
Enter fullscreen mode Exit fullscreen mode

I place my DbContext implementation here if I'm using EF Core.

It's not uncommon to make the Persistence folder its project. I frequently do this to have all database facing-code inside of one project.

Presentation Layer

The Presentation layer is the entry point to our system. Typically, you would implement this as a Web API project.

The most important part of the Presentation layer is the Controllers, which define the API endpoints in our system.

Here is the folder structure I like to use:

πŸ“ Presentation
|__ πŸ“ Controllers
|__ πŸ“ Middlewares
|__ πŸ“ ViewModels
|__ πŸ“ ...
|__ #️⃣ Program.cs
Enter fullscreen mode Exit fullscreen mode

Sometimes, I will move the Presentation layer away from the actual Web API project. I do this to isolate the Controllers and enforce stricter constraints. You don't have to do this if it is too complicated for you.

Is This The Only Way?

You don't have to follow the folder structure I proposed to the T. Clean Architecture is very flexible, and you can experiment with it and structure it the way you like.

Do you like more granularity? Create more specific projects.

Do you dislike a lot of projects? Separate concerns using folders.

I'm here to give you options to explore. But it's up to you to decide what's best.


P.S. Whenever you're ready, there are 2 ways I can help you:

  1. Pragmatic Clean Architecture: This comprehensive course will teach you the system I use to ship production-ready applications using Clean Architecture. Learn how to apply the best practices of modern software architecture. Join 950+ students here.

  2. Patreon Community: Think like a senior software engineer with access to the source code I use in my YouTube videos and exclusive discounts for my courses. Join 820+ engineers here.

Top comments (0)