简体   繁体   English

没有DI的多层MVC Dispose()

[英]MVC Dispose() in multi-layer without DI

I currently have a multi-layer MVC app (Web, BLL, DAL) and trying to understand how to correctly use dispose for DbContext. 我目前有一个多层MVC应用程序(Web,BLL,DAL),试图了解如何正确使用DbContext的Dispose。

There's a whole bunch of info on how to do it with dependency injection. 关于如何使用依赖注入进行操作的信息很多。

I'm not sure I will use DI (yet). 我不确定我是否会使用DI。 Hence why I'm trying to work out if what I've done so far is correct. 因此,为什么我要努力弄清到目前为止所做的事情是否正确。

Below is what has been done: 以下是已完成的操作:

Web 网页

In the Controller, instantiate the DbContext, and have the Dispose() method. 在Controller中,实例化DbContext,并使用Dispose()方法。

  private MyDbContext _context = new MyDbContext();
  ...
  ...

  protected override void Dispose(bool disposing)
  {
      if (disposing)
      {
       _context.Dispose();
      }
      base.Dispose(disposing);
  }

Instantiate the BLL.service object and pass it the DbContext 实例化BLL.service对象并将其传递给DbContext

BLL BLL

Instantiate the DAL.repository object and pass it the DbContext 实例化DAL.repository对象并将其传递给DbContext

DAL DAL

This class inherits from : IDisposable 此类继承自:IDisposable

And contains Dispose methods. 并包含Dispose方法。

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                _context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

Context reason: 上下文原因:

The controller instantiates DbContext, and it get's passed down to the repository classes. 控制器实例化DbContext,并将其传递给存储库类。 Better this way (ie share the context), rather than each repository instantiate their own context. 这样更好(即共享上下文),而不是每个存储库都实例化它们自己的上下文。

Questions: 问题:

I question if the repository classes need Dispose() logic. 我怀疑存储库类是否需要Dispose()逻辑。 And what about the BLL? 那BLL呢? It has a context object, but simply passing it along? 它有一个上下文对象,但是只是传递它呢? Is Dispose() logic required here. 这里是否需要Dispose()逻辑。

Ie Is the Dispose() in the controller enough? 即控制器中的Dispose()是否足够? Or do is Dispose required at each level? 还是在每个级别都需要“处置”? And generally if what I've done is correct? 通常,如果我所做的是正确的? Thanks. 谢谢。

尝试对创建DbContext进行抽象,看看这个这个

I question if the repository classes need Dispose() logic. 我怀疑存储库类是否需要Dispose()逻辑。 And what about the BLL? 那BLL呢? It has a context object, but simply passing it along? 它有一个上下文对象,但是只是传递它呢? Is Dispose() logic required here. 这里是否需要Dispose()逻辑。

Nope. 不。 Not required. 不需要。

The dbContext does not hold any expensive resources to warrant its disposal (connections open for example). dbContext不拥有任何昂贵的资源来保证其处置(例如,打开连接)。 The dbContext closes its connection as soon as the data is retrieved. 检索数据后,dbContext将立即关闭其连接。 Even in high loads, the performance gain is negligible. 即使在高负载下,性能增益也可以忽略不计。

Issues with using Dispose() on a DbContext 在DbContext上使用Dispose()的问题

Exposing the Dispose() method of the dbContext raises potential issues of the method being called in the future, effectively defeating the purpose of an ORM (unintentionally killing the lazy loader). 公开dbContext的Dispose()方法会引发将来调用该方法的潜在问题,从而有效地破坏了ORM的目的(无意中杀死了惰性加载器)。 This issue will be apparent on shared dbContexts, where one repository disposes it while some other repositories might still be using it. 在共享的dbContexts上,此问题将很明显,在该共享库中,一个存储库将其处置,而其他一些存储库可能仍在使用它。

Though there are certain special scenarios that you need to explicitly call the dbContext.Dispose(), but for most cases it is good enough. 尽管在某些特殊情况下您需要显式调用dbContext.Dispose(),但在大多数情况下它已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM