I have the following code example:
public interface IRepository {
// Whatever
}
public class SampleRepository : IRepository {
// Implements 'Whatever'
}
public class NHibernateRepository : IRepository, IDisposable {
// ...
public void Dispose() { ... }
}
Now - is that really bad? I'm not sure, but this seems to be pretty the same as not marking the destructor of the base class virtual in C++
.
I don't want to make the IRepository
interface implement IDisposable
, because that would bring unwanted complexity and bunch of classes which would also have to implement IDisposable
.
How should this case be handled?
I'm sure this can happen to any type hierarchy - when one of the derived type has to manage disposable resources.
So what should I do - pull the IDisposable
up to the very first interface or leave it as it and hope user would distinguish disposable and non-disposable repositories?
Thank you.
What you've described is okay with one important proviso: an object of a type which has added 'IDisposable' to its base must never be passed to a consumer which may end up holding or persisting the object for an unknown duration.
Basically, the owner of an IDisposable object must(*) either take care of disposing the object itself, or hand the object off to a new owner which can accept and honor the responsibility. Initially, IDisposable objects are generally "owned" by their creator, but hand-offs are common. A reference to an IDispoable object can be given to another object without transferring ownership; in that scenario, the owner is still responsible for disposing the object when it is no longer needed. For that to happen, the owner has to know the object is no longer needed. The most common patterns are:
If one of those patterns applies, you may be in good shape. If not you may be in trouble.