Four Patterns That Keep External Systems at Arm's Length

Every external system arrives with its own vocabulary: column names in snake_case, status codes that only the vendor understands, responses that bury the one value you wanted four levels deep. Let that vocabulary into your business logic and it will stay for the life of the product. The Resources layer is LVD's border post, and it runs on four classic patterns.

Translation is the whole job

Layer three, with a volatility of 0.6, is where LVD puts everything that touches the outside world. Its central duty is translation between the representations out there and the models of your domain. Nothing from a database, a vendor API or a message queue should reach the rest of the application without passing through it first.

The layer's dependency rule makes that duty manageable. Resources may depend only on Schema and Utils. External models come in, domain models go out, and the code in between has only your own types and pure helpers to work with. Even a very eccentric external API cannot impose its vocabulary on the business logic.

Four ways to keep your distance

  • Repository. A collection-like interface over storage. Callers ask for a trip, not a SQL row, and the database technology stays hidden. Column conventions, row mapping and any validation of what comes back sit in one place.
  • Adapter. A wrapper that presents a third-party interface through a contract your domain defined. When the vendor changes, or is replaced, the adapter is the only code that has to.
  • Facade. A simplified front for a sprawling subsystem. If a service offers dozens of options and your domain needs three, expose three.
  • Anti-corruption layer. A concept from Domain-Driven Design for keeping a foreign model, such as a legacy system's odd structures, from seeping into your own. It translates inbound data into domain terms and outbound requests into whatever the other side expects.

Two details of the repository are worth copying. It checks what it returns against the schema, so a stale or corrupted row is caught at the border instead of travelling into the domain. And any cache sits entirely inside it, so callers cannot tell whether a result came from memory or from the database.

The facade deserves a concrete picture too. Imagine a messaging vendor whose API covers templates, throttling rules, delivery windows and fallback channels. Your domain only ever wants to tell a rider that the car has arrived, so the facade offers a single notify(riderId, message) and keeps the rest of the vendor's surface out of sight.

The adapter is the easiest to see in code. The contract belongs to the domain, so it lives in Schema. One vendor's quirks stay in Resources:

// L1 Schema: what the domain needs
export interface Geocoder {
  locate(address: string): Promise<Coordinates | null>;
}

// L3 Resources: one vendor's quirks stay here
export class AcmeMapsGeocoder implements Geocoder {
  constructor(private client: AcmeMapsClient) {}
  async locate(address: string) {
    const hit = (await this.client.search({ q: address, limit: 1 })).results[0];
    return hit ? { lat: hit.position.y, lng: hit.position.x } : null;
  }
}

A quick audit for leaks

You can test your own boundary by reading the imports in your Tools. If any of them mention a vendor SDK, or if a vendor's field names show up in a tool's logic, the boundary has leaked. In the book's arrangement, Tools depend on the interface in Schema and never on the implementation in Resources, which is exactly what makes a replacement cheap.

The payoff shows up the day a vendor is replaced. The book's point is that only the adapter implementation should need modification, and that no code in Tools or above should notice. If swapping a provider means editing a tool, a piece of the vendor's vocabulary has crept past the border.

The four patterns also combine. A repository may use a facade over a driver, and an anti-corruption layer is often a set of adapters with a stricter attitude about naming.

The takeaway

All four patterns do one thing: they make the outside world speak your language before it gets near your logic. Choose the one that fits the shape of the dependency, then protect it with resilience patterns and sensible caching and error translation. The layer page has the short version.

What the book covers

  • A repository with an in-repository cache and row-to-domain mapping functions
  • A payment adapter and the vendor swap it makes cheap
  • How a facade shrinks a sprawling subsystem to the few calls the domain needs
  • Why the anti-corruption layer is a natural extension of Resources
  • Resilience, caching, error translation and testing for the same layer