System ArchitectureBackend Architect

How to implement the Anti-Corruption Layer (ACL) pattern when integrating two domain systems and what is it used for?

Pass interviews with Hintsage AI assistant

Answer.

The Anti-Corruption Layer (ACL) is an architectural pattern used to protect the internal domain model of an application from the influence of external systems during integration.

Why it's needed: When your system interacts with others (e.g., inherited data and business logic from legacy software or third-party services), the external contract may change or contain distortions. ACL allows you to isolate the internal model from changes that are incompatible with your business logic.

How to implement: Additional components are introduced — adapters and facades, which transform external data/interfaces into internal objects. These can be services, mappers, special DTO classes.

Example in Python:

class ExternalOrder: def __init__(self, order_id, amount, created_at): self.order_id = order_id self.amount = amount self.created_at = created_at class InternalOrder: def __init__(self, id, total, timestamp): self.id = id self.total = total self.timestamp = timestamp class OrderAdapter: @staticmethod def from_external(external_order): return InternalOrder( id=external_order.order_id, total=external_order.amount, timestamp=external_order.created_at )

Here, OrderAdapter isolates the internal system from changes in the structure of the external order.

Key features:

  • Protection from uncontrolled changes in external APIs.
  • Transformation and filtering of data according to your rules.
  • Minimizing the influence of external domains on your code.

Trick Questions.

Can simple POJO/DTO mappers be used instead of a full-fledged ACL?

No, simple mappers do not protect business logic and are not fully-fledged adapters. ACL is a holistic layer that implements protected integration, including contextual transformation of commands, events, and business errors.

How many levels of transformation are permissible in ACL?

As many as necessary to ensure complete isolation. Typically, this is at least two: structure transformation and mapping of business logic (e.g., statuses, error codes, and access policies).

How to properly handle external system errors in ACL?

ACL should intercept and adapt external errors to internal exception classes or status codes. Never directly propagate errors from third-party systems into your application.