Anti-corruption Layer is one of the design pattern to enable application communicate with external system.
Problem
For some reasons, some of the services cannot use common method to communicate and it cannot be change due to their own constrains. However, application still need to get resource and execute command from it. It is require to convert the response and executed result to application compatible format.
It will be time consuming if each service implement it’s own logic to communicate such “out-standing” service. Also, it will be more complicate on change when such system and it will impact to all involved services.
Solution
Implement the middle layer between services which role as a middleman to:
- Collect consumer request;
- Transform request format / communication channel and send to target system;
- Receive target system response;
- Transform response format / communication channel and send back to consumer;
Anti-corruption layer is the middleman which do tasks above. When the target system change, it can eliminate the change scale by only need to update anti-corruption layer, consumer do not need to update in their side.
Use case
- Communicate with legacy or specific protocol (e.g. SOAP, HL7, etc)
- Communicate with legacy channel (e.g. COM / RPC call, etc)
Implementation
Consideration
Compare with façade and adapter
Façade is set of interface and adapter is a converter between different system.
Anti-corruption layer combine the characteristic of façade and adapter, it provides the interfaces / event handlers to collect request and send out response between target system; In addition, anti-corruption layer contains a certain logics to translate the content to fulfill each others needs;
Compare with Ambassador
Ambassador is focus on non-fictional requirement, and anti-corruption layer is a sort of converter, so certain business logic might contain in there.
Reference
- Anti-corruption Layer, Microsoft Azure
https://learn.microsoft.com/en-us/azure/architecture/patterns/anti-corruption-layer - The Anti-Corruption Layer Pattern, Ahmed Shirin, Dev.to
https://dev.to/asarnaout/the-anti-corruption-layer-pattern-pcd
Leave a Reply