CanonicalDisposableDerivedClass.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 derived from an IDisposable class and that itself owns one ore more disposable resources.
  20. /// </summary>
  21. public class CanonicalDisposableDerivedClass : CanonicalDisposableClass
  22. {
  23. // private fields
  24. private IDisposable _anotherDisposableResource;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="CanonicalDisposableDerivedClass"/> class.
  27. /// </summary>
  28. /// <param name="disposableResource">A disposable resource.</param>
  29. /// <param name="anotherDisposableResource">Another disposable resource.</param>
  30. public CanonicalDisposableDerivedClass(IDisposable disposableResource, IDisposable anotherDisposableResource)
  31. : base(disposableResource)
  32. {
  33. _anotherDisposableResource = anotherDisposableResource;
  34. }
  35. // NOTE: only implement a finalizer if you MUST (and only if the base class does not)
  36. //~CanonicalDisposableDerivedClass()
  37. //{
  38. // Dispose(false);
  39. //}
  40. // public methods
  41. /// <summary>
  42. /// Another method.
  43. /// </summary>
  44. public void AnotherMethod()
  45. {
  46. ThrowIfDisposed();
  47. // ...
  48. }
  49. // protected methods
  50. /// <summary>
  51. /// Releases unmanaged and - optionally - managed resources.
  52. /// </summary>
  53. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  54. protected override void Dispose(bool disposing)
  55. {
  56. // this method can be called multiple times
  57. // make sure your implementation of this method does not throw any exceptions
  58. if (!Disposed)
  59. {
  60. if (disposing)
  61. {
  62. // dispose of any managed disposable resources you own here
  63. if (_anotherDisposableResource != null)
  64. {
  65. _anotherDisposableResource.Dispose();
  66. _anotherDisposableResource = null; // not strictly necessary but a good idea
  67. }
  68. }
  69. // dispose of any unmanaged resources here
  70. }
  71. base.Dispose(disposing); // call base Dispose last
  72. }
  73. }
  74. }