I have a question about domain modeling and nullab...
# announcements
j
I have a question about domain modeling and nullable fields, let's say I have (simplified example)
Copy code
data class Application(val applicantName: String?, val dateOfBirth: LocalDate?, val status: ApplicationStatus)
Now this type can have 10-20 nullable fields because when status is "DRAFT" most of the fields can be missing at this point. After a while when status is let's say READY I know that these nullable fields should always have a value, so they should not be nullable anymore. Is there some way to model this so that I would actually have types like DraftApplication and ReadyApplication, with last one not-nullable access to fields?
I would like to avoid field duplication as much as possible of course
m
I think
sealed class
would be a good starting point. Setting up different 'state's.
4
c
Personally, I don’t like this model. I think when you model a domain, if you make all attributes nullable because you will not get all the information at one time, that’s a failure. Model the intermediate states, e.g. RFP, RFQ, Q, etc. Then do a buidler. Would be my suggestion.
👍 1
j
Can you point any example for what you suggest since I'm not sure what you mean? I'm open to totally different approach
m
Copy code
sealed class Application {
   data class DraftApplication(val applicantName:String?, val dateOfBirth:LocalDate?):Application()
   data class InFlightApplication(val applicantName:String, dateOfBirth:LocalDate):Application()
   data class ApprovedApplication(val application:InFlightApplication, val dateApproved:LocalDate):Application()
}
This is an idea. So initially you have a Draft Application, which would possibly be the original request from the User/UI, so potential errors etc, so everything is null. Would have a validation step that takes a DraftApplication and returns either an InFlightApplication or an error/exception. And as a further example, perhaps there's an approval process, that has original application, and an approval date. This is just a rough idea of what can be done.
👍🏻 2