Caching and Error Translation in the Resources Layer
Caching and error translation both hide something from callers: where data came from and how a vendor fails. See how the Resources layer handles each.
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.
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.
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;
}
}
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.
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.