How should I inherit IDisposable?

前端 未结 6 1060
南笙
南笙 2021-02-04 01:31

Class names have been changed to protect the innocent.

If I have an interface named ISomeInterface. I also have classes that inherit the interface, FirstClass

相关标签:
6条回答
  • 2021-02-04 01:55

    All of the answers written so far miss a key point: it's only necessary for a base type or base interface to implement IDisposable if it's likely that code which expects an base-class instance might otherwise acquire ownership of an instance which requires disposal without realizing it. The most common scenario via which this may occur is with a factory method; a prime example is IEnumerable<T>/IEnumerator<T>. Most enumerators do not require cleanup, but code which calls IEnumerable<T>.GetEnumerator generally has no particular reason to believe that the returned enumerator will actually require cleanup, nor to believe that it won't. It's generally faster to have all implementations of IEnumerator<T> implement IDisposable, and have all consumers call Dispose on the returned enumerator, than to have consumers check whether the returned type implements IDisposable and call it if so.

    If it is expected that base-type references will generally only be used by methods which will not be responsible for cleaning up the items in question, there is no need for the base type to implement IDisposable. The code which would be responsible for cleanup will know that the objects it's dealing with implement IDisposable whether or not the base type does.

    0 讨论(0)
  • 2021-02-04 02:00

    It depends on your interface, but I'd lean toward #2. If you have two implementations of ISomeInterface and only one needs disposing, then there's the possibility you need to refactor.

    In general when you're binding to an interface, it's better to have that interface inherit IDisposable rather than the base class; if your interface doesn't inherit IDisposable, you must cast to IDisposable to dispose of the object, and that runs the risk of an InvalidCast...

    0 讨论(0)
  • 2021-02-04 02:04

    If there is a reasonable chance that an abstract entity (interface or abstract class) might need to be disposable, it should implement it. Stream, for example doesn't itself need IDisposable, nor does IEnumerator<T>...

    An abstract base class may be simpler, as you can have a default (empty) implementation of Dispose() then, and possibly the finalizer / Dispose(bool) pattern, i.e.

    void IDisposable.Dispose() { Dispose(true); GC.SuppressFinalize(this); }
    protected virtual void Dispose(bool disposing) {}
    ~BaseType() {Dispose(false);}
    
    0 讨论(0)
  • 2021-02-04 02:11

    If you know some implementations of ISomeInterface require disposal, then the interface should inherit IDisposable, even if concrete implementations of the interface don't have anything to dispose of.

    For instance, in the BCL, IDataReader implements IDisposable, even though one could certainly imagine data reader implementations that don't have external resources that need to be disposed of.

    0 讨论(0)
  • 2021-02-04 02:11

    If you want all your code to deal with ISomeInterfaces generically, then yes they should all be disposable.

    If not, then the code that creates FirstClass should dispose it:

    using (FirstClass foo = new FirstClass()) {
        someObjectThatWantsISomeInterface.Act(foo);
    }
    

    otherwise, you could always use something like this extension method:

    public static void DisposeIfPossible(this object o) {
        IDisposable disp = o as IDisposable;
        if (disp != null)
            disp.Dispose();
    }
    
    // ...
    someObject.DisposeIfPossible(); // extension method on object
    

    I should also mention that I would prefer a template base class approach to this. I stubbed this out in this blog on building disposable things properly.

    0 讨论(0)
  • 2021-02-04 02:14

    My advice is go to the root and not directly to a concrete class. Point 2 is to the root and you are driven by some sort of contract by FirstClass. If you know that classes must implement some interface then you want to ensure that the interface they sign a contract iwth inherits IDisposable

    0 讨论(0)
提交回复
热议问题