views:

58

answers:

3

I would like to use the Session-per-request pattern, as is done frequently when using NHibernate in an ASP.NET environment.

My first thought was to put the code to create the context in the BeginRequest event handler in Global.asax, but I discovered that this event is firing not only for the initial request that actually does the work, but also for the subsequent requests for static files such as CSS and images.

I don't want to create a whole bunch of extra contexts when they're not needed, so is there a way I can just get my code to run on the initial request, and not the ones for the static files?

A: 

When I'm using a dependency injection framework I typically have a separate assembly with the domain model in it. That assembly also contains interfaces for repositories. The implementation of the repository contains the code that actually instantiates the EF ObjectContext.

If this is a really simple application though and you're not doing DI you could always instantiate the EF ObjectContext within the constructor of your controller. Have a look at the ASP.NET MVC sample applications at http://www.asp.net/mvc for good basic information on getting started. I think all three of the sample applications they have use EF now.

Nevada
A: 

Are you using IIS 6 with wildcard mapping? CSS and Images shouldn't trigger BeginRequest.

Another popular way to do this is by overriding the OnActionExecuted and OnActionExecuting methods inside of a base Controller.

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _context = new SomeEntities();
        base.OnActionExecuting(filterContext);
    }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        _context.Dispose();
        base.OnActionExecuted(filterContext);
    }
jfar
No, I'm using IIS7. Should BeginRequest be called for static files in that case?
Brian Sullivan
Nope, they shouldn't even touch the pipeline.
jfar
+1  A: 
  1. Use constructor injection for anything which needs a context.
  2. Use the DI framework of your choice and map the context.
  3. Scope the context as request-scoped.
Craig Stuntz