dimanche 8 septembre 2019

What is the better way to transform one object to another in Kotlin?

I've multiple object classes in my project and I need to transform one object to another.

I tried creating a common util and start converting them the code started growing and was not able to manage in a single file.

Then I started creating individual transformer class something similar to this,

/** This transforms [Square] to [Rectangle] **/
object SquareToRectangleTransformer {
   fun transform(square: Square): Rectangle {
       checkNotNull(square.side) { missing ("side") }
       val length = square.side
       val breadth = square.side
       return Rectangle(length, breadth
   }
}

Now my project has multiple transformer classes something like the above and now I find this approach as an anti pattern since it's creating class explosion problem.

I searched for other alternatives, like a plugin or dependency library which could do this job for me with less code and fount this answer interesting .

Is there any similar libraries available in Kotlin which could do this job for me?

Aucun commentaire:

Enregistrer un commentaire