I’m struggling to decide on how to deal with this ...
# codingconventions
d
I’m struggling to decide on how to deal with this case where I have a method that writes a message to the database without any kind of ORM. The problem is the message has 4+ number of properties (at least sender, timestamp, subject, body) that are required to create one. But in many parts of the code, the row id is going to be desired for this message (something you don’t have until you call the aforementioned method.) Writing a method with 4+ properties is not too awesome, but if I want to create an immutable object to represent this message, how do i create one without having the row id before it’s written to the db? TL;DR for most of the code base I want an immutable message with at least id, sender, timestamp, subject and body, but when creating this message I don’t have the id - I have to first create it in the database. How do I do this in a sane way?
m
Spring Data JDBC uses
null
for the
id
in a
data class
. So the persistence layer, and your code, can tell if this is a new entity, or an existing one based on
id
value. It is still a
val
, and leverages the
copy
function to create the new instance with the
id
populated.
d
Ah, I guess I was also trying to avoid nullable types but it’s really seeming like that just isn’t possible. (in a sane way)
What about something like this? Is this just silly?
I think this makes a bit more sense if going that route but it loses the data class benefits…
I think I’m still disturbed by this having the nullable id in the object because it’s not a domain property.
it is strictly related to the persistent layer.