ObjectContext management goblal for EF for a Web Application in .NET

1

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?

    
asked by ADRIAN LANDERO 28.01.2016 в 00:07
source

2 answers

1

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.

    
answered by 28.01.2016 в 00:34
1
  • No emulas , but encapsulas EF with UnitOfWork . This is done for a simple reason, since EF itself implements the 2 patterns: Being able to have a persistence layer of data in an assembly that depends on all EF dependencies before all the assemblies that use the data layer depend on the dependencies of EF.
  • There are 2 approaches to make the EF global:

  • Implementing IHttpModule and putting an EF singleton into the implementation. This will make you have a single EF for all customers and all calls. Be careful with this when it comes to having concurrency.
  • Registering in HttpApplication the EF so that it only lasts during the life cycle of the one corresponding to the application.
  • 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.

        
    answered by 28.01.2016 в 13:44