Question
Eduard Lebedyuk · Mar 23, 2017

Converting objects between classes

Let's say I have two classes: A and B. They share no common parent except %SystemBase. But these classes have some properties with the same name (type may be the same or not):

Class A {

Property data As C;

Property data2;

}
Class B {

Property data As D;

Property data3;

}

I need to automatically convert  object of class A into object of class B. I'm planing to write a class generator that generates a separate class that converts A to B.  It would work like this:

1. Query %Dictionary to get list of properties:

  • With the same names
  • With both properties being of datatype or object type

2. Iterate over result and generate convert method:

  • If both properties are datatypes, generate simple set line
  • If both properties are objects
    • Check if class generator already generated converter class for this pair of classes, call class generator if not
    • Add a call to the converter class

Is it a viable approach?

0
0 367
Discussion (4)0
Log in or sign up to continue

wouldn't it be easier to store your data in a more generic form to avoid having to do that?

It totally would! Don't have that option though.

Because I want it done automatically. I already wrote several converters manually as described, but started thinking about automating the task.

Why not consider using an Ensemble DTL to do this?  Your use case many not be specific to Ensemble but I've used DTLs to do this for instances like you describe.