πŸ›οΈ

Clean Architecture Folder Structure

System Architecture Intermediate 1 min read 200 words
Clean Architecture

Clean Architecture Folder Structure

An approach to organizing code by features rather than technical types, similar to Vertical Slice Architecture.

Core Principle

Focus on features, not types.

Instead of grouping files by type (Commands/Queries), group everything related to a single command (feature) in one folder.

Traditional vs Feature-Based Organization

Traditional (by type):

πŸ“ Application
β”œβ”€β”€ πŸ“ Commands
β”‚   β”œβ”€β”€ CreateBookingCommand.cs
β”‚   β”œβ”€β”€ CancelBookingCommand.cs
β”‚   └── ...
β”œβ”€β”€ πŸ“ Queries
β”‚   β”œβ”€β”€ GetBookingQuery.cs
β”‚   └── ...
└── πŸ“ Validators
    └── ...

Feature-Based (recommended):

πŸ“ Application
β”œβ”€β”€ πŸ“ FeatureFolder1
β”‚   β”œβ”€β”€ πŸ“ Feature1A
β”‚   └── πŸ“ Feature1B
β”œβ”€β”€ πŸ“ FeatureFolder2
β”‚   β”œβ”€β”€ πŸ“ Feature2A
β”‚   └── πŸ“ Feature2B
└── ...

Practical Example: Bookings Feature

πŸ“ Application
β”œβ”€β”€ πŸ“ Bookings
β”‚   β”œβ”€β”€ πŸ“ Cancel
β”‚   β”‚   β”œβ”€β”€ CancelBookingCommand.cs
β”‚   β”‚   β”œβ”€β”€ CancelBookingHandler.cs
β”‚   β”‚   └── CancelBookingValidator.cs
β”‚   β”œβ”€β”€ πŸ“ Confirm
β”‚   β”‚   β”œβ”€β”€ ConfirmBookingCommand.cs
β”‚   β”‚   β”œβ”€β”€ ConfirmBookingHandler.cs
β”‚   β”‚   └── ConfirmBookingValidator.cs
β”‚   β”œβ”€β”€ πŸ“ Reject
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ πŸ“ Reserve
β”‚   β”‚   └── ...
β”‚   └── ...

What’s the Idea?

I’m grouping the files based on the feature they belong to.

A use case will be a command/query and a respective handler. And often, there’s a command validator there.

Key Benefits

  1. Better cohesion - Related code stays together
  2. High coupling for a single feature - Everything you need for one feature is in one place
  3. Easier navigation - Find all related code instantly
  4. Scalability - Add new features without touching existing code

Relation to Vertical Slice Architecture

If you’re familiar with Vertical Slice Architecture, this idea is similar. Each vertical slice represents a complete feature from API to database, organized in its own folder.


Sources

  • Arhitectura/Clean arch folder structure.png

πŸ“š Related Articles