Nguyen Le Phong

Name a Boundary, Then Give It Examples

A layer name on a diagram feels clear until the first real ticket arrives.

The whiteboard looked tidy. An API layer, a domain layer, an integration layer and a small box labeled "policy." For about ten minutes everyone agreed. Then the first real ticket came in ("normalize phone numbers at sign-up"), and three people pointed at three different boxes.

A small team at a wooden table sorts blank interface cards, with simple architecture boxes and arrows on a whiteboard behind them.
Cards on the table keep the boxes on the whiteboard honest. Each card is a real case the team has to place on one side of a line.

Names feel clear until someone uses them

A boundary with a name feels settled. Often it isn't. Repository, service, use case, adapter, policy, gateway: each word arrives carrying the history of every codebase the reader has worked in. Two engineers can both say "service" and picture completely different responsibilities.

Domain-Driven Design hit the same wall with business words. Martin Fowler's note on bounded contexts describes an electricity utility where "meter" meant different things in different departments: the link between the grid and a location, the link to a customer, or the physical device on the wall. Eric Evans's answer was to stop forcing one meaning everywhere and give each context its own model. Fowler's Ubiquitous Language asks teams to build a common, rigorous language between developers and users. "Rigorous" is the hard part, and a name on its own rarely gets you there.

Examples pin the boundary down

Examples are much harder to fake. Swap each abstract sentence for one concrete case:

  • Instead of "the policy layer decides eligibility," write one input and the expected answer: a customer with two overdue invoices tries to place a third order, and the result is "blocked: overdue balance."
  • Instead of "the adapter hides the provider," show one provider error and what the domain sees: the payment gateway times out, and the domain gets retryable, with no HTTP status code anywhere in sight.
  • Instead of "the use case orchestrates," trace one request from the handler to the repository and back, and write down who does what at each step.

The idea has been around for a while. Fowler wrote about Specification by Example in 2004, pointing out that examples are usually much easier to come up with than general rules, especially for the people outside engineering who the software is for. He also calls it perhaps his most used tool, but never his only one, because tests are always incomplete. That's fine here. You aren't trying to specify every case. You want enough cases that everyone draws the line in the same place.

A good example forces someone to own it

Back to the phone numbers. The moment you write that example down, someone has to say where normalization lives: handler, use case, domain or repository. The answer might be obvious once it's said out loud. Writing it down keeps it from living in one person's head.

Even better, make the example executable and keep it next to the code that owns it:

// domain/phone.test.ts
// The domain owns normalization. Handlers pass raw input through.
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { normalizePhone } from './phone'

const cases = [
  ['0912 345 678', '+84912345678'],
  ['+84 912 345 678', '+84912345678'],
  ['84912345678', '+84912345678'],
] as const

for (const [input, expected] of cases) {
  test(`normalizes ${input}`, () => {
    assert.equal(normalizePhone(input), expected)
  })
}

Now the decision is visible in the repo. A new teammate who wonders where normalization happens finds the test beside the domain code, and the cases show exactly which formats the team agreed to accept.

Examples also show when a line is in the wrong place

A diagram can look clean while the examples keep crossing the line. Patterns worth watching:

  • Every important case needs the domain to know provider details. The adapter contract is probably too thin (Ports & Adapters goes into what a good one looks like).
  • Every use case repeats the same eligibility check. That policy deserves its own explicit home.
  • Nobody can say which side an example belongs on, and the argument keeps coming back. The boundary itself is still fuzzy, and a better name won't fix it.

Matt Wynne's Example Mapping gives you a lightweight way to run this as a session. Rules go on blue cards, examples on green cards under each rule, open questions on red ones. A small group should get through a well-understood story in about 25 minutes. Two of his warning signs carry over nicely to architecture: a rule with lots of examples may be several rules tangled together, and a pile of red cards means the team still has a lot to learn.

Here's what that looks like for one boundary, the adapter between your domain and a payment provider:

Example Does the domain see it? Who handles it
Provider says the card was declined Yes, as declined Adapter maps the provider's error code
Provider times out Yes, as retryable Adapter maps it, the domain decides whether to retry
Customer pays in a currency you don't support Yes, as a business rule Domain rejects it before calling the adapter
Provider rotates its API keys No Adapter config
Provider renames a field in its JSON No Adapter parsing
Provider returns an HTTP 429 rate-limit response No Adapter backs off and retries

Three cases cross, three don't. If your team argues over a row, you've found the part of the boundary that still needs a decision.

Diagrams show the places, examples show the distance

Diagrams still earn their spot. They show the shape of a system fast. A diagram without examples is a map without distances, though: you know where the towns are, not how long the drive takes. A handful of concrete flows makes the map honest.

Examples also look after the people who join later. They don't need to inherit every argument that produced the architecture. They need enough visible behavior to see why the boundary exists and what would count as breaking it. If you keep decision records, the examples belong right next to the decision. And if you're still deciding where the lines should go at all, Choose the Boundary Before You Choose the Tool covers that earlier step.

Try this week

  • Pick one boundary in your system and write three examples that should cross it and three that shouldn't.
  • If one of them won't sit on either side, raise it as an open question before the next feature lands on that line.
  • Turn one example into a test that lives next to the code that owns it.
  • Next time someone proposes a new layer, ask them to trace one real request through it.
What did you think?

Frequently asked questions

Why aren't layer names enough to define a boundary?
Words like service, adapter or policy carry different meanings for different engineers. Domain-Driven Design ran into the same problem with business terms, which is why bounded contexts give each part of a system its own model. A name only becomes precise when the team agrees on concrete cases.
What kind of examples help define an architecture boundary?
One input with its expected result for a rule, one external error with what the domain should see, and one real request traced from the handler to storage and back. Each example forces the team to say which side of the line the behavior belongs on.
How do examples reveal a bad boundary?
If most important examples force the domain to know provider details, the adapter contract is too thin. If every use case repeats the same check, the rule needs its own home. If nobody can place an example, the boundary itself still needs a decision.
How can a team find boundary examples quickly?
Run a short Example Mapping session, a technique from Matt Wynne: rules on blue cards, examples on green cards, open questions on red. A small group can usually map a well-understood story in about 25 minutes.
Should boundary examples become tests?
Where you can, yes. A small test next to the code that owns the behavior keeps the agreement visible in the repo, and new teammates can read the cases to see exactly what the boundary accepts.