Posted by: Zeeshan Amjad | May 25, 2011

Define base class for ViewModel Revisited


We discussed the simple base class for smple ViewModel here. Now we are going to extend this concept. This time we are going to implement the disposable pattern and implement the INotifyPropertyChangedinterface, so its child classes can properly manage the resource. The first step is, in addition to INotifyPropertyChanged interface, our base class also implement IDisposable interface.

Code Snippet
public abstract class ViewModelBase : INotifyPropertyChanged,
                                    IDisposable

 

IDisposable interface contains only one method named Dispose. Here is simple implementation of this.

Code Snippet
public void Dispose()
{
    // Use SupressFinalize in case a subclass
    // of this type implements a finalizer.
    GC.SuppressFinalize(this);
}

 

SuppressFinalize methods make sure that garbage collector will not call the finalize on object those don’t required it. We also create one virtual method, which child classes are going to override. That method take one Boolean parameter to check weather we should call dispose to internal resources or not. We also make one Boolean field to make sure that we call the dispose of resource only once (in case of unmanaged resource release those only once). Here is our virtual method.

Code Snippet
protected virtual void Dispose(bool disposing)
{
    if (!_disposed)
    {
        if (disposing)
        {
            // Dispose Managed Resource
        }

        // Dispose Unmanaged Resources

        _disposed = true;
    }
}

 

Here _dispose is our private Boolean field. Now we call this method from Dispose method (implementation of IDispose interface). Here is updated version of Dispose method.

Code Snippet
public void Dispose()
{
    Dispose(true);

    // Use SupressFinalize in case a subclass
    // of this type implements a finalizer.
    GC.SuppressFinalize(this);
}

 

Now here is complete code of our new ViewModelBase class that implements both INotifyPropertyChanged and IDisposbale interfaces.

Code Snippet
public abstract class ViewModelBase : INotifyPropertyChanged,
                                    IDisposable
{
    private bool _disposed;
    public event EventHandler RequestClose;

    public ICommand ExitCommand
    { get; set; }

    #region Constructor

    public ViewModelBase()
    {
        _disposed = false;
    }

    #endregion

    public void Close()
    {
        EventHandler handler = this.RequestClose;

        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }

    #region NotifyPropertyChanged Methods
    
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    #region Disposable Methods

    public void Dispose()
    {
        Dispose(true);

        // Use SupressFinalize in case a subclass
        // of this type implements a finalizer.
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed)
        {
            if (disposing)
            {
                // Dispose Managed Resource
            }

            // Dispose Unmanaged Resources

            _disposed = true;
        }
    }

    #endregion
}


Responses

  1. […] we defined the ViewModel based class here to reuse some common functionality. Here is our ViewModelBase […]


Leave a comment

Categories