Typorithm · medium

Hexagonal (Ports & Adapters)

hexagonal-architecturearchitectureportsadapterstestability
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Hexagonal (Ports & Adapters)

0%

Layered & Clean

Hexagonal architecture (ports and adapters) lets the core app define ports — interfaces for what it needs. Adapters for databases, HTTP, or email plug into those ports, so the core knows nothing about I/O and any adapter can be swapped, including a fake one for tests.

The core application defines ports: plain interfaces describing the inputs and outputs it requires, with no mention of databases, HTTP, or email.

Adapters live on the outside and implement those ports — an EmailAdapter, an SmsAdapter, or a test double can all satisfy the same NotificationPort.

Because the core depends only on the port, you can swap adapters freely, which makes the core trivially testable: inject a fake adapter and assert on it.

01

Ports are interfaces the core owns.

02

Adapters implement ports and hold the I/O.

03

The core knows nothing about concrete I/O.

// the port — an interface the core owns
interface NotificationPort {
  send(msg: string): void;
}

// an adapter plugs into the port
class EmailAdapter implements NotificationPort {
  send(msg: string): void {
    smtp.deliver(msg);
  }
}

// the core depends on the port, not on I/O
class SignupService {
  constructor(private notifier: NotificationPort) {}
  register(user) {
    this.notifier.send("welcome");
  }
}

Read the implementation

Connect each part of the code to the concept before you type.

1. A port (interface)

The port is an interface owned by the core; it describes what the core needs without naming any technology.

interface NotificationPort {

2. An adapter plugs in

The email adapter implements the port; other adapters (SMS, push) could plug into the same port.

class EmailAdapter implements NotificationPort {

3. I/O lives in the adapter

The SMTP call lives only in the adapter; the core never touches it directly.

smtp.deliver(msg);

4. Core depends on the port

The core service depends on the port interface, so it knows nothing about email or any concrete I/O.

constructor(private notifier: NotificationPort) {}

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Hexagonal (Ports & Adapters): Lesson & Practice · Typorithm | ApplyDjinn