It seems to me that many guides on Room (and other ORMs for that matter) focus on the creation of Room entities and go on to use these as their domain models from then on. But what if my model needs its actual structure to perform some business logic?
For example, take the following class:
class Report(var id: Long, var patient: Patient, var surgery: Surgery) {
var minimumAllowableBloodLoss: Double = 0.0
get() = ((this.patient.hemoglobin - this.patient.minHemoglobin) / this.patient.hemoglobin) * this.patient.bloodVolume * this.patient.weight
private set
var hourlyDiuresis: Double = 0.0
get() = this.patient.diuresisOutput / this.surgery.duration
private set
var urineOutput: Double = 0.0
get() = this.hourlyDiuresis / this.patient.weight
private set
var intakeSupply: Double = 0.0
get() = this.patient.totalIntake / this.patient.weight
private set
var finalFluidBalance: Double = 0.0
get() = this.patient.totalIntake - this.patient.totalOutput
private set
}
If I made this class into a Room entity, I would've had to change my object references to just foreign keys, essentially making impossible doing the calculations I need from this class.
Naturally, my first instinct was to scrap that idea entirely and create a representation object, I believe it's also called a "persistance model":
@Entity
data class ReportRow(
var patientId: Long, var surgeryId: Long) {
@PrimaryKey(autoGenerate = true)
var id: Int = 0
}
But this also means I would have to create conversion methods from the persistence model to domain and vice versa.
This led me to believe maybe I'm missing something entirely or I'm just not using the tools correctly, is there a better alternative for these cases?