Whetstone.
The layersPorts and adapters, pointing inward
Module 5 · Lesson 112 min

Ports and adapters, pointing inward

Fusion is layered, and the layering is the architecture. There are four layers, and there is exactly one legal direction for a dependency to point: inward, toward the domain. The domain knows nothing about anyone; everyone else knows the domain.

The layer map (apps/fusion-backend/CLAUDE.md)
domain/  <-- application/
                  |
                  v
          persistence/
          presentation/

The mechanism is ports and adapters (the hexagonal architecture). A port is an interface the inner layers define for something they need (“I need to save a note”). An adapter is an outer-layer class that implements it (“here’s how you save a note to Postgres”). The inner layer depends on the interface it owns; the outer layer depends on the inner layer to implement it. Dependency points inward even though data flows out.

This is not new to you, it’s your Nest instinct with the arrows made strict. In Nest you inject an interface and register a provider for it. Same idea. The discipline Fusion adds is that the interface must live in the inner layer and the implementation in the outer one, so the domain never takes a dependency on EF Core, HTTP, or anything infrastructural.

Nest/Next → Fusion, the same idea
providerthentokenA port: an interface the application/domain defines
@Injectable()An adapter: an outer-layer class implementing the port
moduleRoughly the bounded context / layer boundary
DIthencontainerSame: constructor injection wires adapters to ports
Practice

Try it yourself

Recall

Port versus adapter

Define both and say where each lives.

In Fusion's hexagonal layout, what is a port, what is an adapter, and which layer does each live in?

Reveal answer

A port is an interface an inner layer (application or domain) defines for something it needs, e.g. IFlightNoteRepository. An adapter is the outer-layer (persistence/presentation) class that implements it, e.g. FlightNoteRepository. The port lives inward, the adapter outward, so dependencies point toward the domain.

Quiz

Which import is illegal

Which of these would break the layer rules?

  1. Aapplication/ imports from domain/
  2. Bpersistence/ imports from application/ and domain/
  3. Cdomain/ imports from persistence/
  4. Dpresentation/ imports from application/
Show answer

Correct answer: C — domain/ imports from persistence/

domain/ importing from persistence/ points the dependency outward, which is illegal. The domain must depend on nothing; everyone depends on it. The other three point inward or across correctly.

Sign in to track your progress →