6
I am developing an application with Entity Framework, with several repositories.
Each repository encapsulates SaveChanges()
dbcontext.
When a business layer operation uses multiple repositories, and there is a change in the data, I need to call SaveChanges()
only once.
Obviously, I can do it in any repository, but it seems strange to me to have to call one at random. What is the recommendation in this situation?
EDITED
Repositories are only on interfaces so far, there is no implementation, follow one of them
public interface IUserRepository
{
User getByID(string ID);
User getByIDWithActiveGlossaries(string ID);
IEnumerable<User> getAll();
bool Save();
}
And there are other repositories, all implementing Save()
. Save is in a base interface, put together here to simplify. Save will call SaveChanges()
of DbContext
.
PS: I evaluated the use of UnitOfWork
, but discarded, not seeing need. That’s why, alias, I call SaveChanges()
at the end of a complete operation, which may involve several changes.
FINAL EDITION
I had discarded the use of Unit of Work because there was no need for a mechanism to group operations, since SaveChanges()
is already "all or nothing" (all operations successfully or all are reversed).
However, thanks to the answers of Hokos and Dherik, I realized that Unit of Work
not only has this functionality, but also allows grouping the operation of Save()
.
Could for an example the way your repository is done?
– Filipe Oliveira
This will depend a lot on the implementation you are using, for example, if you are using dependency injection the treatment can be done through the context object that is shared between the repositories
– Julio Borges
@Julioborges So that’s exactly what I was going to do when I thought: Hey, I don’t want to expose Dbcontext in the business layer... then I saw that there should be a method
Save()
interfaces, but the question of where to save when there were several repositories in the same operation.– RSinohara
I removed my answer, I hadn’t seen your remark about discarding the Unit of Work. But it wasn’t clear to me why you dropped it. Either way, you’ll need to create the context in a single place and move to all repositories to achieve your goal, and Unit Of Work helps organize this.
– Dherik
@Dherik I dismissed Unit Of Work because I didn’t need a mechanism to ensure integrity in operations, which for me is the main reason for Unit of work: ensuring that all operations are successful, or that none is executed. But seeing your answer I thought that Unit Of Work also serves to centralize the control of Dbcontext, which is what I need. Return your answer, I will accept and edit my question to be complete.
– RSinohara
@Rsinohara, I get it. At first I thought you needed the integrity of operations between repositories. I returned my answer because, even if it is not the preferred solution to your case, it can help other people with a similar problem who get to your question.
– Dherik
@Dherik I actually rethought the situation, as I mentioned in the question (I don’t know if it was the most appropriate way to put it, but I didn’t want to simply remove the mention of the Unit of Work).
– RSinohara
Opa, It took me a while to see and the guys already blasted the answer, really the best is the Unit Of Work, combining this with Dependency Injection definitely meets your need.
– Julio Borges