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

Why add methods on RepositoryBaseOfT contain SaveChanges #266

Closed
kfathy-ys opened this issue May 17, 2022 · 3 comments
Closed

Why add methods on RepositoryBaseOfT contain SaveChanges #266

kfathy-ys opened this issue May 17, 2022 · 3 comments

Comments

@kfathy-ys
Copy link

Why the framwork don't give the free to developer to save chanes or not , may be i need to write multiple add then call save changed.

_repositroy.AddAsync(tenant1 );
_repositroy.AddAsync(tenant2);
_repositroy.SaveChanges();

I you remove SaveChanged method from Add method i can write the example without need to use even unit of work or begin transation

@kfathy-ys kfathy-ys changed the title Whay ecch metho on RepositoryBaseOfT contain SaveChanges Why add methods on RepositoryBaseOfT contain SaveChanges May 17, 2022
@ardalis
Copy link
Owner

ardalis commented May 17, 2022

The repository pattern is meant to make access to persistence resemble an in-memory collection with more advanced querying capabilities. When you add an item to a List<T> or other collection, you don't need to call SaveChanges or anything to commit the operation. So for this sample repository we don't, either. If you need UoW you can certainly create your own similar Repository or just wrap the operations in transactions and roll them back if needed.

@kfathy-ys
Copy link
Author

Thank you @ardalis for your replay
So you see the the best practice that core layer don't feel that it work with store engine , so we must build our code as there is no transactions and find some way to make roll back after saving.
please told me you opinion abou that.

@ardalis
Copy link
Owner

ardalis commented May 17, 2022

It's up to you how you want to design it. For most systems, especially web APIs that are only doing one operation at a time, typically, having each repo operation save as part of it works fine. For others, you'll want a separate unit of work and/or transaction pattern to wrap around several operations. There's an issue discussing exactly this here:

ardalis/CleanArchitecture#327

Do what works for your needs.

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