CURRICULUM PREVIEWRevision 1 · Open for reviewReview the full course
CONNECTING THE PIECES10 min read

Hexagonal ports and adapters

Build one booking use case in C# and Go.

Before you beginclean-architecture

The problem and the pattern

A port describes a conversation with the application. An inbound adapter translates HTTP into a reservation command. An outbound port lets the use case save a booking without knowing PostgreSQL. C# commonly wires this through DI; Go commonly wires constructors explicitly. The business use case is the same.

Follow the flow

Make it concrete

This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.

Focused example
C#
public interface IBookingStore
{
    Task SaveAsync(Booking booking, CancellationToken ct);
}
public sealed class Reserve(IBookingStore store)
{
    public Task ExecuteAsync(Booking booking, CancellationToken ct)
        => store.SaveAsync(booking, ct);
}
Go
type BookingStore interface {
    Save(context.Context, Booking) error
}
type Reserve struct { store BookingStore }
func (r Reserve) Execute(ctx context.Context, b Booking) error {
    return r.store.Save(ctx, b)
}
// reserve := Reserve{store: NewPgStore(pool)}

Trade-offs

  • Ports should reflect operations the application needs.
  • A single adapter does not make an interface mandatory.
  • Keep cancellation and errors meaningful across the boundary.

When to avoid it

Avoid exposing an entire ORM through a port: it leaks infrastructure and defeats the boundary.

Your turn

Replace the PostgreSQL adapter with a fake that records the booking. Which application code needs to change?

Reveal a worked answer

The composition root chooses the fake; the use case remains unchanged. Assert the saved booking and error propagation. Keep an integration test for the real adapter so the fake cannot conceal SQL bugs.

Keep exploring

One more piece connected.

Mark this lesson complete when you can explain the idea.