销毁 dataContext 与保持其打开以供将来的数据库访问的性能考虑?

发布于 2024-12-05 11:30:51 字数 1425 浏览 2 评论 0原文

我正在使用 LINQ2SQL 来处理 ASP 中的数据库需求。 Net MVC 3 项目。我有一个单独的 model,其中包含在其自己的类中的所有数据库访问权限,如下所示:

 public class OperationsMetricsDB
{

    public IEnumerable<client> GetAllClients()
    {
        OperationsMetricsDataContext db = new OperationsMetricsDataContext();
        var clients = from r in db.clients
                      orderby r.client_name ascending
                      select r;
        return clients;
    }
    public void AddClient(client newClient)
    {
        OperationsMetricsDataContext db = new OperationsMetricsDataContext();

        db.clients.InsertOnSubmit(newClient);
        db.SubmitChanges();

    }

该类中有大约 50 个不同的方法,它们都会创建然后销毁我的 DataContext 的副本代码>.我的理由是,这种方式可以节省内存,因为在我使用连接并释放内存后,它会破坏 DataContext。但是,我有一种感觉,最好使用 dataContext 的一个副本并保持其打开状态,而不是一遍又一遍地处理和重新建立连接。例如,

public class OperationsMetricsDB
{
    OperationsMetricsDataContext db = new OperationsMetricsDataContext();
    public IEnumerable<client> GetAllClients()
    {            
        var clients = from r in db.clients
                      orderby r.client_name ascending
                      select r;
        return clients;
    }
    public void AddClient(client newClient)
    {
        db.clients.InsertOnSubmit(newClient);
        db.SubmitChanges();

    }

这方面的最佳实践是什么?

I'm using LINQ2SQL to handle my database needs in a ASP. Net MVC 3 project. I have a separate model which contains all my database access in its own class as follows:

 public class OperationsMetricsDB
{

    public IEnumerable<client> GetAllClients()
    {
        OperationsMetricsDataContext db = new OperationsMetricsDataContext();
        var clients = from r in db.clients
                      orderby r.client_name ascending
                      select r;
        return clients;
    }
    public void AddClient(client newClient)
    {
        OperationsMetricsDataContext db = new OperationsMetricsDataContext();

        db.clients.InsertOnSubmit(newClient);
        db.SubmitChanges();

    }

I have about 50 different methods in this class which all create and then destroy a copy of my DataContext. My reasoning was that this way would save memory because it would destroy the DataContext after I use the connection and free up that memory. However, I have a feeling that it may be better to use one copy the dataContext and keep it open instead of disposing and reestablishing the connection over and over again. e.g

public class OperationsMetricsDB
{
    OperationsMetricsDataContext db = new OperationsMetricsDataContext();
    public IEnumerable<client> GetAllClients()
    {            
        var clients = from r in db.clients
                      orderby r.client_name ascending
                      select r;
        return clients;
    }
    public void AddClient(client newClient)
    {
        db.clients.InsertOnSubmit(newClient);
        db.SubmitChanges();

    }

What is the best practice on this?

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

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

发布评论

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

评论(3

香草可樂 2024-12-12 11:30:51

为此,我个人将工作单元模式与存储库结合使用。

UnitOfWork 创建并管理 DataContext。然后,它会根据请求将上下文传递到每个存储库。每次调用者想要对数据库执行一组新操作时,他们都会创建一个新的工作单元。

接口看起来像这样:

public interface IUnitOfWork
{
    IRepository<T> GenerateRepository<T>();
    void SaveChanges();
}

public interface IRepository<T> where T : class
{
    public IQueryable<T> Find();
    public T Create(T newItem);
    public T Delete(T item);
    public T Update(T item);
}

确保上下文的生命周期恰好是一个工作单元的长度(比单个操作长,但比应用程序的生命周期短)。

I personally use the Unit of Work pattern in conjunction with Repositories for this.

The UnitOfWork creates and manages the DataContext. It then passes the context to each repository when requested. Each time the caller wants to do a new set of operations with the database, they create a new UnitOfWork.

The interfaces would look something like:

public interface IUnitOfWork
{
    IRepository<T> GenerateRepository<T>();
    void SaveChanges();
}

public interface IRepository<T> where T : class
{
    public IQueryable<T> Find();
    public T Create(T newItem);
    public T Delete(T item);
    public T Update(T item);
}

That ensures that the context's lifespan is exactly one Unit of Work long (which is longer than a single operation but shorter than the lifespan of the application).

白馒头 2024-12-12 11:30:51

不建议长时间携带数据上下文。所以你走在正确的道路上。据我所知,它使用连接池,因此在应用程序生命周期中创建多个数据上下文对性能的影响并不太严重。

但我不会为数据类的每个方法调用创建一个新的上下文实例。

我更喜欢在工作风格单元中使用它。在 Web 应用程序中,http 请求的处理可以视为一个工作单元。

所以我的建议是在 http 请求的生命周期内创建一个 datacontext 实例,然后将其丢弃。

Its not recommended to cary a datacontext a long time with you. So you are on the right path. It uses connection pooling as far as i know, so the performance hit of creating more than one datacontext in an applications lifetime is not too serious.

But i would not create a new context instance for every single method call of your data class.

I prefer to use it in a unit of work style. Within a web application the processing of a http request can be seen as a unit of work.

So my advice is to create one datacontext instance for the lifetime of on http request and dispose it afterwards.

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