Hi,

I have a class which implements dispose patteren. Please see the sample code below.

public class MyClass : IDisposable
    {

    private readonly IBanking mybanking;
        public MyClass(IBanking banking)
        {
            if (banking == null)
            {
                throw new ArgumentNullException("banking");
            }
            mybanking = banking;
        }

        ~MyClass()
        {
            Dispose(false);
        }

        #region IDisposable Members


        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion


        protected virtual void Dispose(bool disposing)
        {
            if (!IsDisposed && disposing)
            {
        // Dispose your objects
                mybanking = null;
            }
        }

        private bool IsDisposed { get; set; }
    }

Like MyClass, I have many classes which will implement the same dispose pattern and dispose there won objects.
Can we write a reusable code for dispose pattern. I think we can write it using generic. But not getting how to write.

Can anyone has any idea?

Regards,

Why are you implementing IDisposable at all? You don't allocate any unmanaged resources, so there is no need for you to do so. Also, this is C#, we don't normally have destructors unless there is a good reason to do so. This isn't one of them.

But not getting how to write.

As Momerath mentioned, you need to consider whether the class needs to implement IDisposable in the first place. If it doesn't, there's no need to complicate the code by doing so.

As for how to have a reusable disposal pattern without manually implementing it in every class, you can use a base class:

public abstract class Disposable : IDisposable
{
    protected bool _disposed { get; private set; }

    protected virtual void Dispose(bool disposing)
    {
        _disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    ~Disposable()
    {
        Dispose(false);
    }
}

The downside to this is it basically hijacks your base class for disposal and you can't inherit from anything else. That would force you to place Disposable one level higher than your highest base class, which may or may not be something you want in your design. Once you have the base class, overriding Dispose() for class-specific needs is simple enough:

public class Foo : Disposable
{
    //...

    protected override void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            if (disposing)
            {
                // Dispose of managed resources here
            }

            // Dispose of unmanaged resources here
        }

        base.Dispose(disposing);
    }

    //...
}

Now you need to ask yourself if it's worth it. All you're saving yourself is a field member, the IDisposable interface implementation, and a finalizer. All of these are boilerplate that don't really change for the traditional disposal pattern. The actual disposal part of the pattern must still be implemented in each derived class. This also precludes any possibility of simplifying the pattern for cases such as sealed classes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.