objectbox / objectbox/objectbox-dart

Allowing a different Constructor as the one used by ObjectBox

Open
#570 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Dart
Stars
1.2k
Forks
162
Avg merge
15m
Merged PRs (30d)
1

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the constructor-selection failure with the Person example and reviewing the generator behavior described in the issue. Compare the four proposed approaches, then confirm the chosen design supports a separate user constructor and ObjectBox deserialization without breaking existing entities.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.