Adding linq to sql datacontext in httpcontext in business layer

I need linq to sql datacontext to be available at my business / data level to access all the objects in my repository. However, since this is a web application, I want to create and destroy it for each request. I am wondering if one single class that can lazily create and attach a datacontext to the current HttpContext would work. My question is: will the datacontext be automatically deleted after the request is completed? Below is the code I'm thinking of. This will accomplish my task: is there a thread-safe instance of the datacontext that is lazily accessible and automatically deleted after the request is complete?

public class SingletonDC { public static NorthwindDataContext Default { get { NorthwindDataContext defaultInstance = (NorthwindDataContext)System.Web.HttpContext.Current.Items["datacontext"]; if (defaultInstance == null) { defaultInstance = new NorthwindDataContext(); System.Web.HttpContext.Current.Items.Add("datacontext", defaultInstance); } return defaultInstance; } } } 
+6
linq-to-sql datacontext
Mar 20 2018-10-10T00:
source share
2 answers

What you imagine makes sense - using the HTTP request context to store the material - but No, the one-time objects stored in the current HttpContext will not be automatically configured when the request is completed. One way or another, you have to do it.

There is an "End Request" event, which can be easily connected, for example, using the code that you go to Global.asax.cs. In the Application_EndRequest () method, you can call Dispose() manually for each object in the list that requires it.

One way to do this is to loop over each item in the context, check IDisposable, and then call Dispose if necessary.

 protected void Application_EndRequest(Object sender, EventArgs e) { foreach (var key in HttpContext.Current.Items.Keys) { var disposable = HttpContext.Current.Items[key] as IDisposable; if (disposable != null) { disposable.Dispose(); HttpContext.Current.Items[key] = null; } } } 

I think it should work out. ASPNET does not do this automatically for you. Of course, you need protection against exceptions, etc., before using this code in a real application.




Keith Craig from Vertigo wrote a related post a while ago , describing what you want to do as a template, in other words a way to do what needs to be repeated. It provides a class to help with this, to lazy load the DB context and pass it to the current context. There are some pitfalls with the approach - you can read about them in the discussion of the comment on this post. There are also a bunch of related articles cited in the comments.

+8
Mar 22 '10 at 11:15
source

Cheeso code will throw an InvalidOperationException "Collection was modified; enumeration operation may not execute" because it is trying to change the HttpContext elements iterates through.

You can use a copy of the list to prevent this.

 protected void Application_EndRequest(Object sender, EventArgs e) { var keys = new ArrayList(HttpContext.Current.Items.Keys); foreach (var key in keys) { var disposable = HttpContext.Current.Items[key] as IDisposable; if (disposable != null) { disposable.Dispose(); HttpContext.Current.Items[key] = null; } } } 
+3
Jun 01 '13 at
source



All Articles