Ryan Simon
08/25/2020, 11:10 PMreferencedOn
. These are properties that are fetched lazily.
object ClassEquipments : IntIdTable("class_equipments") {
val equipmentId = integer("equipment_id").entityId().references(Equipments.id)
val classId = integer("class_id").entityId().references(Classes.id)
val createdAt = timestamp("created_at").default(Instant.now())
val updatedAt = timestamp("updated_at").default(Instant.now())
init {
index("ce_class_id_equipment_id", true, classId, equipmentId)
}
}
class ClassEquipmentEntity(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<ClassEquipmentEntity>(ClassEquipments)
var equipmentId by ClassEquipments.equipmentId
var classId by ClassEquipments.classId
val createdAt by ClassRatings.createdAt
var updatedAt by ClassRatings.updatedAt
val equipment by EquipmentEntity referencedOn Equipments.id
val clazz by ClassEntity referencedOn Classes.id
}
Our transformation extension looks like this
fun ClassEquipmentEntity.toModel(): ClassEquipment = ClassEquipment(
id = id.value,
equipmentId = equipmentId.value,
classId = classId.value,
clazz = clazz.toModel(),
equipment = equipment.toModel(),
updatedAt = updatedAt.offsetDateTime,
createdAt = createdAt.offsetDateTime
)
We convert everything at once. If we always wanted a clazz
and an equipment
this would be fine. There are times, however, that we don't want these objects.
Now, this could be a question of premature optimization, but I'm wondering if anyone has been able to have a nice data class representation of a domain model that also allows the flexibility to load relationships on demand?
Or maybe this approach is flawed somehow? Really curious to hear from anyone familiar with this. Thanks in advance!Michael
08/25/2020, 11:37 PMRyan Simon
08/26/2020, 2:38 AMJilles van Gurp
08/26/2020, 5:27 AMMatteo Mirk
08/26/2020, 9:05 AMWe convert everything at once. If we always wanted aThis is exactly what Domain Driven Design solves with “bounded contexts” and “aggregates”, if you don’t know these concepts I suggest to read about them, it will be enlightening 😉 The problem you have is a classic one of stuffing too much data inside a single class that can’t map correctly with Domain Use Cases. Referring to DDD concepts above and CQRS, you need to have different Read Models/Bounded Contexts depending on your use cases, so when you need a set of properties you’ll have those only. You will need more classes, of course, but the design will be less complex and more isolated.and anclazz
this would be fine. There are times, however, that we don’t want these objects.equipment
Ryan Simon
08/26/2020, 3:48 PMRyan Simon
08/26/2020, 3:49 PMMatteo Mirk
08/26/2020, 3:56 PMRyan Simon
08/26/2020, 4:21 PMDrew Aquino
08/26/2020, 4:31 PMMatteo Mirk
08/27/2020, 8:42 AMMatteo Mirk
08/27/2020, 8:45 AMRyan Simon
08/27/2020, 8:56 PMMatteo Mirk
08/28/2020, 8:19 AMDrew Aquino
08/31/2020, 1:03 AM