I am learning EF and have seen many examples, and during my learning I came to know about using repository and unit of work patterns. I got why to use repository but I do not have understanding of unit of work really is.
Having no understanding is making DAL understanding difficult. Kindly guide me.
Thanks
The DataContext or ObjectContext is the Unit of Work.
So, your DAL will save, delete and retrieve objects and your DataContext/ObjectContext will keep track of your objects, manage transactions and apply changes.
This is an example just to illustrate the idea of the solution.
And your repository will look like:
Another way is to put the unit of work (Object context) globally:
You need to define what will be your unit of work scope. For this example, it will be a web request. In a real world implementation, I’d use dependency injection for that.
In this example, ObjectContext is the unit of work and it will live in the current request. In your global asax you could add:
In your Repositories, you just call
ContextProvider.CurrentContext