Example of using the anti-corruption layer pattern
Is this project, it's illustrated how to use this pattern to avoid corrupting the Application (or Domain) layer with changes external projects.
The Application has a UseCase corresponding to buy a book Operation. The first thing to get is detailed information about this book, through an external project (Api.BookInfo.Legacy). The interface to this external service requires the code used to be a string.
![](https://private-user-images.githubusercontent.com/10137467/237730223-f057ec66-470d-4fdf-a327-403485787655.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxNDI1MzQsIm5iZiI6MTcyNDE0MjIzNCwicGF0aCI6Ii8xMDEzNzQ2Ny8yMzc3MzAyMjMtZjA1N2VjNjYtNDcwZC00ZmRmLWEzMjctNDAzNDg1Nzg3NjU1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA4MjM1NFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQ5ODI2NWFjZjEzNTgwNzhhY2VmNTdjNjEyOGIyMjRhYmVhOWFiMjYwNzNiMzI3NzU1NmU1NzFiZWQ1YWY4M2UmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.WWYPV-qUhW8Z9-gqNQNBUU04TYCa9RoFK7G6_Dq3qRg)
The Api.BookInfo.Legacy API will be discontinued, a new API called Api.BookInfo.New will have to be used. This API uses integer type code to identify books. So that this change does not affect the Application layer, an intermediate project (BookInfo.AntiCorruptionLayer) was created, responsible for "translating the code" to the new format (integer type):
![](https://private-user-images.githubusercontent.com/10137467/237732508-982f9461-d925-4493-858a-79dee0917d6c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxNDI1MzQsIm5iZiI6MTcyNDE0MjIzNCwicGF0aCI6Ii8xMDEzNzQ2Ny8yMzc3MzI1MDgtOTgyZjk0NjEtZDkyNS00NDkzLTg1OGEtNzlkZWUwOTE3ZDZjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA4MjM1NFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTFiNWQ5NmZiNmFjZGVjZjBmMTE5Yjk3ZDUwNTI3YTUzZDU1NzZiMGJhMDgxYjE4M2UyYzU0NDU2Nzk4ZGExYTAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.gipMhktbQd37gVGou57k5uJHYUxigqqjS_T-oNAlatU)
To execute the project, a Console application was created where the execution is printed in a terminal. In DI you can choose to use the new API (with the anti-corruption layer) and the old one:
var services = new ServiceCollection()
.AddBuyBookUseCase()
.AddBuyBookOutputPort()
.AddBookInfoAcl(); // <- new API (with anti-corruption layer)
//.AddBookInfoLegacy(); <- Legacy API