Files
Umbraco-CMS/src/Umbraco.Core/DisposableObject.cs

61 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Threading;
namespace Umbraco.Core
{
/// <summary>
2015-07-14 15:55:56 +02:00
/// Abstract implementation of IDisposable.
/// </summary>
2015-07-14 15:55:56 +02:00
/// <remarks>
/// Can also be used as a pattern for when inheriting is not possible.
///
/// See also: https://msdn.microsoft.com/en-us/library/b1yfkh5e%28v=vs.110%29.aspx
/// See also: https://lostechies.com/chrispatterson/2012/11/29/idisposable-done-right/
///
/// Note: if an object's ctor throws, it will never be disposed, and so if that ctor
/// has allocated disposable objects, it should take care of disposing them.
/// </remarks>
public abstract class DisposableObject : IDisposable
{
private bool _disposed;
2015-07-14 15:55:56 +02:00
private readonly object _locko = new object();
2015-07-14 15:55:56 +02:00
// gets a value indicating whether this instance is disposed.
// for internal tests only (not thread safe)
//TODO make this internal + rename "Disposed" when we can break compatibility
public bool IsDisposed { get { return _disposed; } }
2015-07-14 15:55:56 +02:00
// implements IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
2015-07-14 15:55:56 +02:00
// finalizer
~DisposableObject()
{
Dispose(false);
}
2015-07-14 15:55:56 +02:00
//TODO make this private, non-virtual when we can break compatibility
protected virtual void Dispose(bool disposing)
{
2015-07-14 15:55:56 +02:00
lock (_locko)
{
if (_disposed) return;
_disposed = true;
}
2015-07-14 15:55:56 +02:00
DisposeUnmanagedResources();
2015-07-14 15:55:56 +02:00
if (disposing)
DisposeResources();
}
protected abstract void DisposeResources();
protected virtual void DisposeUnmanagedResources()
2015-07-14 15:55:56 +02:00
{ }
}
}