Answer the question
In order to leave comments, you need to log in
Kotlin, generics and mappers: is it worth it?
Good day to all.
In the course of writing a small SDK for the customer's API in the Android Library Module, I became too lazy to describe 100500 data-class mappers from the data-layer to the domain-layer and the idea came to use generics - namely, to create some of the following simple construction:
object SDKMapper {
@Suppress("UNCHECKED_CAST")
fun <T, F> map(data: F, to: Class<T>): T = to.getDeclaredConstructor(to).newInstance(data)
}
constructor(data: MyResponseDTO) {
// some code here with values set to fields
}
fun getResponse(): MyResponse = SDKMapper.map(repository.getResponse(), MyResponse::class.java)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question