views:

155

answers:

1

Hello,

I'm trying to use linq to sql for my project (very short deadline), and I'm kind of in a bind. I don't know the best way to have a data context handy per request thread.

I want something like a singleton class from which all my repository classes can access the current data context. However, singleton class is static and is not thread-safe and therefore not suitable for web apps. I want something that would create a data context at the beginning of the request and dispose of it along with the request.

Can anyone please share their solution to this problem? I've been searching for a solution and I've found a good post from Rick Strahl : http://www.west-wind.com/weblog/posts/246222.aspx but I don't completely understand his thread-safe or business object approach. If somebody has a simplified version of his thread-safe approach, i'd love to take a look.

A: 

In the BeginRequest event of Global.asax you could store it in HttpContext.Current.Items which is sort of like a "session state" for the individual request. Each request gets it's own context so there's no threading issues if you are storing a new DataContext per request.

You can dispose it in the EndRequest event.

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.items.aspx

However this will not scale well if you are going to be creating the DataContext on every request even if it doesn't get used. Instead you could make a static method that does lazy initialization.

(Sorry, typing this code on an iPhone... I know...)

private static MyDataContext GetDataContext() {
    var dc = HttpContext.Current.Items["dc"] as MyDataContext;
    if (dc==null) {
        dc = new MyDataContext();
        HttpContext.Current.Items.Add("dc", dc);
    }
    return dc;
}
Josh Einstein
looks like a good solution. However, would this work if I do it in my "model/business" layer by referencing System.Web.HttpContext?
do I need to dispose of the datacontext in EndRequest event? Won't it get disposed as soon as the request end anyways? If that's the case then do I even need to put it in global.asax? Maybe I can just have a singletonclass in my business layer that creates datacontext and attaches it to the current httpcontext. Would this work?
Yes, you should dispose of the DataContext in the EndRequest event otherwise it will simply go out of scope and need to be finalized by the garbage collector. As far as having your business layer access HttpContext... technically there's nothing wrong with it but it does create a dependency on ASP.NET. You might want to look into using an IOC container (plenty of info about that here) which makes it easier to pass dependencies around.
Josh Einstein
but won't adding it to global.asax also create a dependency between my business/data layer and the presentation layer? The reason being that all my repositories that access the datacontext reside in my business/data layer.
btw, an interesting page I found which states reasons for why it's not important to dispose of datacontext: http://lee.hdgreetings.com/2008/06/linq-datacontex.html