Course map

Topic 050 / Core

Layered architecture

Chapter 4.4 · Dependencies and Application Structure

What and why

Separation of concerns (→ 49) says dependencies should point one way, but not which layers exist or in what order — without a fixed, named stack, every file ends up with its own idea of what it's allowed to call. Layered architecture organizes an application into a fixed stack — typically presentation, application, domain, infrastructure — where each layer depends only on the layers below it, never above, and never skips a layer upward. Builds on: → 49.

Diagram

Presentation    (HTTP handlers, request/response shapes)
     │ depends on
Application     (use-case orchestration: "complete a task")
     │ depends on
Domain          (entities, business rules, state machines → 46)
     │ depends on
Infrastructure  (database, external APIs, message brokers)

Dependencies point only downward. Domain never imports Infrastructure directly —
it defines the interface (→ 48, → 49); Infrastructure implements it.

How it works

  1. Keep HTTP/CLI/GraphQL concerns only in Presentation — request parsing, response shaping, status codes.
  2. Keep orchestration of one use case — "load the task, call complete(), persist, notify" — in Application, with no business rules of its own.
  3. Keep invariants and state transitions in Domain, framework-free: no HTTP types, no ORM base classes, no database imports.
  4. Keep everything that talks to the outside world — database drivers, HTTP clients, message brokers — in Infrastructure, implementing interfaces the Domain or Application layer defines.
  5. Enforce the direction with a simple check (an import rule, a code-review habit): if Domain imports anything from Infrastructure, the layering is already broken.

Practical example

(illustrative). In the task manager: TaskController (presentation) calls CompleteTaskUseCase (application), which calls Task.complete() (domain, enforcing the state machine → 46), which is persisted through the TaskRepository interface (defined in domain, implemented by PostgresTaskRepository in infrastructure).

When to use it

  • Use when: this is the sensible default for most applications — familiar, testable, and enough structure for a CRUD-shaped domain.
  • Avoid when: the domain is thin — a handler that validates input and writes one row does not need four layers wrapped around it.
  • Failure scenarios: a "layered" application where the domain layer silently imports an ORM base entity class for convenience — the layers exist in folder names only, every domain test now needs a real database, and the dependency direction is already violated.
  • Simpler alternative: a two-layer split (handler plus storage) for a genuinely simple CRUD service; add the full stack once business rules accumulate enough to need protecting.

Practice

(20–30 min; no lab services — any language). Take a handler that mixes validation, business logic, and a raw SQL call in one function; split it into the four layers above. Done when the domain layer runs and passes its tests with the database entirely removed (stub the repository interface with an in-memory fake).

Key takeaway

A fixed stack with dependencies pointing only downward is the default shape for most applications. Done when you can: name, for any file, which layer it belongs to and justify what it is allowed to import. Used next in → 51 (clean architecture names the same rule "dependencies point inward"), → 52 (hexagonal architecture names it "ports and adapters"), → 53 (modular monolith repeats this stack inside each module).

References

None — this topic makes no external claims.

Source directory

Search the guide