Clean Architecture is an architectural pattern that separates concerns into layers, with dependencies pointing inward toward business logic.

Core Principles

Dependency Rule

  • Source code dependencies point inward only
  • Inner layers know nothing about outer layers
  • Business logic independent of frameworks, UI, database

Layers (from innermost to outermost)

Entities (Enterprise Business Rules)

  • Core business objects
  • Most stable, least likely to change
  • Pure domain logic

Use Cases (Application Business Rules)

  • Application-specific business rules
  • Orchestrates entities
  • Defines inputs/outputs (ports)

Interface Adapters

  • Converts data between use cases and external world
  • Controllers, presenters, gateways
  • Adapts external formats to internal

Frameworks & Drivers

  • External tools and frameworks
  • Database, web framework, UI
  • Most volatile, changes frequently

Benefits

  • Testability: Business logic tested without external dependencies
  • Independence: UI, database, frameworks are swappable
  • Maintainability: Changes isolated to appropriate layers
  • Flexibility: Multiple UIs, databases, frameworks possible

Key Patterns

Dependency Inversion

  • Business logic doesn’t depend on infrastructure
  • Infrastructure implements interfaces defined by business logic

Screaming Architecture

  • Project structure reveals what the application does
  • Not dominated by framework folders

Resources