How to emulate a Unit of Work
using EF4 for a web project and have a unique ObjectContext
to be able to access it globally in the application?
How to emulate a Unit of Work
using EF4 for a web project and have a unique ObjectContext
to be able to access it globally in the application?
To implement UoW
you also need to implement the pattern Repository
, in this way you can decouple the controller action or your persistence business layer.
Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10)
What you do not mention is if you are using EF Code First
or you implement it with a edmx
, I recommend using Code First
, it's the best.
A clarification, that is global does not mean that the instance is the same every time you go to the server, but that you must do it by Request, for each invocation of the browser it will be a new instance of the EF context
On the other hand, you evaluated using an IoC library to inject the instance of the context in the constructor where you need it.
There are 2 approaches to make the EF global:
If you want more information, you can consult the following page entry: Managing DbContext the right way with Entity Framework 6: an in-depth guide . It covers, in a very extensive and deep way, your problematic.