Move the Port Home
Medium About 25 min Free, no account needed
A *Port abstraction is declared beside its adapter, so the core reaches outward to see its own port. Move the single port into the core; duplicating it leaves a stray copy that fails the role-placement rule.
The brief
In a ports-and-adapters (hexagonal) architecture, a port is an abstraction owned by the application core. The adapters implement it. Dependencies point inward: the core never references the adapter layer.
This team already "extracted a port". An InventoryPort abstraction exists and OrderService
depends on it, so it looks decoupled. But the port is declared in Shop.Adapters, right next
to its SQL implementation. So Shop.Core carries a using Shop.Adapters just to see its own port,
and at the namespace level the dependency arrow still points Shop.Core to Shop.Adapters.
Worse: a *Port role is stranded in the adapter namespace, where ports do not belong.
The architecture rules flag both symptoms. The obvious move is to "give the core its own port": add
a fresh InventoryPort in Shop.Core, point OrderService at that one, and leave the adapter as
it was. Try that and you'll find it isn't that simple. The symptom (wrong layer) is protecting you
from a deeper problem, and the half-fix trips a different rule.
Figure out why the port is stranded in the adapter layer in the first place, then fix it so the
core genuinely owns its port, passing every architecture rule, not just the layering one. The
behavior must stay byte-for-byte identical: same inputs, same "reserved ..." output.
- Move the single port abstraction
InventoryPortintoShop.Core(don't add a second copy, don't inline the concrete store). - Keep
SqlInventoryStoreinShop.Adapters, now depending inward on the core's port. Shop.Coremust not depend onShop.Adaptersat all, and the only*Portin the system must live in the core.- Wire the concrete store in
Solution(the composition root).
Your submission must (1) still pass the behavior tests (identical reservation strings) and
(2) satisfy every architecture rule shown in the results: both the rule about which way the
layer arrow points and the rule about where a *Port role lives. A naive "duplicate the port in
the core" clears the layering rule but leaves a stray *Port in Shop.Adapters, so the
role-placement rule stays red; only moving the single port turns them all green.
The layering rule is a symptom. The real work is moving the one abstraction so the core owns it outright, with no copy left behind on the wrong side of the boundary.
How it is graded
You work across multiple files, and architecture rules turn the design boundaries into executable feedback.
Related guides
What it practices
- Ports and adapters
- Driving and driven adapters meet the application only through its ports.
- Dependency inversion
- Point dependencies at abstractions the core owns, never at outer mechanisms.
Related katas
- Config Is Infrastructure Medium Free The shipping core reads a framework configuration type directly, welding the domain to infrastructure. Invert it behind a core-owned options port, and don't just rename the framework wrapper into the core, which trips a second rule.
- The Layer Skip Medium Free A controller reaches past the application layer straight into infrastructure for the order total. Route the call back through the middle layer with a port the application owns; a thin pass-through that still touches infrastructure only moves the smell, it doesn't remove it.
- Two Sides of the Hexagon Medium Free A driving (web) adapter reaches the driven (persistence) adapter directly for a quick existence check. Inject the repository "via its interface" and you'll find the seam is still on the wrong side of the hexagon.
- Preserve the Opaque Cursor Medium Pro An export invents offsets from row counts even though the storage service owns continuation tokens. Hide adapter details behind materialized pages and return every opaque token unchanged.