LINQ to SQL的 - 那么你的生活的DataContext? [英] LINQ to SQL - where does your DataContext live?

查看:139
本文介绍了LINQ to SQL的 - 那么你的生活的DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LINQ的数据访问对象库SQL。库中都腹板(web应用/ Web服务)和非web(窗口服务)的上下文中使用。最初,我存储在的DataContext 的电流的HttpContext ,因为它允许我管理工作的一个相当小的单元(一个Web请求),避免全局对象的web应用程序。很显然,这并不在Windows服务工作。

I'm using LINQ to SQL in a data access object library. The library is used in both web (web application/web service) and non-web (windows service) contexts. Initially, I stored the DataContext on the current HttpContext since it permitted me to manage a fairly small unit of work (one web request) and avoided global objects in a web app. Obviously, this doesn't work in a Windows Service.

里克·斯特劳对管理的DataContext 的一生一篇好文:的 http://www.west-wind.com/weblog/posts/246222.aspx 。不幸的是,我无法弥补的最好的办法我的脑海里。一个全球性的的DataContext 的原因不能正常工作,他提到,每个​​线程的DataContext 看似复杂,比潜在的更麻烦它的价值,和每个对象实例似乎挑剔 - 你失去了一些优雅,当你装上的DataContext 用来创建一个 DAO DAO 因此它可以更新删除后来 - 更何况,有一些令人不快的鸡和eggish约的关系

Rick Strahl has a nice article on managing the DataContext's lifetime: http://www.west-wind.com/weblog/posts/246222.aspx. Unfortunately, I can't make up my mind on the best approach. A global DataContext doesn't work for reasons he mentions, a per-Thread DataContext seems complicated and potentially more trouble than it's worth, and a per-object instance seems fussy - you lose some elegance when you attach the DataContext used to create a DAO to that DAO so it can update or delete later - not to mention, there's something unpleasantly chicken-and-eggish about the relationship.

有没有人有能够证明一种方法比另一种更好的个人体验?或者更好的是,没有任何人有第四或第五的做法,我没有看到?哪里是存储和管理的最佳地点你的的DataContext

Does anyone have personal experience that suggests one approach is better than another? Or better yet, does anyone have a fourth or fifth approach I'm not seeing? Where is the best place to store and manage your DataContext?

推荐答案

从的的DataContext类 MSDN文档是什么,我会建议如下:

The guidelines from the MSDN documentation on the DataContext class are what I would recommend following:

在一般情况下,一个DataContext实例是
  设计寿命为一个单位
  工作,然而你的应用程序定义
  这个词。一个DataContext是
  重量轻,不昂贵
  创建。一个典型的LINQ to SQL
  应用程序创建的DataContext
  在方法范围或作为一个实例
  短命类的成员说
  再present逻辑组相关
  数据库操作。

In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

由于的DataContext 的IDisposable ,我觉得最简单的创建和使用的DataContext 使用一种方法在语句,所以它可以妥善处理。

Because DataContext is IDisposable, I find it easiest to create and use a DataContext in a using statement within one method, so it can be disposed of properly.

另外请注意,任何实例成员不能保证是线程安全的,所以共用一个的DataContext 多个线程之间将是不明智的。

Also note that "any instance members are not guaranteed to be thread safe", so sharing one DataContext between multiple threads would be unwise.

这篇关于LINQ to SQL的 - 那么你的生活的DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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