objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence

Home Page:https://docs.objectbox.io/getting-started

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allowing a different Constructor as the one used by ObjectBox

FabrizioG202 opened this issue · comments

When I am creating Entities which have custom types, ObjectBox tries to use the default constructor or the first with parameter names matching the Entity's property names (https://docs.objectbox.io/entity-annotations#objectbox-database-persistence-with-entity-annotations).

If I understood correctly, this would enforce the creation of a custom constructor for use by the user while the default would be reserved for ObjectBox.

For example, the following does not build using the generator:

@Entity()
final class Person {
  Person.ob({
    required this.uid,
    required this.name,
    required this.id,
    required this.metadataBytes,
  }) : metadata = PersonMetadata.fromBytes(metadataBytes);

  Person({
    required this.uid,
    required this.name,
    required this.metadata,
    this.id = 0,
  });

  int id;

  @Index()
  final String uid;
  final String name;

  @Transient()
  final PersonMetadata metadata;

  @Property(type: PropertyType.byteVector)
  final ByteData metadataBytes;
}

class PersonMetadata {
  PersonMetadata.fromBytes(ByteData data);
  ByteData toBytes() => ByteData(0);
}

as ObjectBox is using the default Person constructor to deserialize the object.

Describe the solution you'd like

I though of four possible ways this could possibly be achieved:

  • Addition of a new annotation to annotate the constructor to be used by the user to directly annotate the constructor that ObjectBox should use.
  • Addition of a constructor field to the Entity annotation. If I am not mistaken, this is the approach used by JsonSerializable (https://github.com/google/json_serializable.dart/blob/85e83641befab6a30526c24950ecfd3fe3f9ce62/json_annotation/lib/src/json_serializable.dart#L66)
  • Use the first constructor that has the required properties, independently from if it is the default. Meaning that in the example above, ObjectBox would use the .ob constructor even if a default is available, since the first one has all the required properties.
  • Add the possibility of Adding the Entity decorator to the constructor itself (maybe this could be too confusing for a first user or to difficult to implement on the generator)

Thanks for this! It's correct that the generator currently only looks for an unnamed constructor. After a quick look, I don't see any reason on why it was restricted that way other than simplicity. So maybe the generator can support looking at all constructors. The docs also kind of imply this is how it should work (this is also how it works for Java).

No problem! So, the best solution would be the third one, as it would not require the addition of a new annotation class and would not modify current classes?

If that is the case I could open a pull request to fix this

@FabrizioG202 Yes, that's probably the best way to go.

You can submit a pull request for this! But note that depending on the magnitude of the changes we may require you to sign a contributors license agreement (CLA).