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

Immutable entities via copyWithId setter #393

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rafuck
Copy link

@rafuck rafuck commented Mar 4, 2022

Immutable entities support (see Issue-307)

@Entity()
class TestEntityImmutable {
  @Id(useCopyWith: true)
  final int? id;

  final int payload;

  TestEntityImmutable copyWith({int? id, int? payload}) =>
      TestEntityImmutable(
        id: id,
        payload: payload ?? this.payload,
      );

  const TestEntityImmutable({this.id, required this.payload});

  TestEntityImmutable copyWithId(int newId) =>
      (id != newId) ? copyWith(id: newId) : this;
}

@rafuck rafuck changed the title Immutable entities with copyWithId setter Immutable entities via copyWithId setter Mar 4, 2022
@greenrobot-team
Copy link
Member

Thanks, we'll have a look!

@greenrobot-team
Copy link
Member

From a quick look this breaks the contract that put modifies the same object as mentioned in the original issue.

@rafuck
Copy link
Author

rafuck commented Mar 7, 2022

From a quick look this breaks the contract that put modifies the same object as mentioned in the original issue.

Yes, put and putMany (and etc) change ID for objects before (or after) send it to storage. In the case of immutability (in this PR) it is imposible to change immutable objects, and it sends to storage new objects, constructed by copyWith. But it is obvious for the client code that an immutable entity should not change. And if such code uses immutability, it should not rely on side effects. In other cases, everything works as before.

Maybe it might make sense to define new methods for immutable entities (putImmutable, putManyImmutable, etc)... It is not problem.

@greenrobot-team
Copy link
Member

Again thanks for this! I guess you already figured out that relations are going to be a problem as well.

We'll have a look at this in more detail and if it makes sense to integrate.

@rafuck
Copy link
Author

rafuck commented Mar 15, 2022

Again thanks for this! I guess you already figured out that relations are going to be a problem as well.

Yes. My bad: I don't think about relations at the start.. It is really lot of work to make relations in immutable case!

We'll have a look at this in more detail and if it makes sense to integrate.

Thank you

@rafuck
Copy link
Author

rafuck commented Jun 16, 2022

Ping?

@greenrobot-team
Copy link
Member

@rafuck Haven't looked at this, yet.

@erdzan12
Copy link

up

@gombal88
Copy link

any update in the case of immutable classes and autoincrementing id?

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

Successfully merging this pull request may close these issues.

4 participants