views:

314

answers:

3

Greetings, Trying to sort through the best way to provide access to my Entity Manager while keeping the context open through the request to permit late loading. I am seeing a lot of examples like the following:

public class SomeController
{
     MyEntities entities = new MyEntities();
}

The problem I see with this setup is that if you have a layer of business classes that you want to make calls into, you end up having to pass the manager as a parameter to these methods, like so:

public static GetEntity(MyEntities entityManager, int id)
{
      return entityManager.Series.FirstOrDefault(s => s.SeriesId == id);
}

Obviously I am looking for a good, thread safe way, to provide the entityManager to the method without passing it. The way also needs to be unit testable, my previous attempts with putting it in Session did not work for unit tests.

I am actually looking for the recommended way of dealing with the Entity Framework in ASP .NET MVC for an enterprise level application.

Thanks in advance

+1  A: 

Entity Framework v1.0 excels in Windows Forms applications where you can use the object context for as long as you like. In asp.net and mvc in particular it's a bit harder. My solution to this was to make the repositories or entity managers more like services that MVC could communicate with. I created a sort of generic all purpose base repository I could use whenever I felt like it and just stopped bothering too much about doing it right. I would try to avoid leaving the object context open for even a ms longer than is absolutely needed in a web application.

Have a look at EF4. I started using EF in production environment when that was in beta 0.75 or something similar and had no real issues with it except for it being "hard work" sometimes.

mhenrixon
A: 

Don't lazy load entities in the view. Don't make business layer calls in the view. Load all the entities the view will need up front in the controller, compute all the sums and averages the view will need up front in the controller, etc. After all, that's what the controller is for.

Justice
I dont know man, that seems like an awful lot of duplicate code, especially if you have admin and anonymous sections. I would feel better concentrating the Get* code into a central location, in cause I need to change the way I bring it back. I agree with you on aggregate logic, if you only do it in one place
xximjasonxx
Use automapper (http://www.codeplex.com/AutoMapper) to map the parts of the domain that you need to properties in a DTO that you send to the view.
Justice
+1  A: 

You might want to look at the Repository pattern (here's a write up of Repository with Linq to SQL).

The basic idea would be that instead of creating a static class, you instantiate a version of the Repository. You can pass in your EntityManager as a parameter to the class in the constructor -- or better yet, a factory that can create your EntityManager for the class so that it can do unit of work instantiation of the manager.

For MVC I use a base controller class. In this class you could create your entity manager factory and make it a property of the class so deriving classes have access to it. Allow it to be injected from a constructor but created with the proper default if the instance passed in is null. Whenever a controller method needs to create a repository, it can use this instance to pass into the Repository so that it can create the manager required.

In this way, you get rid of the static methods and allow mock instances to be used in your unit tests. By passing in a factory -- which ought to create instances that implement interfaces, btw -- you decouple your repository from the actual manager class.

tvanfosson
Your idea is close to what I want. However, you are not exposing the Entity Manager to the Business layer, where I need to use it. But I think you have given me a nudge in the right direction
xximjasonxx