IDisposable Part 3 – Encapsulating an IDisposable class
For part 3 of my series on IDisposable, I’m going to focus on ownership of other IDisposable resources. In this series, we’re going to build on our LicenseAGenerator class from Part 2, encapsulating inside of a class which will use it repeatedly.
In this case, we’re going to suppose that we have a class that repeatedly needs to check licenses. If we were doing this infrequently, we could just create a new LicenseAGenerator as needed, and use it. However, if we were going to be doing this frequently, we might want to construct our license generator one time, and preserve it for the lifetime of our new object. In this case, we might not want to continually construct and dispose our generator, especially if the generator construction is time consuming.
So, say we have a class “A”, and a factory which constructs these classes for us, checking the license. Our A class does not have a public constructor, and must be created via our factory. Our factory class is going to “own” a LicenseAGenerator, which is a disposable resource. If your class encapsulates a disposable resource, and has a sense of ownership of it, it is a good idea to implement IDisposable on the class as well. This allows the user of your class to dispose of the native resources deterministically.
In this case, our factory class could look something like this:
/// <summary> /// Our main factory class /// </summary> /// <remarks>Note that this is marked sealed.</remarks> public sealed class AFactory : IDisposable { /// <summary> /// Our license generator. This is constructed with our class /// </summary> private LicenseAGenerator generator = new LicenseAGenerator(); /// <summary> /// We still need a way to verify that our object has not been disposed. /// </summary> private bool disposed; /// <summary> /// Creates an A class for a specific user. /// </summary> /// <param name="user">The user.</param> /// <returns>A newly constructed instance of A.</returns> public A CreateForUser(string user) { // We still need to make sure we haven't been disposed in this case if (this.disposed) throw new ObjectDisposedException("AFactory"); // Here, we use our IDisposable resource to check our license if (this.generator.LicenseValid(user)) { return new A(); } throw new LicenseException(String.Format("User {0} does not have a valid license.")); } /// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { // In this case, we do not need to provide a virtual method, since we're in a sealed class! // We still need to mark us as disposed, and dispose of our managed resources this.disposed = true; this.generator.Dispose(); } }
There are a few things different about this from our previous IDisposable implementations.
- We decided to seal this class. That simplifies our usage, because we no longer have to provide a way for subclasses to be handled.
- We do not, ourselves, own any native resources. This means we no longer need a finalizer.
These two differences eliminate the need for the protected Dispose(bool disposing) method. If somebody forgets to dispose of us, the finalizer will ignore us, but will still finalize our LicenseAGenerator member correctly. This means we do not need the finalizer.
Without the finalizer, we have no need to differentiate between finalization and Dispose() being called on our object, so we can put our logic directly in the Dispose() method. If we were not sealed, or if we had native resources, we would need to implement the pattern from Part 1 or Part 2.

Hi reed,
In this case, if the user forgets to call the dispose method of AFactory, unmanaged resources of LicenseGenerator will not be released. While in pattern1 and partter2. the finalizer will call dispose(false) to dispose unmanned resource when they forget to. Will this be a problem?
Zamesking:
It won’t be a problem. If AFactory is every a candidate for finalization, that means that all of its private managed resources (in this case, LicenseGenerator) will also be candidates for finalization. When LicenseGenerator’s finalizer runs, it will dispose of the unmanaged resource. There is no need for an explicit finalizer in AFactory, only in LicenseGenerator.
This is why, when encapsulating an IDisposable, you don’t need a finalizer – even though you still want to be IDisposable, and why the pattern is differnet in this case. If you directly contain a native resource, you need a finalizer – otherwise, you typically do not need one.
Reed,
Thanks for explaining it clearly, I just forgot the purpose of the IDisposable interface. Actually, here AFactory class implements IDisposable interface to make it can be release it’s managed resources explicitly.
Keep your good work, I decide to go through all your MVVM articles. Actually, I get to know you in msdn forums, and find you can always explaining tech in clear words, and your suggestions in the blogs are very helpful.
Zamesking:
Thanks for the feedback!
I’m glad the post is helping. The series on IDisposable was probably my favorite, up until my new MVVM one
Both are trying to tackle a subject that’s been talked about a lot, but take it on from a slightly different angle, in a bit more depth. I hope you find them useful.
-Reed