hello all. i'm using Room and i have an entity cl...
# android
r
hello all. i'm using Room and i have an entity class with 23 properties. the entity's primary key is a natural key that comes from a remote source. my preference would be to use a data class and that only the primary key property participates in the auto-generated
equals()
and
hashCode()
methods. i also would prefer that all properties of the class are immutable, so the class is more like a functional style data type, and i would appreciate having kotlin auto-generate the
copy()
method with the selective override parameters. however, i can't seem to achieve all these things at the same time because if i create the data class with only the primary key property in the primary constructor, Room needs public setters for the rest of the properties, and i will not get the
copy()
method with parameters for all the non-constructor properties. if i put all immutable properties in the primary constructor, i lose the desired
equals()
and
hashCode()
semantics and i also have concerns about the overhead of that many properties participating in those methods. has anyone tried/been able to achieve this with a data class? my current solution is a standard class with all immutable properties in the primary constructor, and a copy constructor that takes a source instance argument, then individual override parameters for all the properties, but that's a bit of headache and error-prone to maintain
đź§µ 1
p
There are a lot of strong arguments to be made for separating the database Entities, over-the-wire DTOs and your application data models into distinct separate classes. There was a good talk at this year’s NY Droidcon that touches on this - https://www.droidcon.com/2022/09/29/indemnity-in-the-data-layer/
Separation will do you a lot of good, but comes at the cost of converting between the objects. Its worth it though!
r
appreciated, thanks. i'll take a closer at the NIA app's design, because i have been using that as a guide