SYS ARCHITECTLearning Platform
Settings
Theory

Clean Architecture Principles

📋 Overview

Clean Architecture is a software design philosophy that promotes the strict separation of concerns into concentric layers. The primary goal is to create systems that are independent of frameworks, UI, databases, and external dependencies. By placing business logic at the "core" and pointing dependencies inward, the architecture ensures that the most critical part of the application is the easiest to test and the hardest to break.


🏗️ Core Principles & Characteristics

  • The Dependency Rule: Source code dependencies must only point inwards. The inner circles (Domain/Entities) know nothing about the outer circles (UI/DB).
  • Separation of Concerns: Each layer has a specific responsibility (e.g., Domain for business rules, Infrastructure for persistence).
  • Framework Independence: The architecture does not depend on the existence of some library of feature-laden software. This allows you to use frameworks as tools, rather than forcing the system into their constraints.
  • Testability: The business rules can be tested without the UI, Database, Web Server, or any other external element.
  • Modularity: Logic is divided into cohesive modules that communicate through well-defined interfaces.

⚖️ Trade-offs: Pros & Cons

  • Pros:
    • Longevity: The core business logic remains unchanged even if the database (e.g., SQL to NoSQL) or UI (e.g., Web to Mobile) is swapped.
    • High Testability: High coverage of business rules is possible with fast unit tests.
    • Parallel Development: Different teams can work on different layers (e.g., UI vs. Core) simultaneously with minimal friction.
  • Cons:
    • Initial Overhead: Requires more boilerplate code (interfaces, DTOs, mappers) than a simple "Monolithic" or "Active Record" pattern.
    • Indirection: Can be harder to follow the flow of data initially due to the high number of abstractions and interfaces.
    • Over-engineering Risk: For small, simple CRUD applications, Clean Architecture can be excessive.

🌍 Real-World Implementation

  • Entities (Inner Core): Plain objects representing business objects with logic (e.g., User, Account).
  • Use Cases (Application): Orchestrates the flow of data to and from entities (e.g., TransferMoneyUseCase).
  • Interface Adapters: Controllers (Spring/Express), Presenters, and Gateways (Repositories) that convert data for the core.
  • Frameworks & Drivers (Outer Layer): Databases (PostgreSQL/MongoDB), UI frameworks (React/Vue), and 3rd-party APIs.
  • Hexagonal / Onion Architecture: These are variations of the same core principle (Ports & Adapters).

💡 Interview "Gotchas" & Tips

  • Don't Pass Entities Out: One of the biggest mistakes is passing Core Entities all the way to the UI. Use DTOs (Data Transfer Objects) at the boundary.
  • The UI is a Detail: In Clean Architecture, the Web or Mobile app is just a "delivery mechanism," not the center of the application.
  • DB as a Plugin: View the database as a "plugin" that provides persistence. Your business logic should work even if the DB is just an in-memory Hash Map.
  • Inversion of Control (IoC): Use Dependency Injection to provide concrete implementations of interfaces (defined in the core) from the infrastructure layer.

📐 Suggested Architecture Primitives

  • Domain Entities: Business logic and data structures.
  • Repository Interfaces: Contracts for data persistence.
  • Use Case Interactors: Specific business workflows.
  • Data Mappers: To convert between DB rows, Entities, and DTOs.
  • Dependency Injection Container: To wire up the layers at runtime.
Canvas