Search code examples
c#domain-driven-designautomapperclean-architecturenswag

Clean Architecture external REST API as adapter


I am getting in the topic of clean architecture and I am struggling how to integrate an external REST API. I would define the interface of the API in the application layer and put the implementation into the infrastructure layer.

But where to store the DTOs/models of the external api?

Do I have to create a mapping so that I do not store the models in the core layer? This seems to me the correct approach but then it gets ugly when I try to use the nswag to generate the client. I kind of have to make a new adapter layer in between which seems not nice to me.


Solution

  • One technique is to treat the external API DTOs like a data layer. The concept is the same as talking to a database except it is making an HTTP request. This will allow you to swap implementation details from say an API to maybe a bus or direct DB call. In one project I worked on, we put all external API calls in the DAL, and treated it just like any other data access call. Because, after all it is just data and may not be expressed correctly by the customer facing Entity in the app.