Skip to content

πŸ—οΈ Clean Architecture

Tools and frameworks (Express, Spring, NestJS, PostgreSQL) are details. A professional architecture allows you to postpone decisions about these details. The β€œCenter” of your application should only contain the business rules that make your project unique.


The five basic principles of object-oriented design and programming.

  1. S: Single Responsibility: A class/module should have one, and only one, reason to change.
  2. O: Open/Closed: Software entities should be open for extension, but closed for modification.
  3. L: Liskov Substitution: Objects of a superclass should be replaceable with objects of its subclasses without breaking the application.
  4. I: Interface Segregation: No client should be forced to depend on methods it does not use.
  5. D: Dependency Inversion: Depend on abstractions, not concretions. (The heart of Hexagonal Architecture).

The goal is to create a clear separation between the Core Logic and the External World.

  • The Core (Domain): Contains Business Entities and Use Cases. It has no dependencies on any external library or database.
  • Ports (Interfaces): Definitions of how the core wants to interact with the world (e.g., UserRepository interface).
  • Adapters (Implementation): The β€œglue” code.
    • Driving Adapters: Take input from the user (REST Controllers, CLI).
    • Driven Adapters: Take output from the Core to the world (SQL Implementation, SMTP Mailer).

A methodology for mapping complex business requirements into software.

  • Bounded Context: A clear boundary within which a particular domain model is defined and applicable. (e.g., β€œProduct” in the Sales context vs. β€œProduct” in the Inventory context).
  • Ubiquitous Language: Developers and Business Stakeholders must use the same terms in conversation and in the code.
  • Entities: Objects with a unique identity that persists over time (e.g., a User with an ID).
  • Value Objects: Objects defined only by their attributes (e.g., an Address or Money). They are immutable.
  • Aggregates: A cluster of associated objects that we treat as a unit for data changes. The Aggregate Root is the only gatekeeper for changes.
  • Repositories: Abstractions used to retrieve and store Aggregates.

AspectClassic Layered (N-Tier)Clean / Hexagonal
ComplexityLow (Fast to start)High (More boilerplate/files)
TestabilityHard (Database is coupled)Easy (Core is pure logic)
EvolutionHard (UI/DB changes leak)Easy (Replace adapters only)
Ideal ForSimple CRUD / MVPsComplex Systems / Long-term
  • Avoid Over-engineering: If you are building a small internal tool that just moves data from a form to a table, Clean Architecture is likely overkill.
  • Embrace it when: The business logic is complex, the project will last for years, or you anticipate changing your infrastructure (e.g., moving from a Monolith to Microservices).

πŸ’‘ Note: The most common mistake is β€œDogmatism.” Don’t force every single microservice into a full DDD/Hexagonal structure. Apply the level of architectural rigor that matches the complexity of the domain. Sometimes a simple Service/Repository pattern is enough.