A

clean architecture in 3 minutes

Clean Architecture Overview

  • Clean architecture breaks an application into logical components.

  • The key principle is that components do not need to be aware of each other.

Core Concept: Models

  • Models traverse the entire application.

    • Represent entities in the database or persistence mechanisms.

    • Handle incoming requests and outgoing responses.

  • Models serve as the only shared components in the architecture.

Application Flow

  • Request Handling

    • Requests come from APIs, socket listeners, or user interfaces.

    • Request arrives at the application layer.

  • Business Logic Processing

    • The application layer contains all business logic.

    • Logic organized into features or use cases correlating with user actions (e.g., register, sign in, add to cart).

    • Larger actions can invoke smaller actions to modularize operations.

Decoupling and Interfaces

  • The application layer is ignorant of:

    • Databases, repositories, or persistence.

    • Shared services or message buses.

  • Use of Interfaces

    • The application defines interfaces (e.g., an email class must have a send message method).

    • Adopt any service that matches the defined interface, enabling flexibility in swapping out services (e.g., database, email, notification).

Benefits of Clean Architecture

  • Prevents stray logic in repositories and web APIs.

  • Business logic remains centralized in the application layer, ensuring simplicity and clarity.

  • Application processes requests in a structured manner: receive, process, send response.

  • This separation of concerns enhances maintainability and scalability.