Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room entities and domain models, should they be separate?

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?

like image 450
Eduardo Naveda Avatar asked Mar 04 '18 18:03

Eduardo Naveda


People also ask

Is domain model same as entity?

In practical purpose, domain and model are the same, while entity is also a domain/object that would be used to store in the database.

What are the entities in room?

A Room entity includes fields for each column in the corresponding table in the database, including one or more columns that comprise the primary key.

What is a domain and entity?

Each domain contains a set of data related to a specific purpose or function (data access, exceptions, policy violations, and so forth). For a description of all domains, see Domains. Each domain contains one or more entities. An entity is a set of related attributes, and an attribute is basically a field value.

What does the domain model incorporate?

In software engineering, a domain model is a conceptual model of the domain that incorporates both behavior and data. In ontology engineering, a domain model is a formal representation of a knowledge domain with concepts, roles, datatypes, individuals, and rules, typically grounded in a description logic.


1 Answers

They should be separate.

From the docs:

Entity: Represents a table within the database.

Despite the documention referring often to entities as "data models", many examples simplify the design and assume entities = business models.

However, such design is likely to violate Single Responsibility Principle and quickly falls apart once you wish to keep your data in the cloud, where data may be kept using some no-SQL solution.

like image 101
Maciej Beimcik Avatar answered Oct 17 '22 09:10

Maciej Beimcik