0

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

3 Answers 3

2

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.

1

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.

1
  • 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 May 29, 2009 at 14:22
0

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.

2
  • 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 May 29, 2009 at 14:11
  • Use automapper (codeplex.com/AutoMapper) to map the parts of the domain that you need to properties in a DTO that you send to the view.
    – yfeldblum
    May 29, 2009 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.