首页 > 解决方案 > Android Kotlin Room Entity data class should be var, val or don't care?

问题描述

I am trying to be conventional, that is why I am refactoring some Kotlin codes in an Android project last few days. I looked at many Room entity data class example, but still not get the best practice of use of val/var in the context of Room. I suspect that the suitable way should be val since it is at the persistence layer and DTO (Data Transfer Objects) should be created about these entities which can be modified. Can someone please assure or deny this hypothesis!?

标签: kotlinentityandroid-roomdtoconventions

解决方案


有点晚了,但也许有些人正在寻找答案。简而言之:没关系。当指定参数 val 仅用于创建 getter 时,var 为属性创建 getter 和 setter。


推荐阅读