Kamila
04/04/2022, 7:19 AMclass Something(val name: String) {
companion object {
fun toDto(): Dto(name)
}
}
data class Dto(val name: String)
But I would prefer to have possibility to write:
Dto.from(foo: Something) = Dto(name = foo.name)
Is it possible?kqr
04/04/2022, 7:24 AMKamila
04/04/2022, 7:25 AMkqr
04/04/2022, 7:29 AMfun Something.toDto() = Dto(this.name)
?Kamila
04/04/2022, 7:30 AMSomething
into different Dtokqr
04/04/2022, 7:35 AMKamila
04/04/2022, 7:45 AMDto
or Something
kqr
04/04/2022, 7:47 AMJoffrey
04/04/2022, 7:54 AMfun Dto.Companion.from(..)
Kamila
04/04/2022, 7:58 AMJoffrey
04/04/2022, 8:02 AMKamila
04/04/2022, 8:09 AMkqr
04/04/2022, 8:11 AMKamila
04/04/2022, 8:11 AMkqr
04/04/2022, 8:12 AMKamila
04/04/2022, 8:16 AMcompanion object {}
on the DTO? Or is there some magic to be able to do
data class Dto(val name: String)
fun Dto.Companion.from(smth: Something) = Dto(name = smth.name)
// Or do I have to write
data class Dto(val name: String) {
companion object { }
}
Joffrey
04/04/2022, 8:26 AMfun Domain.toDto(): Dto
Kamila
04/04/2022, 9:54 AMfun Dto.Companion.from(obj: Domain) = Dto()
Joffrey
04/04/2022, 11:29 AMtoDto()
version so much?Kamila
04/05/2022, 8:32 AMdto: SomeDto = domain.toDto()
dto: SomeOtherDto = domain.toDto()
dto: YetAnotherDto = domina.toDto()
which reads worse than
dto = SomeDto.from(domain)
dto = SomeOtherDto.from(domain)
dto = YetAnotherDto.from(domain)
Joffrey
04/05/2022, 8:36 AMval dto = domain.toSomeDto()
val dto = domain.toSomeOtherDto()
val dto = domain.toYetAnotherDto()
It goes in line with Kotlin's String.toInt()
and the likes.
And it also reads nicer (IMO) when chained, especially with nullable values:
val dto = something.getSomeDomainEntitySomehow()?.toSomeDto()
As opposed to:
val dto = someting.getSomeDomainEntitySomehow()?.let { SomeDto.from(it) }
Kamila
04/05/2022, 8:50 AMdomain.toSomeDto(other: Info)
vs
SomeDto.from(domain, other)
which IMHO reads easier in the way of understanding how someDto
composedJoffrey
04/05/2022, 8:55 AMtoDto(extra info)
variant, like toInt(radix)
.
It always boils down to the message you want to convey, I'll let you judge of that šKamila
04/05/2022, 8:40 PM