久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將 linq 到 sql datacontext 附加到業(yè)務(wù)層中的 httpcon

attaching linq to sql datacontext to httpcontext in business layer(將 linq 到 sql datacontext 附加到業(yè)務(wù)層中的 httpcontext)
本文介紹了將 linq 到 sql datacontext 附加到業(yè)務(wù)層中的 httpcontext的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我需要我的 linq to sql 數(shù)據(jù)上下文在我的業(yè)務(wù)/數(shù)據(jù)層中可用,以便我的所有存儲庫對象進(jìn)行訪問.但是,由于這是一個網(wǎng)絡(luò)應(yīng)用程序,我想根據(jù)請求創(chuàng)建和銷毀它.我想知道是否有一個可以懶惰地創(chuàng)建數(shù)據(jù)上下文并將其附加到當(dāng)前 HttpContext 的單例類是否可行.我的問題是:請求結(jié)束時數(shù)據(jù)上下文會自動處理嗎?下面是我在想什么的代碼.這是否可以實(shí)現(xiàn)我的目的:擁有一個延遲可用并在請求結(jié)束時自動處理的線程安全數(shù)據(jù)上下文實(shí)例?

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

推薦答案

你的想象是有道理的 - 使用 HTTP 請求上下文來存儲東西 - 但是 不, 存儲在當(dāng)前 HttpContext 中的一次性對象將請求結(jié)束時不會自動處理.不知何故,你將不得不自己處理它.

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.

有一個結(jié)束請求"事件,您可以輕松掛鉤,例如使用放入 Global.asax.cs 的代碼.在 Application_EndRequest() 方法中,您可以對列表中需要它的每個對象手動調(diào)用 Dispose().

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.

一種方法是遍歷上下文中的每個項(xiàng)目,測試 IDisposable,然后在適當(dāng)時調(diào)用 Dispose.

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

我認(rèn)為應(yīng)該這樣做.ASPNET 不會自動為您執(zhí)行此操作.當(dāng)然,在真正的應(yīng)用程序中使用此代碼之前,您需要防止異常等.

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.

Vertigo 的 Keith Craig 寫道 前一段時間關(guān)于該主題的相關(guān)帖子,描述了您想要做什么作為一種模式,換句話說,一種做事的方式應(yīng)該重復(fù).他提供了一個類來幫助解決這個問題,延遲加載數(shù)據(jù)庫上下文并將其放入當(dāng)前上下文.這種方法存在一些缺陷 - 您可以在該帖子的評論討論中閱讀有關(guān)它們的信息.評論中還引用了一堆相關(guān)文章.

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.

這篇關(guān)于將 linq 到 sql datacontext 附加到業(yè)務(wù)層中的 httpcontext的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Asp.net System.Web.HttpContext.Current.Session null in global.asax(Asp.net System.Web.HttpContext.Current.Session 在 global.asax 中為 null)
Caught exception is null itself !(捕獲的異常本身為空!)
Is an empty textbox considered an empty string or null?(空文本框被視為空字符串還是 null?)
System.DirectoryServices - The server is not operational(System.DirectoryServices - 服務(wù)器無法運(yùn)行)
Accessing Active Directory from ASP.Net MVC using C#(使用 C# 從 ASP.Net MVC 訪問 Active Directory)
How can I find out which server hosts LDAP on my windows domain?(如何找出在我的 Windows 域上托管 LDAP 的服務(wù)器?)
主站蜘蛛池模板: 毛片入口| 天天操网 | 欧美高清dvd | 日日夜夜天天久久 | 亚洲 自拍 另类 欧美 丝袜 | 成人免费毛片在线观看 | 亚洲国产精品激情在线观看 | 97国产精品 | 日韩视频在线免费观看 | 日韩爱爱网站 | 欧美精品综合在线 | 五月婷婷中文 | 欧美一区免费 | 国产探花 | 亚洲成人精品久久 | 日韩亚洲视频 | 99久久精品免费看国产四区 | www.日日干 | 成人免费视频观看视频 | 国产精品久久国产精品久久 | 国产激情一区二区三区 | 羞羞视频免费观 | 91精品国产综合久久久动漫日韩 | 国产99视频精品免费播放照片 | 国产亚洲精品综合一区 | 日韩福利一区 | 国产精品一二三区在线观看 | 日韩欧美国产精品 | 国产三级日本三级 | 亚洲视频免费一区 | 欧美一区二区三区在线 | 国产电影一区二区 | 国产精品欧美一区二区三区不卡 | 亚洲欧美在线视频 | 欧美精品一区二区三区四区 在线 | 日韩www | 欧美精品一区二区在线观看 | 亚洲成人一二区 | 91国自视频| 九九热精品视频 | 美女张开腿露出尿口 |