简体   繁体   English

处理所有实现IDisposable的嵌套对象

[英]Dispose of nested objects which all implement IDisposable

I have the following code in my project. 我的项目中有以下代码。 Do I have to dispose the inner class explicit? 我是否必须明确处理内部类? If so where? 如果是这样的话?

public class Outer : IDisposable
{
    Context context = new Context();
    Inner inner;

    public Outer()
    {
        inner = new Inner(context);
    }

    public void Dispose()
    {
        context.Dispose();
    }
}

public class Inner : IDisposable
{
    Context context;

    public Inner(Context context)
    {
        this.context = context;
    }

    public void Dispose()
    {
        context.Dispose();
    }
}

Context is something like DbContext from Entity Framework. Context类似于Entity Framework中的DbContext。

Well, in this case you need to work out what should actually "own" the context. 那么,在这种情况下,你需要找出实际应该“拥有”上下文的内容。 If you've got it in Inner , do you really need it in Outer as well? 如果你在Inner得到它,你真的需要它在Outer吗? Which of them really takes responsibility for them? 他们中哪一个真的对他们负责? It looks to me like you really want: 它看起来像你真的想要:

public sealed class Outer : IDisposable
{
    private readonly Inner inner;

    public Outer()
    {
        inner = new Inner(new Context());
    }

    public void Dispose()
    {
        inner.Dispose();
    }
}

public sealed class Inner : IDisposable
{
    private readonly Context context;

    public Inner(Context context)
    {
        this.context = context;
    }

    public void Dispose()
    {
        context.Dispose();
    }
}

Note that having made both Outer and Inner sealed, there's no need to write a protected Dispose(bool disposing) method etc - that's really for inheritance, which becomes a pain in general. 注意,已经将OuterInner密封,没有必要编写受保护的Dispose(bool disposing)方法等 - 这实际上是继承,这一般会变得很痛苦。 If you really need to subclass Outer and Inner with the possibility of needing to dispose more resources, you'll need a more complicated implementations. 如果您确实需要将OuterInner子类Outer需要处理更多资源的可能性,那么您将需要更复杂的实现。

Personally I try not to implement IDisposable if possible, and keep disposable things only within local variables with using statements. 我个人尽量尝试实现IDisposable ,并且只using语句将一次性事物保存在局部变量中。 It's not always possible of course, but it's worth trying... 当然,这并不总是可行,但值得尝试......

Yes, it is best to Dipsose inner , simply because it implements IDisposable and Outer owns it. 是的,最好是Dipsose inner ,因为它实现了IDisposable和Outer拥有它。

The fact that in this particular setup it can be shown to be superfluous is not important. 事实上,在这种特殊的设置中它可以被证明是多余的并不重要。 Inner may later be used in another situation, or change itself. 内部可能稍后在另一种情况下使用,或者自行改变。 You should isolate you implementation from that. 您应该将实现与此隔离开来。

If anything, you might reconsider if Inner should Dispose of the context. 如果有的话,你可能会重新考虑内部是否应该处理上下文。 It doesn't create it but gets it passed in. Even better if you could eliminate Inner.context. 它不会创建它但会传入它。如果你可以消除Inner.context,那就更好了。

In this specific case, you don't need to, since you're already disposing the context. 在这种特定情况下,您不需要 ,因为您已经处理了上下文。
However, you should dispose it anyway, in case Inner.Dispose ever changes. 但是,无论如何都要处理它,以防Inner.Dispose发生变化。

In general, you should err on the side of disposing things. 一般来说,你应该在处理事情方面犯错。
Disposing the same object twice won't cause problems. 两次处理同一物体不会造成问题。

Yes, but you should also implement IDisposable correctly. 是的,但你也应该正确实现IDisposable。

  private bool _disposed;

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

  protected virtual void Dispose(bool disposing)
  {
     if (!_disposed)
     {
        if (disposing)
        {
           // Managed
           inner.Dispose();
        }

        // Unmanaged
     }

     _disposed = true;
  }

  ~Outer()
  {
     Dispose(false);
  } 

Based on the pasted code it's the Outer class which should Dispose of the context. 基于粘贴的代码,它是应该处理上下文的外部类。 It's great if you can keep allocation and disposing close together. 如果你可以保持分配和紧密配置,那就太棒了。 The lifetime of the context object can be very sanely be managed by the Outer class. Outer类可以非常理智地管理上下文对象的生命周期。

It's a bad idea to dispose all the objects you can reach. 处置您可以到达的所有对象是一个坏主意。 You should only dispose those objects you allocated yourself. 您应该只处理您自己分配的对象。

Disposing in the Inner class can also mean you can't use the Outer object after you dispose the Inner object. 在Inner类中处置也意味着在处理Inner对象后不能使用Outer对象。 This is fine in the sample you posted but not generally. 在您发布的样本中这很好但通常不是。

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

相关问题 NUnit是否处置实现IDisposable的对象? - Does NUnit dispose of objects that implement IDisposable? 在 IDisposable 对象数组中处置()还是不处置()元素? - To Dispose() or Not To Dispose() elements in an array of IDisposable objects? 如何处置实现IDiposable的对象以及在其上调用的属性/方法的类型实现IDisposable - how to dispose off objects that implement IDiposable and also the types of properties/methods invoked on them implement IDisposable 我们可以对未实现IDisposable的对象使用Using语句吗 - Can we use Using statement for objects which does not implement IDisposable 如何处置具有无法实现IDisposable的属性的类? - How to dispose of a class with properties that do not implement IDisposable? 处置未实现IDisposable的第三方类使用的资源 - Dispose of resources used by 3rd party class which does not implement IDisposable 实施Dispose但不实施IDisposable背后的原因? - Reasoning behind implementing Dispose but not implement IDisposable? 创建IDisposable对象的工厂,如何处置它们? - Factory that creates IDisposable objects, how to dispose them? 是否有时间忽略IDisposable.Dispose? - Is There a Time at which to ignore IDisposable.Dispose? 处理对象是否处理了所有IDisposable属性? - Does disposing an object dispose of all IDisposable properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM