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

Saving aggregate #42

Closed
cosinus84 opened this issue Feb 17, 2022 · 3 comments
Closed

Saving aggregate #42

cosinus84 opened this issue Feb 17, 2022 · 3 comments

Comments

@cosinus84
Copy link

Hi, your code is a true source of inspiration regarding ddd, lots of explanations, great job!

In your code there is only save and delete for the user. For update I have some issues regarding saving/updating the aggregate.

  1. First load the aggregate from repo/unitOfWork
  2. Do some changes based on dto
  3. Save the aggregate

Using typeorm(in the end) the save/update method would update all aggregate(step 1) properties (not only the changed ones).

If we have two requests that change two different properties(firstName, lastName) and the first request has some delay/more work/etc, that means that the last request would be overwritten by the first. ( Somehow the repo shoud save only what changed in the aggregate?)

Thanks,

@Sairyss
Copy link
Owner

Sairyss commented Feb 17, 2022

Good point.
TypeOrm repositories provide an update() so it would be safer to use that instead.
Will fix it in the future if I have time

@cosinus84
Copy link
Author

Thanks for your quick answer, but if we look more general.... aggregate saving, the whole aggregate (all stackoverflow and other articles on the web out there), is fishy for me :).

Basically for this to work you shoud mix repository with logic, as in standard module->controller->service. Otherwise keeping logic in aggregate only you should "tell" somehow to the repository to save the exact fields that needs to update, maybe rows to insert, delete. Do you have some suggestion on this ?

@Sairyss
Copy link
Owner

Sairyss commented Feb 18, 2022

TypeOrm save and update methods support partial updating (meaning that it discards all undefined properties). You can create an update method in your repository that accepts something like Partial<Entity> and updates only those fields you need it to update.
Anyway, I don't think this is something you should worry about that much. Updating a single aggregate by multiple request should not happen often. Most of your aggregates are probably updated by a single user anyway and in most cases users are not sending requests that fast.
I'd recommend spending time on a solution only if you have a real bottleneck in your application that is causing problems, otherwise don't worry about it.

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

No branches or pull requests

2 participants