Platform.Disposables

LinksPlatform's Platform.Disposables Class Library


Keywords
Disposable, DisposableBase, LinksPlatform, Disposables, EnsureExtensions, Disposal, GenericObjectExtensions, IDisposableExtensions, IDisposable, class-library, csharp, dotnet-framework, dotnet-standard
License
MIT
Install
Install-Package Platform.Disposables -Version 0.1.2

Documentation

NuGet Version and Downloads count Actions Status Codacy Badge CodeFactor

Disposables (русская версия)

This library helps to make objects disposable in a fast, short, easy and safe way.

Platform.Disposables.DisposableBase abstract class tries to dispose the object at both on instance destruction and OnProcessExit whatever comes first even if Dispose method was not called anywhere by user. And takes care your disposal logic executes only once by default, and if you really need it, you can allow multiple calls and attempts to dispose, by overriding corresponding properties.

The Platform.Disposables.IDisposable interface extends the System.IDisposable with IsDisposed property and Destruct method. The Destruct method is designed to never throw exceptions, that makes it safe to use in class destructors. All ignored exceptions are available at Platform.Disposables.IgnoredDisposables if you need to debug them.

Namespace: Platform.Disposables

Forked from: Konard/LinksPlatform/Platform/Platform.Helpers/Disposables

Package at NuGet: Platform.Disposables

Examples

If you can use inheritance in your class. For example if you don't have other base class inherited.

using Platform.Disposables;

namespace Examples
{
    public class DisposableBaseUsageExample : DisposableBase
    {
        protected override void Dispose(bool manual, bool wasDisposed)
        {
            // Dispose logic
        }
    }
}

If you cannot use inheritance in your class. For example if you have other base class inherited.

using Platform.Disposables;

namespace Examples
{
    public class DisposableUsageExample : System.IDisposable
    {
        private readonly Disposable _disposable;

        public DisposableUsageExample() => _disposable = new Disposable(Disposed);

        public void Dispose() => _disposable.Dispose();

        ~DisposableUsageExample() => _disposable.Destruct();

        private void Disposed(bool manual, bool wasDisposed)
        {
            // Dispose logic
        }
    }
}

If you do not have access to the internal structure of the object's class. You can use a disposable object container. The container is convertible back to the passed object at any time.

using Platform.Disposables;

namespace Examples
{
    public class Examples
    {
        public static void UseAndDispose()
        {
            var array = new int[] { 1, 2, 3 };
            Disposable<int[]> disposableArray = (array, () => array = null);
            WorkWithObjectAndDispose(disposableArray);
            // Here array == null
        }

        private static void WorkWithObjectAndDispose(Disposable<int[]> disposableArray)
        {
            using (disposableArray)
            {
                int[] array = disposableArray;

                // Use your object here

            } // Here there is call to () => array = null
        }
    }
}

Documentation

PDF file with code for e-readers.

Depend on

Dependent libraries