Use-cases
Good use-cases for Konvert are given, when the classes between which you want to map are structured very similar.
The following is a simple example for that, as the target class PersonDto
has the same fields as Person
:
@KonvertTo(PersonDto::class)
class Person(val firstName: String, val lastName: String, val age: Int)
class PersonDto(val firstName: String, val lastName: String, val age: Int)
Even when the types of the fields are slightly different, like in the following example, it might be a good use-case for Konvert, as there are already various provided TypeConverter
s:
@KonvertTo(PersonDto::class)
class Person(val name: String, val age: Int)
class PersonDto(val name: String, val age: Long)
If the names of some fields do not match, that is no problem either, as you can simply define your own mappings:
@KonvertTo(PersonDto::class,
mappings=[Mapping(source="birthday", target="dateOfBirth")]
)
class Person(val name: String, val birthday: Date)
class PersonDto(val name: String, val dateOfBirth: Instant)
As shown, Konvert can be used in various scenarios and can be customized to fulfill a lot of typical use-cases!
It is often the better choice to manually implement the mapping code, when most fields do not match automatically.
The following classes have no common fields, and you have to define a custom mapping for each. While you defined those custom mappings, you probably could have written the mapping code yourself.
class Person(val firstName: String, val lastName: String) class PersonDto(val givenName: String, val familyName: String)
You will find similar examples in the docs to show, how you nevertheless could do it with Konvert.