jeudi 27 août 2015

Generically wrapping API with explicit methods by type

I'm looking for a way to easily manage decisions based on type, or sets of types. (Example, if is string, double, etc do one thing, if is enum do something else, etc). I have a cross section of both importance AND maintainability being critical.

I am working with an API that forces the following convention.

double dblField;
if(!apiObj.IsNull("NameOfDOubleProperty"))
  dblefield = apiObj.GetDouble("NameOfDOubleProperty");

string stringField;
if(!apiObj.IsNull("NameOfStringProperty"))
   stringField = apiObj.GetDouble("NameOfStringProperty");

 //Repeat for each property

I LOATHE magic strings all over code. If I change something in the app I am reading this from, then I have to ocme add another one, or worse remember to rename every place.

Step1: On start up, get readonly static fields (generated by a single time reflection call). I have that.

Step 2: My question - if it is a string, double, or other common primitive, i have a generic apiobj.GetField that I can cast. However, in the case of some objects, i need special handling.

Thoughts or patterns?





Aucun commentaire:

Enregistrer un commentaire