The layersPorts and adapters, pointing inward
No narration yet
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?

Quiz

Which import is illegal

Which of these would break the layer rules?