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)