Disposable pattern

Most .NET developers already know that the common language runtime requires all resources to be allocated from a managed heap. Since .NET developers are forced to live with managed memory from the start, most of us quickly learn to cleanup our resources by implementing the IDisposable.Dispose method in our code.

As simple as the IDispose approach is, however, there are still a few additional steps. Those steps are also pretty simple and most developers manage not to get too terribly confused. However, since the spirit animal of most software developers is something akin to an agitated herd of feral cats (do cats herd??), Microsoft established a common pattern for cleaning up resources – just to make sure that all .NET developers are dancing to the same tune with the same steps.

Microsoft calls their approach the “Dispose Pattern”. Here’s a link to their white paper on the topic: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/dispose-pattern

Now, because I spend way too much time listening to my spirit animal, and also because I’m too lazy to implement the same few steps over and over again, I decided a long time ago to encapsulate the handful of steps in the “Dispose Pattern” into a simple base class. I’ve used this base class in my code for years. It works well and I’ve only ever seen one potential drawback to using the class. As we all know, .NET doesn’t allow for multiple inheritance, so the fact that my code is in a base class means that there is one situation where I haven’t been able to use the code: when a class already derives from another class. I have found though, when a class already derives from something else, and that class allocates resources, it usually already implements IDisposable, or the disposable pattern, so it’s not really an issue.

Here is the pattern that Microsoft suggest to the cats … Er, I mean, developers:

 

Not too complicated. In fact, Visual Studio even has snippets now that will add this code to a class with a single keystroke. However, that wasn’t the case when I created my class. Also, I would argue that my class extracts the memory management code out of the way, into a separate class, where it doesn’t contribute to the overall noise level – as it would if you used the handy-dandy snippet in Visual Studio.

I call my class DisposableBase. It’s part of my CG.Core nuget package, which can be found here on GitHub: https://github.com/CodeGator/CG.Core

Here is the trimmed down code for my class:

 

The class has a single virtual Dispose method that accepts a boolean parameter. The idea of the parameter, taken from the Microsoft “Dispose Pattern”, is that it’s value should be True to cleanup managed resources, or False otherwise. The Dispose method is called from the class’ IDispose.Dispose method. That method also uses the IsDisposed property to track whether an object has been disposed of previously. That check prevents multiple attempts at cleanup. After the object is cleaned up, the class calls the CG.SuppressFinalize method, again following the “Dispose Pattern”.

There it is. I use that class all over the place and have for a long, long time now. By itself, it doesn’t do anything that simply following the Microsoft “Dispose Pattern” wouldn’t, but it relieves a developer from having the manually add the code over and over again, and, as I said before, it lowers the overall noise level by moving the resource management code out to a separate class.