Clean Code Galaxy Architect's Folder Structure Simple. Clean. Efficient.

By Benjamin

Amidst the dynamic landscape of software development, maintaining a robust and well-ordered project structure is as critical as the code itself. Clean Architecture Folder Structure, an enumeration from the software architecture philosophy of Robert C. Martin, epitomizes this significance. It's a dependency rule set that dictates how software should be structured, promoting testability, expedited development, and easier understanding of the system.

๐Ÿ”ต ๐‡๐จ๐ฐ ๐ˆ ๐’๐ญ๐ซ๐ฎ๐œ๐ญ๐ฎ๐ซ๐ž ๐Œ๐ฒ ๐‚๐ฅ๐ž๐š๐ง ๐€๐ซ๐œ๐ก๐ข๐ญ๐ž๐œ๐ญ๐ฎ๐ซ๐ž ๐๐ซ๐จ๐ฃ๐ž๐œ๐ญ๐ฌ (๐…๐จ๐ฅ๐๐ž๐ซ ๐›๐ฒ ๐…๐จ๐ฅ๐๐ž๐ซ) Clean Architecture isnโ€™t just about principles, itโ€™s about clarity. Aโ€ฆ | Pedro Constantino | 23 comments
๐Ÿ”ต ๐‡๐จ๐ฐ ๐ˆ ๐’๐ญ๐ซ๐ฎ๐œ๐ญ๐ฎ๐ซ๐ž ๐Œ๐ฒ ๐‚๐ฅ๐ž๐š๐ง ๐€๐ซ๐œ๐ก๐ข๐ญ๐ž๐œ๐ญ๐ฎ๐ซ๐ž ๐๐ซ๐จ๐ฃ๐ž๐œ๐ญ๐ฌ (๐…๐จ๐ฅ๐๐ž๐ซ ๐›๐ฒ ๐…๐จ๐ฅ๐๐ž๐ซ) Clean Architecture isnโ€™t just about principles, itโ€™s about clarity. Aโ€ฆ | Pedro Constantino | 23 comments

The essence of clean architecture lies in its principle of independent, testable packages. It advocates forisolating code into four to seven packages with well-defined responsibilities. These packages are arranged in a circular dependency structure, with no cycles, making it easier to understand the system's flow and maintain the packages independently.

FastAPI Folder Structure Explained (Beginner Friendly Guide)
FastAPI Folder Structure Explained (Beginner Friendly Guide)

The Core Packages of Clean Architecture Folder Structure

Leveraging the foundational clean architecture, let's delve into its key packages: Entities, Use Cases, Interface Adapters, and Cross-Cutting Concerns.

Frontend vs Backend Folder Structure Explained ๐Ÿ’ป๐Ÿš€
Frontend vs Backend Folder Structure Explained ๐Ÿ’ป๐Ÿš€

Each of these packages plays an integral role in lending the architecture its flexibility, testability, and independence. Understanding their roles and responsibilities is essential for effectively exploiting the benefits of clean architecture.

Entities Package

How I Structure My Clean Architecture Projects (Folder by Folder) - Makemychance
How I Structure My Clean Architecture Projects (Folder by Folder) - Makemychance

The Entities package encapsulates the core business rules of the system. It holds the classes that can exist independently and are not dependent on other parts of the system. These entities can be mapped to database records but are not defined by their database columns.

Entities are instantiated and manipulated by the Use Cases package. An example of an entity might be a `Customer` or `Order` class, with their own business rules and behaviors. For instance, an `Order` class might have a method to calculate its total cost, which contains the core business logic.

Use Cases Package

the flow diagram shows how to use it in an application with multiple functions and features
the flow diagram shows how to use it in an application with multiple functions and features

The Use Cases package is responsible for orchestrating the business rules of the application. It initiates and controls the flow of activities that make up the business use cases. This package is where the operations of the system are defined, encapsulating the business rules and the response to specific requests.

A use case could be `CreateOrder`, which would fetch relevant entities, apply business rules, and pass the order to the relevant output ports (usually interface adapters) once all operations have been completed.

Interface Adapters of Clean Architecture

How to Structure Backend APIs in Express.js (Clean Architecture)
How to Structure Backend APIs in Express.js (Clean Architecture)

The Interface Adapters category, also known as the input and output ports, is responsible for connecting the application to external entities that the application communicates with, such as databases, web services, and user interfaces.

Creating a clean interface makes it possible to write tests and swap out one implementation for another, promoting flexibility and testability. The adapters translate the data types used internally in the system with the external format required by the other systems.

a purple background with yellow dots and the words folder structure on top of each one
a purple background with yellow dots and the words folder structure on top of each one
Flutter Provider + MVVM Architecture Tutorial
Flutter Provider + MVVM Architecture Tutorial
a diagram that shows the different types of architecture and how they are used to describe them
a diagram that shows the different types of architecture and how they are used to describe them
How to Structure Large Express.js Projects (Clean Architecture Guide)
How to Structure Large Express.js Projects (Clean Architecture Guide)
Spring Boot Clean Architecture ๐Ÿ“๐Ÿš€
Spring Boot Clean Architecture ๐Ÿ“๐Ÿš€
Need an Architecture Portfolio That Stands Out? Hereโ€™s Your Dream Template!
Need an Architecture Portfolio That Stands Out? Hereโ€™s Your Dream Template!
an architectural brochure is shown with images and text on the front, in spanish
an architectural brochure is shown with images and text on the front, in spanish
Master ASP.NET MVC: Optimal Folder Structure for Developers ๐Ÿ“‚
Master ASP.NET MVC: Optimal Folder Structure for Developers ๐Ÿ“‚
an image of the black screen with different icons and symbols on it's side
an image of the black screen with different icons and symbols on it's side
the brochure is designed to look like an architectural building
the brochure is designed to look like an architectural building

Input Ports

Input ports receive the requests from the external world, like HTTP requests. They map external requests to commands or queries that can be understood by the application. For instance, an HTTP controller might capture a request and map it to a `CreateOrder` command, which can then be handled by the `Use Cases` package.

Input ports can be further categorized as:

  • Adapters: Software that translates data between two formats.
  • Drivers: Software that controls other software according to certain rules.

Each can possess three forms - controllers, presenters, and writers - depending on the nature of the external system they're connecting to.

Output Ports

Output ports send the responses generated by the application back to the external world, like HTTP responses. They map the responses back to the format expected by the caller. For example, an `OrderService` may use an `OrderRepository` to create an order and then pass it to an output port, which would then prepare the order data as an HTTP response.

Output ports can take the form of view models that represent the data to be displayed on the user interface, domain events that are pushed to external systems, and payload alike.

Cross-Cutting Concerns in Folder Structure

Cross-cutting concerns are services that cut across multiple bounded contexts. Unlike the traditional packages, which focus on specific business rules, these are more abstract and handle platform-level activities like logging, transaction management, and email services.

These concerns can be abstracted into separate packages and then injected into the right places in the architecture. The advantage of this approach is that it makes the system more modular and easier to understand.

In this architectural structure, new features can be added quickly, and changes in one part of the system have minimal impact on other parts. Each package has a specific responsibility, making them independent and easier to test. Above all, clean architecture promotes a clear separation of concerns, enhancing software maintainability and readability.

Implementing clean architecture folder structure requires careful planning and understanding, but the benefits it offers - improved testability, faster development, and easier system understanding - make it an invaluable approach that every software development team should consider.