关于 Linq 中数据库上下文重用的意见

发布于 2024-09-27 10:07:07 字数 904 浏览 3 评论 0原文

我有一个使用 linq 访问数据库的类。有些方法调用其他方法。例如:

class UserManager
{
   public User[] getList()
   {
       using(var db = new MyContext())
       {
             return db.Users.Where(item => item.Active == false);
       }
    }
    public User[] addUser(string name)
    {
       using(var db = new MyContext())
       {
           db.Users.InsertOnSubmit(new User() { id = Guid.NewId(),  name = name, active = false ...});
       }
       return getList();
    }

...

在对 addUser 的调用中,我需要返回新列表。 (我知道就目前情况而言,这不是一个很好的设计,但为了简单起见,我删除了细节。)但是,对 getList 的调用创建了第二个数据上下文。

我可以用额外的方法来填充它,即:

public getList()
{
     using(var db = new MyContext())
        return getList(db);
}
public getList(MyContext db)
{
      ...
}

然后替换 addUser 中的调用,以保持相同的数据上下文。

我似乎在我的代码中经常看到这种类型的事情,并且我担心创建和释放所有这些数据上下文的成本。有谁认为是否值得投入额外的工作来消除这些上下文的创建和删除?

I have a class that uses linq to access the database. Some methods call others. For example:

class UserManager
{
   public User[] getList()
   {
       using(var db = new MyContext())
       {
             return db.Users.Where(item => item.Active == false);
       }
    }
    public User[] addUser(string name)
    {
       using(var db = new MyContext())
       {
           db.Users.InsertOnSubmit(new User() { id = Guid.NewId(),  name = name, active = false ...});
       }
       return getList();
    }

...

In the call to addUser I am required to return the new list. (I know as it stands it isn't a great design, but I have eliminated detail for simplicity.) However, the call to getList creates a second data context.

I could pad this out with extra methods, viz:

public getList()
{
     using(var db = new MyContext())
        return getList(db);
}
public getList(MyContext db)
{
      ...
}

Then replace my call in addUser so as to keep the same data context.

I seem to see this type of thing a lot in my code, and I am concerned with the cost of creating and releasing all these data contexts. Does anyone have an opinion as to whether it is worthwhile putting in the extra work to eliminate the creation and deletion of these contexts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[浮城] 2024-10-04 10:07:07

Microsoft 提供了以下建议,以防止重复使用 DataContext 实例 http://msdn.microsoft .com/en-us/library/bb386929.aspx

常见问题 (LINQ to SQL)

连接池

问。有没有一个结构可以帮助
使用 DataContext 池?

A. 不要尝试重用实例
DataContext。
每个 DataContext
维护状态(包括身份
缓存)用于一项特定的编辑/查询
会议。基于获取新实例
根据数据库的当前状态,
使用新的 DataContext。

您仍然可以使用底层 ADO.NET
连接池。了解更多
信息,请参阅 SQL Server 连接
池化 (ADO.NET)。

Microsoft provides the following advice/recommendation to not reuse DataContext instances http://msdn.microsoft.com/en-us/library/bb386929.aspx

Frequently Asked Questions (LINQ to SQL)

Connection Pooling

Q. Is there a construct that can help
with DataContext pooling?

A. Do not try to reuse instances of
DataContext.
Each DataContext
maintains state (including an identity
cache) for one particular edit/query
session. To obtain new instances based
on the current state of the database,
use a new DataContext.

You can still use underlying ADO.NET
connection pooling. For more
information, see SQL Server Connection
Pooling (ADO.NET).

青丝拂面 2024-10-04 10:07:07

可以重用同一逻辑操作的不同部分(也许通过将数据上下文作为参数传递),但您不应该重用更多的东西:

  • 它缓存对象;这会很快变得太大,
  • 就不应该在线程之间共享它
  • 一旦遇到异常,

,重用等等是非常不明智的。所以:原子操作很好;长寿命的应用程序上下文;坏的。

It is ok to reuse for different parts of the same logical operation (perhaps by passing the data-context in as an argument), hut you shouldn't reuse much beyond that:

  • it caches objects; this will grow too big very quickly
  • you shouldn't share it between threads
  • once you've hit an exception, it gets very unwise to reuse

Etc. So: atomic operations fine; a long-life app context; bad.

人生百味 2024-10-04 10:07:07

我通常做的是创建一个类,您可以将其称为 DataManager 之类的类,并将所有数据函数作为成员。此类在其构造函数上创建 MyContext 的实例。

class DataManager
{
   private MyContext db;

   public DataManager() {
       db = new MyContext();
   }

   public User[] getList()
   {
       return db.Users.Where(item => item.Active == false);
   }

   public User[] addUser(string name)
   {
       db.Users.InsertOnSubmit(new User() { id = Guid.NewId(),  name = name, active = false ...});
       return getList();
   }
}

每当您执行一组操作时,都会创建此类的实例。例如,在控制器上,您可以将此类作为成员。只是不要用它创建一个全局变量,当你完成它时实例化并处置它。

What I usually do is create a class the you could call something like DataManager with all data functions as members. This class creates an instance of MyContext on its constructor.

class DataManager
{
   private MyContext db;

   public DataManager() {
       db = new MyContext();
   }

   public User[] getList()
   {
       return db.Users.Where(item => item.Active == false);
   }

   public User[] addUser(string name)
   {
       db.Users.InsertOnSubmit(new User() { id = Guid.NewId(),  name = name, active = false ...});
       return getList();
   }
}

You create an instance of this class whenever you are doing a set of operations. On a Controller, for instance, you could have this class as a member. Just don't make a global var out of it, instantiate and dispose when you are done with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文