CanonicalDisposableClass.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. namespace MongoDB.Shared
  17. {
  18. /// <summary>
  19. /// Represents a class that owns one or more disposable resources.
  20. /// </summary>
  21. public class CanonicalDisposableClass : IDisposable
  22. {
  23. // private fields
  24. private bool _disposed;
  25. private IDisposable _disposableResource;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="CanonicalDisposableClass"/> class.
  28. /// </summary>
  29. /// <param name="disposableResource">A disposable resource.</param>
  30. public CanonicalDisposableClass(IDisposable disposableResource)
  31. {
  32. _disposableResource = disposableResource;
  33. }
  34. // NOTE: only implement a finalizer if you MUST
  35. //~CanonicalDisposableClass()
  36. //{
  37. // Dispose(false);
  38. //}
  39. // protected properties
  40. /// <summary>
  41. /// Gets a value indicating whether this <see cref="CanonicalDisposableClass"/> is disposed.
  42. /// </summary>
  43. /// <value>
  44. /// <c>true</c> if disposed; otherwise, <c>false</c>.
  45. /// </value>
  46. protected bool Disposed
  47. {
  48. // only implement this property if you anticipate having derived classes
  49. // CLR compliance prevents us making the field itself protected because of the leading underscore
  50. get { return _disposed; }
  51. }
  52. // public methods
  53. /// <summary>
  54. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  55. /// </summary>
  56. public void Dispose()
  57. {
  58. Dispose(true);
  59. GC.SuppressFinalize(this); // in case a derived class has a finalizer
  60. }
  61. /// <summary>
  62. /// Some method.
  63. /// </summary>
  64. public void SomeMethod()
  65. {
  66. ThrowIfDisposed();
  67. // ...
  68. }
  69. // protected methods
  70. /// <summary>
  71. /// Releases unmanaged and - optionally - managed resources.
  72. /// </summary>
  73. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  74. protected virtual void Dispose(bool disposing)
  75. {
  76. // this method can be called multiple times
  77. // make sure your implementation of this method does not throw any exceptions
  78. if (!_disposed)
  79. {
  80. if (disposing)
  81. {
  82. // dispose of any managed disposable resources you own here
  83. if (_disposableResource != null)
  84. {
  85. _disposableResource.Dispose();
  86. _disposableResource = null; // not strictly necessary but a good idea
  87. }
  88. }
  89. // dispose of any unmanaged resources here
  90. _disposed = true;
  91. }
  92. }
  93. /// <summary>
  94. /// Throws if disposed.
  95. /// </summary>
  96. /// <exception cref="System.ObjectDisposedException"></exception>
  97. protected void ThrowIfDisposed()
  98. {
  99. if (_disposed)
  100. {
  101. throw new ObjectDisposedException(GetType().Name);
  102. }
  103. }
  104. }
  105. }