当我要处理一个数据上下文 [英] When should I dispose of a data context

查看:111
本文介绍了当我要处理一个数据上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个应用程序的数据访问层。接入层大量使用LINQ类返回数据。目前,为了体现数据到数据库我添加了一个私有数据的上下文成员和公共Save方法。在code看起来是这样的:

I'm currently writing a data access layer for an application. The access layer makes extensive use of linq classes to return data. Currently in order to reflect data back to the database I've added a private data context member and a public save method. The code looks something like this:

private DataContext myDb;
public static MyClass GetMyClassById(int id)
{
    DataContext db = new DataContext();
    MyClass result = (from item in db.MyClasss
                      where item.id == id
                      select item).Single();
    result.myDb = db;
    return result;
}

public void Save()
{
    db.SubmitChanges();
}

这是一个总在简化,但它给人的总体思路。有没有更好的方式来处理那种模式?我应该是我要访问的分贝,每次实例化一个新的数据上下文?

That's a gross over simplification but it gives the general idea. Is there a better way to handle that sort of pattern? Should I be instantiating a new data context every time i want to visit the db?

推荐答案

它实际上并没有太大的关系。我问马特·沃伦从LINQ to SQL的团队这个前一阵子,和这里的回复:

It actually doesn't matter too much. I asked Matt Warren from the LINQ to SQL team about this a while ago, and here's the reply:

有我们实施的几个原因
  IDisposable的:

There are a few reasons we implemented IDisposable:

如果应用程序逻辑需要持有
  到超出当一个实体
  的DataContext有望被使用或
  有效的,你可以通过强制执行该合同
  调用Dispose。递延装载机
  该实体将仍然引用
  DataContext的,并会尝试使用它
  如果有的话code试图浏览
  递延属性。这些尝试
  将失败。处置也迫使
  DataContext的转储它的缓存
  物化实体使单
  缓存的实体不会意外
  永葆所有实体化
  通过DataContext的,这将
  否则将导致这似乎是一个
  内存泄漏。

If application logic needs to hold onto an entity beyond when the DataContext is expected to be used or valid you can enforce that contract by calling Dispose. Deferred loaders in that entity will still be referencing the DataContext and will try to use it if any code attempts to navigate the deferred properties. These attempts will fail. Dispose also forces the DataContext to dump its cache of materialized entities so that a single cached entity will not accidentally keep alive all entities materialized through that DataContext, which would otherwise cause what appears to be a memory leak.

这是自动关闭逻辑
  DataContext的连接可以
  被骗离开连接
  打开。在DataContext依赖
  应用code枚举所有
  因为要获得查询结果
  一个结果的结束触发
  连接关闭。如果
  应用程序使用的IEnumerable的
  MoveNext方法,而不是一个foreach
  在C#或VB语句,你可以退出
  枚举prematurely。如果你的
  应用经验与问题
  连接不关闭,你
  怀疑自动关闭行为
  不工作,你可以使用的Dispose
  图案周围的工作。

The logic that automatically closes the DataContext connection can be tricked into leaving the connection open. The DataContext relies on the application code enumerating all results of a query since getting to the end of a resultset triggers the connection to close. If the application uses IEnumerable's MoveNext method instead of a foreach statement in C# or VB, you can exit the enumeration prematurely. If your application experiences problems with connections not closing and you suspect the automatic closing behavior is not working you can use the Dispose pattern as a work around.

但基本上你不这样做的真正的需要在大多数情况下处理掉 - 这是由设计。我个人preFER这样做,无论如何,因为它更容易跟随比记住异常的负荷,它,这实现IDisposable的一切处置的原则 - 但你不太可能,如果你的不要的忘了处置。

But basically you don't really need to dispose of them in most cases - and that's by design. I personally prefer to do so anyway, as it's easier to follow the rule of "dispose of everything which implements IDisposable" than to remember a load of exceptions to it - but you're unlikely to leak a resource if you do forget to dispose of it.

这篇关于当我要处理一个数据上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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