a.m. wrote:Can you do some stopwatches and see what is fastest?
I haven't done any stopwatches. But the number of executed SQL commands is the same. I presume it won't give us any real performance improvement. As I've written above the unit of work pattern is more often used for business transactions.
Internally, SaveChanges will start and commit a transaction in SQL Server - you can see this happening in SQL Profiler.
Every time a repository method is called (Insert \ Update \ Delete) it will call save changes and go through the transaction process.
Instead of this, if IDbContext were called from the end of a service or controller method once all entity changes have been made, all SQL statements should be wrapped in a single transaction.
Yes, you will have the same amount of SQL statements, but this should still have performance benefits
Some people have implemented similar functionality along the lines of 'unit of work per http request', the uow starts on 'BeginRequest' and is comitted on 'EndRequest'. Personally, I think this is too late in the pipeline to handle any commit errors & it should be your controllers \ services that handle the SaveChanges.