Video in progress
When do you need an anti-corruption layer, and what does it cost?
When an external or legacy system's model differs sharply from your domain and you can't influence it, an ACL translates that model into your own language. The cost is an extra layer, mapping code, and constantly keeping an eye on the differences between the two models.
📖 Answer
An adapter fits an interface at the class level. An ACL translates meaning across a whole boundary: the other system's concepts, states and error codes must not "leak" into your domain.
Example: a bank's 20-year-old core system. There, the customer status is in a STATUS_CD column with values like 'A', 'B', 'Z9', dates are in YYYYDDD format, and amounts are a CHAR(18) in minor units. If these concepts come straight into your service, the whole codebase fills up with if ("Z9".equals(code)) and nobody knows what Z9 means.
An ACL usually consists of:
- a Translator, which converts external DTOs into domain objects, codes into enums, and formats into standard types;
- a Facade, which collapses the external system's tangled API (4 calls to do one thing) into one meaningful operation;
- error translation, turning
ERR-4471intoCustomerBlockedException.
When you don't need it: when the external model is nearly the same as yours, or when it's under your control and can be changed. Building an ACL between two internal services is often a sign that the teams aren't talking.
It has a cost too, and you shouldn't hide it:
- the mapping code is boring and error-prone: unless it's covered by contract tests and real data samples, it translates wrongly and silently;
- if the external system adds a new state, the ACL doesn't know about it. What to do when an unknown code arrives, fail or continue with
UNKNOWN, is a business decision, so get it settled in advance; - the layer often becomes a "dumping ground". Make it a separate module and name an owner.
I put separate metrics on the ACL: the number of unrecognized codes and translation errors. That is what shows first that the legacy system changed without warning.
Sign up to mark studied questions and save your progress.