mercredi 1 février 2023

Mapping API result to class, should I use reflection and is this a case of DataAdapter design pattern?

It is my first time using an API and I am not entirely sure about the good practices regarding it. I am using a third party API that returns me a class 'A' in C#, that is not quite compatible with my DTO class 'MyA'. I obviously have to map it to my entity, but I don't know what the best way to do it is. I have read of the Data Adapter pattern and from what I understand, it has the purpose to act as a bridge between two incompatible interfaces. Those classes share some properties, but differ in more important ones, so I wonder if this pattern is what I am looking for.

I believe I could create a base class that uses reflection to map the matching properties for the generic case, and build on top of that for the different properties. I have however heard that reflections should be avoided, so another approach would be to manually map even the shared properties. This would probably be easier to do now, but it is not maintainable at all, involves some avoidable logic repetition, and overall contradicts good design practices.

Would my situation be considered a case usage of DataAdapter? From the definitions I read, I think yes, however the posts I found were predominantly about using different data sources (XML,JSON,CSV), not just classes in the same language with some properties that differ?





Aucun commentaire:

Enregistrer un commentaire