I’m thinking it would be smart to setup the Entity object context in Application_BeginRequest, store it in Request.items, use it throughout the request and dispose it in Application_EndRequest. That way the context is always available and I can navigate the Entity Framework object graph in my views, lazy loading what I haven’t already eager fetched.
I think this would make it like developing on Ruby on Rails.
It might be I should be shot for speaking such heresy, but it’s so crazy it just might work 🙂
I can’t get Application_BeginRequest and ..EndRequest to fire on ASP.NET MVC though. Aren’t they fired? Any special trick I need to do?
The 1.0 build of ASP.NET MVC let me hook up event handlers on both beginrequest and endrequest, newing up a SessionScope and storing it in HttpContext.Items in beginrequest (I switched to Castle ActiveRecord) and in endrequest I pick the sessionscope out from HttpContext.Items and dispose it. This enables lazy loading throughout the request lifecycle. (can even navigate object graph in views.)