Video in progress

What is an anti-corruption layer, and when do you add one when integrating with a legacy system or an external API?

An anti-corruption layer is a layer that translates an external or legacy system's model into your own domain model. You need it so the external system's names, codes and odd rules don't leak into your code: if something changes on the outside, only this layer changes.

📖 Answer

Say order-service gets the customer status from a bank's old core banking system. The response looks like this:

{"CLNT_ST": "03", "BLK_FLG": "Y", "DT_UPD": "20260115"}

If this DTO goes straight into the domain code, a month later if ("03".equals(dto.getClntSt())) shows up all over the service. The day the core system changes the codes, you hunt down dozens of places to fix.

An ACL gathers this translation in one place:

public interface CustomerStatusPort {
    CustomerStatus statusOf(CustomerId id);   // domain language
}

@Component
class AbsCustomerStatusAdapter implements CustomerStatusPort {
    private final AbsClient abs;

    AbsCustomerStatusAdapter(AbsClient abs) { this.abs = abs; }

    @Override
    public CustomerStatus statusOf(CustomerId id) {
        AbsClientResponse r = abs.fetch(id.value());
        if ("Y".equals(r.blkFlg())) return CustomerStatus.BLOCKED;
        return switch (r.clntSt()) {
            case "01", "02" -> CustomerStatus.ACTIVE;
            case "03"       -> CustomerStatus.SUSPENDED;
            default -> throw new UnknownAbsStatusException(r.clntSt());
        };
    }
}

The domain knows only CustomerStatusPort and CustomerStatus. In tests you replace the port with a simple fake, with no core system needed.

When I add one:

  • when the external model differs noticeably from your domain (other terms, codes, merged fields);
  • when the external system is on a side you don't control and might change;
  • when splitting a service out of a monolith, so the new service doesn't get tied to the old monolith's model.

If both sides belong to one team and the model is nearly the same, an ACL is an unnecessary layer. Putting an "adapter + mapper + port" on every external call isn't a goal in itself.