Capa Anticorrupción

ACL

Escrito por Darjus Kazakevic · revisado el

Translate a foreign model at your border, so its words never reach your domain.

Every outside system comes with its own model. A payment API, an old ERP, a carrier's CSV file: each has its own words, codes and odd rules. Use them directly and they spread. Their field names reach your classes, and their next breaking change touches everything.

An anti-corruption layer (ACL) stops this. Your domain talks to an interface written in your own words. Only the layer behind it knows the foreign model and does the translation. The cost is one more piece of code to maintain per integration.

En esencia — Put a translation layer between your model and a foreign one, so their names, codes and rules convert at the border instead of spreading into your code.

Cuándo recurrir a él

Recurre a él cuando

  • Calling a legacy system whose model you cannot change
  • Using a third-party API in or near your core subdomain
  • Replacing a legacy system piece by piece, with both systems live

Reconsidéralo cuando

  • The upstream model is clean and close to yours: conform instead
  • A generic subdomain where your own model adds little value
  • A one-off script or a throwaway integration

The moving parts

The port
An interface written only in your domain's words. It says what your context needs, not what the foreign system offers.
The translator
The mapping between the two models. At its simplest it renames fields; at its hardest it matches different ideas. All the ugly code lives here, on purpose.
The adapter
The code that talks to the foreign system (an HTTP client, an SDK, a file reader) and hands raw payloads to the translator.

Applying it well

Write the interface from your side first
Design the port from what your domain needs, as if the vendor did not exist. Copying the vendor's API, even with nicer names, lets their model back in.
Translate meanings, not only field names
Renaming fields makes a mapper, not an ACL. The real work is matching different ideas: their five order states onto your three, or their empty date onto your “not scheduled yet”.
Translate their errors too
HTTP codes, vendor error strings and timeouts belong to the foreign model. Turn them into your own domain errors at the border, or every caller must know what a 409 from that vendor means.
Keep business rules out of the layer
An ACL translates; it does not decide. When rules collect inside it, move them to the domain. Otherwise the layer that protects your model becomes a second model.

Errores habituales

  • A pass-through that only renames fields and calls itself an ACL
  • Vendor types leaking past the layer into domain method signatures
  • Business rules growing inside the translator
  • One shared ACL for several contexts, which couples them again