A translator at the border that keeps an old or foreign model from leaking into your clean design.
An anti-corruption layer (ACL) is a translator that sits between your clean design and another system, such as a legacy app or a third-party API, so that system's model does not leak into yours.
Imagine you are building a new system next to an old one. The old system calls a customer CUST_NM, writes dates as DDMMYY and stores a status as the number 1, 2 or 9. If your new code simply uses those fields, it starts to think in the old system's language. Every new feature repeats its oddities.
An anti-corruption layer is a small piece of code, a border guard and translator, placed between the two. The old system talks to the layer in the old language. The layer talks to your code in your language: a Customer with a fullName, a real date and a status such as active.
The idea comes from Domain-Driven Design. Two models (two bounded contexts) should not be forced to share words. The layer lets each keep its own model, and it handles the meaning of the translation: statuses, error codes, units, formats and even whole protocols.
| Part | Job | Example |
|---|---|---|
| Facade | A simple front for a messy interface | One call getCustomer(id) that hides three legacy calls |
| Adapter | Converts one interface into another | Turns a SOAP reply into a plain object |
| Translator / mapper | Converts the data and its meaning | ST_CD=1 becomes active, DDMMYY becomes a date |
| Error mapping | Converts the outside system's errors into yours | Legacy code 77 becomes a clear CustomerNotFound |
| Anti-corruption service | The layer runs as its own small service | Used when several systems need the same translation |
| Place | How it works | Good for |
|---|---|---|
| A module in your service | A package or folder inside the new app | One consumer, simple and fast |
| A separate service | A small service in front of the legacy system | Many consumers, or when the layer is complex |
| At the edge of a context | Each bounded context translates what it receives | DDD style systems with several teams |
legacy your domain CUST_NM = "SMITH, JANE" -> fullName = "Jane Smith" ST_CD = 1 | 2 | 9 -> status = active | suspended | closed REG_DT = "140305" -> registeredOn = 2005-03-14 (DDMMYY)
A teaching model: the records, the six code sites and the status codes are made up. The date rule (years 00 to 30 mean the 2000s) is an example of a rule you would have to agree with the owners of the legacy data. A real layer also translates errors, retries and the protocol itself.
CustomerDirectory.find(id). Your code depends on this, not on the legacy client.| Term | Meaning in easy words |
|---|---|
| Anti-corruption layer (ACL) | A translation layer that stops an outside model from leaking into your own design |
| Domain model | Your own description of the business: its words, rules and objects |
| Bounded context | A part of a system where one model and one language apply (from DDD) |
| Legacy system | An old system that still runs the business and is hard to change |
| Adapter | Code that converts one interface into another |
| Facade | A simple front that hides a complicated interface |
| Translator / mapper | Code that converts data and its meaning between two models |
| Port | An interface that your domain owns, which an adapter implements |
| Model leakage | Foreign names and rules creeping into your own code |
| Strangler Fig | Replacing a legacy system piece by piece, often with an ACL during the move |
| Unknown value handling | What the layer does when it meets a code it has no rule for |
| Choice | We gain | We lose |
|---|---|---|
| Use a layer | Isolation from a messy model | More code, and a place that must track the other system |
| No layer, use their model directly | Less code at the start | Their quirks spread, and every change hurts |
| Layer inside the service | Simple, fast, one deploy | Hard to reuse for other services |
| Layer as a separate service | Reusable translation for many consumers | A network hop and one more thing to run |
| Thin layer (translate only) | Easy to understand and test | Some rules must live elsewhere |
| Thick layer (with rules) | Hides a lot from callers | Hidden logic and a second source of truth |
ST_CD means.