Skip to main content
removed unnecessary exception.
Source Link
Jesse C. Slicer
  • 14.2k
  • 1
  • 39
  • 52
class Usingifier<T> : IDisposable where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private bool _disposed;

    private T _state;

    public Usingifier(Func<T> initialize, Action<T> cleanUp)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
    }

    public Usingifier<T> Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");return;
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }
    }
}
class Usingifier<T> : IDisposable where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    private T _state;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public Usingifier<T> Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");return;
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }

        if (_autoDispose && _state is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
interface IUsingifier<T> : IDisposable where T : class
{
    T State { get; }

    Usingifier<T> Initialize();
}

class Usingifier<T> : IUsingifier<T> where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public T State { get; private set; }

    public Usingifier<T> Initialize()
    {
        State = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");return;
        }

        _disposed = true;

        if (State != null)
        {
            _cleanUp(State);
        }

        if (_autoDispose && State is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
}
class Usingifier<T> : IDisposable where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private bool _disposed;

    private T _state;

    public Usingifier(Func<T> initialize, Action<T> cleanUp)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
    }

    public Usingifier<T> Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }
    }
}
class Usingifier<T> : IDisposable where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    private T _state;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public Usingifier<T> Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }

        if (_autoDispose && _state is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
interface IUsingifier<T> : IDisposable where T : class
{
    T State { get; }

    Usingifier<T> Initialize();
}

class Usingifier<T> : IUsingifier<T> where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public T State { get; private set; }

    public Usingifier<T> Initialize()
    {
        State = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");
        }

        _disposed = true;

        if (State != null)
        {
            _cleanUp(State);
        }

        if (_autoDispose && State is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
}
class Usingifier<T> : IDisposable where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private bool _disposed;

    private T _state;

    public Usingifier(Func<T> initialize, Action<T> cleanUp)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
    }

    public Usingifier<T> Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            return;
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }
    }
}
class Usingifier<T> : IDisposable where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    private T _state;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public Usingifier<T> Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            return;
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }

        if (_autoDispose && _state is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
interface IUsingifier<T> : IDisposable where T : class
{
    T State { get; }

    Usingifier<T> Initialize();
}

class Usingifier<T> : IUsingifier<T> where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public T State { get; private set; }

    public Usingifier<T> Initialize()
    {
        State = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            return;
        }

        _disposed = true;

        if (State != null)
        {
            _cleanUp(State);
        }

        if (_autoDispose && State is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
}
Extracted interface and exposed `_State`.
Source Link
Jesse C. Slicer
  • 14.2k
  • 1
  • 39
  • 52

I had a couple more thoughts: perhaps expose _state via a property (can then be accessed within your using scope) and then perhaps extracting an interface for the whole caboodle for mocking purposes during unit testing. Behold:

interface IUsingifier<T> : IDisposable where T : class
{
    T State { get; }

    Usingifier<T> Initialize();
}

class Usingifier<T> : IUsingifier<T> where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public T State { get; private set; }

    public Usingifier<T> Initialize()
    {
        State = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");
        }

        _disposed = true;

        if (State != null)
        {
            _cleanUp(State);
        }

        if (_autoDispose && State is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
}

I had a couple more thoughts: perhaps expose _state via a property (can then be accessed within your using scope) and then perhaps extracting an interface for the whole caboodle for mocking purposes during unit testing. Behold:

interface IUsingifier<T> : IDisposable where T : class
{
    T State { get; }

    Usingifier<T> Initialize();
}

class Usingifier<T> : IUsingifier<T> where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public T State { get; private set; }

    public Usingifier<T> Initialize()
    {
        State = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");
        }

        _disposed = true;

        if (State != null)
        {
            _cleanUp(State);
        }

        if (_autoDispose && State is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
}
Added some C#7 goodness to the mix.
Source Link
Jesse C. Slicer
  • 14.2k
  • 1
  • 39
  • 52
class Usingifier<T> : IDisposable where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    private T _state;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public Usingifier<T> Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }

        if (_autoDispose)
        {
            var @object =&& _state as IDisposable;

            if (@objectis !=IDisposable nulldisposable)
            {
                @objectdisposable.Dispose();
            }
        }
    }
class Usingifier<T> : IDisposable where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    private T _state;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public Usingifier<T> Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }

        if (_autoDispose)
        {
            var @object = _state as IDisposable;

            if (@object != null)
            {
                @object.Dispose();
            }
        }
    }
class Usingifier<T> : IDisposable where T : class
{
    private readonly Func<T> _initialize;

    private readonly Action<T> _cleanUp;

    private readonly bool _autoDispose;

    private bool _disposed;

    private T _state;

    public Usingifier(Func<T> initialize, Action<T> cleanUp, bool autoDispose = false)
    {
        _initialize = initialize;
        _cleanUp = cleanUp;
        _autoDispose = autoDispose;
    }

    public Usingifier<T> Initialize()
    {
        _state = _initialize();
        _disposed = false;
        return this;
    }

    public virtual void Dispose()
    {
        if (_disposed)
        {
            throw new ObjectDisposedException(null, "Object has been cleaned up.");
        }

        _disposed = true;

        if (_state != null)
        {
            _cleanUp(_state);
        }

        if (_autoDispose && _state is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
removed `_object` to instead use `_state` for the `IDisposable`.
Source Link
Jesse C. Slicer
  • 14.2k
  • 1
  • 39
  • 52
Loading
added an autodispose for `IDisposable` types.
Source Link
Jesse C. Slicer
  • 14.2k
  • 1
  • 39
  • 52
Loading
Source Link
Jesse C. Slicer
  • 14.2k
  • 1
  • 39
  • 52
Loading