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 and SecondClass. FirstClass uses resources that must be disposed. SecondClass does not.
So the question is, where should I inherit from IDisposable? Both of the following options seem less than ideal:
1) Make FirstClass inherit IDisposable. Then, any code that deals with ISomeInterfaces will have to know whether or not to dispose of them. This smells like tight coupling to me.
2) Make ISomeInterface inherit IDisposable. Then, any class that inherits from it must implement IDisposable, even if there is nothing to dispose. The Dispose method would essentially be blank except for comments.
#2 seems like the correct choice to me, but I'm wondering if there are alternatives.
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 inheritIDisposable
, you must cast toIDisposable
to dispose of the object, and that runs the risk of an InvalidCast...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 needIDisposable
, nor doesIEnumerator<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.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 isIEnumerable<T>
/IEnumerator<T>
. Most enumerators do not require cleanup, but code which callsIEnumerable<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 ofIEnumerator<T>
implementIDisposable
, and have all consumers callDispose
on the returned enumerator, than to have consumers check whether the returned type implementsIDisposable
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 implementIDisposable
whether or not the base type does.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:
otherwise, you could always use something like this extension method:
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.
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
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.