Skip to content

Disposables Package

kobi2294 edited this page Apr 17, 2019 · 1 revision

The Disposables Package

Overview

This package contains classes that help to easily create objects that take advantage of the IDisposable pattern, and the using keyword. Use the static class Disposables and it's methods, to create various objects that implement the IDisposable interface, and perform all different types of logic when they are disposed.

Interfaces

Name Descrption
IDisposableWithData Adds a Data property that the disposable carries with it
IDisposableWithData<T> The generic version of the previos

Classes

Name Description
BaseDisposable Base class for disposables, the provides validation, state, and lifecycle hook
BaseDisposableWithData<T> Inherits from BaseDisposable and adds a Data property
Disposables Static class with shortcut methods, that create various types of disposables

Shortcuts

The static class Disposables contains shortcut methods that create various disposables:

Method Description
WithData<T>(T data) returns a disposable that implements IDisposableWithData<T> with the provided data
Call(Action action) returns a disposable that will execute the provided action after it was disposed
Call<T>(Action<T> action, T data) returns a disposable that will execute the provided action and pass the provided data to it
Call(Action<IDisposable> callback) returns a disposable that will execute the provided action, and pass itself to it
Call<T>(Action<IDisposableWithData<T>> action, T data) returns a disposable that will execute the provided action, and pass itself, with the data it carries, to the action

Details

BaseDisposable

Highlights

  • Has IsDisposed property to check if the object was already disposed
  • Has Validate() method, that should throws an exception if the object was already disposed
  • Has an oberridable OnDisposed method that is called when it is disposed.

Example

    public class OneTimeCalc: BaseDisposable
    {
        int _a;
        int _b;

        public OneTimeCalc(int a, int b)
        {
            _a = a;
            _b = b;
        }

        public int Calculate()
        {
            Validate();
            Dispose();
            return _a + _b;
        }

        protected override void OnDisposed()
        {
            base.OnDisposed();
            Console.WriteLine("Disposing Calculator");
        }
    }

BaseDisposableWithData

Highlights

  • Has a Data Property of type T that holds a value
  • Implements IDisposableWithData and IDisposableWithData<T>
  • Has a constructor that accepts the data

Example

    public class OneTimeCalc : BaseDisposableWithData<(int a, int b)>
    {
        public OneTimeCalc(int a, int b)
            :base((a, b)) {}

        public int Calculate()
        {
            Validate();
            Dispose();
            return Data.a + Data.b;
        }

        protected override void OnDisposed()
        {
            base.OnDisposed();
            Console.WriteLine("Disposing Calculator");
        }
    }

Disposables

Highlights

  • Static class with several shortcut methods that create disposables with specific functionality
  • Allows to easily create Disposables that run specific code on dispose, and carry data with them.

Example - Call

            // create a disposable that will write "Disposed!!!" to the console after it was disposed
            var disposable = MvvmKit.Disposables.Call(() => Console.WriteLine("Disposed!!!"));
            using (disposable)
            {
                Console.WriteLine("Using Disposble");
            }

Example - Call with self and data

        public static void Run()
        {
            var disposable = MvvmKit.Disposables.Call(OnDispose, 42);

            using (disposable)
            {
                Console.WriteLine("Using disposble with data: " + disposable.Data);
            }
        }

        public static void OnDispose(IDisposableWithData<int> disposable)
        {
            Console.WriteLine("Disposing object with data: " + disposable.Data);
        }