

Android App Development with Kotlin(Live).Full Stack Development with React & Node JS(Live).Java Programming - Beginner to Advanced.Data Structure & Algorithm-Self Paced(C++/JAVA).Data Structure & Algorithm Classes (Live).

While the reflection-based approach seems very interesting at first sight, keep in mind that this introduces additional complexity into the code base and using reflection might have a negative impact on runtime performance. We have some special mappings for the parameters name, address, and telephone, while we use a default name based mapping for every other field. This means we can completely leverage the Kotlin compiler here, helping us in case of refactorings without fearing that our code may break. Inside the lambda we provide a conditional mapping, using the when keyword.Īn interesting fact is, that we can map the actual parameter names we retrieve using reflection, like UserView::name.name instead of simple Strings. Next, we call the UserView constructor with a custom parameter mapping. We also don’t want to break the encapsulation of our User class and use a helper class to reach into our User object and pull out its data, in order to create an UserView object.įun User.toUserViewReflection() = with(::UserView). Since UserView is on the outer layer of our application, we don’t want to add this function to our User class. What we need now is a mapping function, that will map User-> UserView. While it’s representing the same data in our domain, some fields are an aggregate of the fields of our User class and some fields simply have a different name: data class UserView( We’ve decided to call this class UserView and we can imagine it being used as a response send from a web controller.

Now we want to provide a different view on this data. It’s also possible that it’s an entity we load from a relational database. Let’s use the following example: We have a class User, which might be a class from our core domain.
