连接LINQ to SQL的DataContext的在业务层的HttpContext [英] attaching linq to sql datacontext to httpcontext in business layer

查看:146
本文介绍了连接LINQ to SQL的DataContext的在业务层的HttpContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的LINQ to SQL的DataContext的是在我的业务/数据层可以为我所有的资源库对象访问。但是,由于这是一个Web应用程序,我想创建和销毁每个请求。我不知道是否有一个单例类,它可以懒洋洋地创建和附加在DataContext到当前HttpContext会工作。我的问题是:会的请求结束时在DataContext得到自动处理?下面是code代表我在想什么。这会实现我的目的:有一个线程安全的datacontext实例,它是懒洋洋地可用,并且自动设置在请求结束时

 公共类SingletonDC
{
    公共静态NorthwindDataContext默认
    {
        得到
        {
            NorthwindDataContext defaultInstance =(NorthwindDataContext)System.Web.HttpContext.Current.Items [的DataContext];
            如果(defaultInstance == NULL)
            {
                defaultInstance =新NorthwindDataContext();
                System.Web.HttpContext.Current.Items.Add(的DataContext,defaultInstance);
            }
            返回defaultInstance;
        }
    }
}


解决方案

你所想象的是有道理的 - 使用HTTP请求上下文来存储的东西 - 但否,一次性存储在当前的HttpContext对象将没有自动神奇地在请求结束时处理。你将不得不喊得那个自己,不知何故。

有是一个结束请求事件,您可以用code,你放入的Global.asax.cs钩住容易,例如。在你Application_EndRequest()方法,你可以的Dispose()手动调用需要它的列表中的每个对象。

做到这一点的方法之一是通过对IDisposable的上下文中的每个项目,测试迭代,并在适当时再调用Dispose。

 保护无效Application_EndRequest(对象发件人,EventArgs的发送)
{
    的foreach(在HttpContext.Current.Items.Keys VAR键)
    {
        VAR一次性= HttpContext.Current.Items [关键]作为IDisposable的;
        如果(一次性!= NULL)
        {
           disposable.Dispose();
           HttpContext.Current.Items [关键] = NULL;
        }
    }
}

我认为oughtta做到这一点。 ASPNET不会自动为您做到这一点。当然,你需要从异常保护等,在实际的应用程序中使用这个code之前。


眩晕的基思·克雷格写<一个href=\"http://blogs.vertigo.com/personal/keithc/Blog/archive/2007/06/28/linq-to-sql-and-the-quote-request-scoped-datacontext-quote-pattern.aspx\">a在话题前一阵子相关的帖子,描述你想要做一个模式,换句话说,做的事情应该被重复的方法是什么。他提供了一个类,以帮助这一说法,以延迟加载数据库上下文并将其拖放到当前上下文。有一些缺陷的方法 - 你可以在该职位的评论讨论阅读它们。也有一堆在评论中提到相关的文章。

I need my linq to sql datacontext to be available across my business/data layer for all my repository objects to access. However since this is a web app, I want to create and destroy it per request. I'm wondering if having a singleton class that can lazily create and attach the datacontext to current HttpContext would work. My question is: would the datacontext get disposed automatically when the request ends? Below is the code for what I'm thinking. Would this accomplish my purpose: have a thread-safe datacontext instance that is lazily available and is automatically disposed when the request ends?

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;
        }
    }
}

解决方案

What you imagine makes sense - using the HTTP Request context to store stuff - but No, disposable objects stored in the current HttpContext will not auto-magically be disposed when the request ends. You will have to hande that yourself, somehow.

There is an "End Request" event that you can hook into easily, for example using code that you drop into Global.asax.cs. In your Application_EndRequest() method, you can call Dispose() manually on each object in the list that requires it.

One way to do it is to iterate through each item in the context, test for IDisposable, and then call Dispose if appropriate.

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 that oughtta do it. ASPNET doesn't do this for you automatically. Of course you need protection from exceptions and so on, before using this code in a real app.


Keith Craig of Vertigo wrote a relevant post on the topic a while ago, describing what you want to do as a pattern, in other words a way of doing things that ought to be repeated. He provides a class to help out with that, to lazy load the DB context and drop it into the current context. There are some pitfalls with the approach - you can read about them in the comment discussion on that post. Also there are a bunch of related articles cited in the comments.

这篇关于连接LINQ to SQL的DataContext的在业务层的HttpContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆