Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing a different Constructor as the one used by ObjectBox #570

Open
FabrizioG202 opened this issue Oct 22, 2023 · 3 comments
Open

Allowing a different Constructor as the one used by ObjectBox #570

FabrizioG202 opened this issue Oct 22, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@FabrizioG202
Copy link

FabrizioG202 commented Oct 22, 2023

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)
@FabrizioG202 FabrizioG202 added the enhancement New feature or request label Oct 22, 2023
@greenrobot-team
Copy link
Member

greenrobot-team commented Oct 23, 2023

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).

@FabrizioG202
Copy link
Author

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

@greenrobot-team
Copy link
Member

@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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants