The ObjectBox documentation describes creating 1:1 and 1:n relations. But is it possible and feasible to create a 1:n:m relation? So I would have a domain data model like
class ClassA {
List<ClassB> myClassB;
}
class ClassB {
List<ClassC> myClassC;
}
class ClassC {
List<ClassD> myClassD;
}
class ClassD {
int x;
}
and would want to transform this back and forth with kind of myClassB.toObjectBox()
and ClassB.fromObjectBox(OboxEntity)
methods.
I am not sure if I can easily extend the 1:n method to a further nested layer, and if, in general, it makes more sense to store the nested data as a JSON string.
=== UPDATE with regards to Markus's answer ===
I typically use 3 data model layers: domain, data transfer, data source. The data transfer layer is providing the intermediate methods between the other 2.
enum myEnum {left, right};
@Entity
class MyDataOboxEntity {
int id;
int enum;
MyDataOboxEntity({this.id, this.enum});
}
class MyDataDtoEntity {
int id;
int enum;
MyDataDtoEntity({this.id, this.enum});
factory MyDataDtoEntity.fromObjectBox(MyDataOboxEntity entity) {
return MyDataDtoEntity(
id: entity.id,
enum: MyEnum[entity.enum],
}
MyDataOboxEntity toObjectBox() {
return MyDataOboxEntity(
id: id,
enum: enum,
}
MyDataEntity toDomain() {
return MyDataEntity(
id: id,
enum: myEnum.values[enum],
);
}
class MyDataEntity {
int id;
MyEnum enum;
MyDataDtoEntity({this.id, this.enum});
}
and then to get a value I would call the data class in the repository with
myData = MyDataDtoEntity.fromObjectBox(getDataFromObjectBox(id: id)).toDomain();