I am currently implementing a repository in my MVC 3 application. All the repository methods that I am implementing that change the data in some way (Add* and Delete*, primarily) currently DO NOT call the SaveChanges method. I explicitly require the user of my repository to do this.
The other option, of course, is that I always call SaveChanges in my mutation methods.
What tends to be the best practice here and why? I’ve been doing it the first way long enough that I have become used to it, but I’m curious if there is a reason the second would be better?
Normally a unit of work from a business or use case perspective involves modifications of many data entities.
You really want to store all the modifications in a transaction or none of them if something fails while submitting.
So its a good idea to call SaveChanges only once at the end of your unit of work and not inside your Add, Update and Delete methods.