/* Copyright 2010-2014 MongoDB Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; namespace MongoDB.Shared { /// /// Represents a class that owns one or more disposable resources. /// public class CanonicalDisposableClass : IDisposable { // private fields private bool _disposed; private IDisposable _disposableResource; /// /// Initializes a new instance of the class. /// /// A disposable resource. public CanonicalDisposableClass(IDisposable disposableResource) { _disposableResource = disposableResource; } // NOTE: only implement a finalizer if you MUST //~CanonicalDisposableClass() //{ // Dispose(false); //} // protected properties /// /// Gets a value indicating whether this is disposed. /// /// /// true if disposed; otherwise, false. /// protected bool Disposed { // only implement this property if you anticipate having derived classes // CLR compliance prevents us making the field itself protected because of the leading underscore get { return _disposed; } } // public methods /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); // in case a derived class has a finalizer } /// /// Some method. /// public void SomeMethod() { ThrowIfDisposed(); // ... } // protected methods /// /// Releases unmanaged and - optionally - managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected virtual void Dispose(bool disposing) { // this method can be called multiple times // make sure your implementation of this method does not throw any exceptions if (!_disposed) { if (disposing) { // dispose of any managed disposable resources you own here if (_disposableResource != null) { _disposableResource.Dispose(); _disposableResource = null; // not strictly necessary but a good idea } } // dispose of any unmanaged resources here _disposed = true; } } /// /// Throws if disposed. /// /// protected void ThrowIfDisposed() { if (_disposed) { throw new ObjectDisposedException(GetType().Name); } } } }