I would want to know where should i use the unit of work ? and why ?
Would like to know where its best seen and should be used. I am using an implementation like :
using (var uow = UnitOfWorkFactory.Create())
{
... transaction go here
}
(I am using ASP.NET MVC 3 with Entity Framework)
Thanks.
A unit of work lets you perform multiple actions within multiple repositories and call Save()
for all of them at once, which calls SaveChanges()
which will turn the unit of work into a transaction. If anything fails during your Save()
call, the operation is rolled back.
So use a unit of work anywhere you need to make sure something doesn't corrupt during the data transaction. Basically use a unit of work when you want to defer transactions until you have finished a set of actions.
This posting is a pretty good overview of how entity framework intends units of work to be used.