mrpmorris / Fluxor

Fluxor is a zero boilerplate Flux/Redux library for Microsoft .NET and Blazor.
MIT License
1.25k stars 144 forks source link

Correct overriding of Dispose #222

Closed uhfath closed 2 years ago

uhfath commented 2 years ago

How to properly override Dispose when inheriting from FluxorComponent ? This is a basic pattern when using IDisposable:

protected virtual void Dispose(bool disposing)
{
    if (!_isDisposed)
    {
        if (disposing)
        {
            //dispose something here
        }

        _isDisposed = true;
    }
}

public void Dispose()
{
    Dispose(disposing: true);
}

The FluxorComponent does just that. However, Disposed is private so when overridden in a descendant class we have no access to it. Perhaps it should be protected or there is a better way out? Maybe return it's initial value from protected virtual void Dispose(bool disposing) ? In this case we could do something like this:

protected override void Dispose(bool disposing)
{
    var isDisposed = base.Dispose(disposing);

    if (!isDisposed)
    {
        if (disposing)
        {
            //dispose something here
        }
    }
}

Of course we can use our own isDisposed like we used to, but that seemed a bit awkward in this case.

mrpmorris commented 2 years ago

Fluxor implements it the way MS recommends - https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose

However, I think the call to Dispose(bool) should only be called if IsDisposed is not true. I'll put this into a future release.