Cleanup or Dispose ViewModels

Dec 7, 2012 at 8:29 PM


I am evaluating various Mvvm frameworks and am new to Simple Mvvm (not to mvvm in general) and am wondering what the recommended practice is for ViewModel "cleanup" when then contain unmanaged resources and the View they control is finalized.

For example, with Mvvm Light, the ViewModelBase class has a virtual member Cleanup().  In my View, I might register an EventCommand with the Closed event of my View and call Cleanup() from it.  This is somewhat intuitive because ALL ViewModels are known to have a CleanUp() method (whether it actually does anything or not).

With Simple Mvvm, would you recommend implementing a Cleanup() method in ViewModels that require it, then having something like the following in the View (and as the developer, just "knowing" this particular ViewModel requires cleanup?  What if the View is a UserControl and doesn't have the "Closed" event, what event would you hook on to?

    <i:EventTrigger EventName="Closed">


Dec 14, 2012 at 2:09 PM

The way you should handle cleanup is to implement IDisposable on your ViewModels the require cleanup.  It's not common for a ViewModel to need to perform cleanup, but if it does, it's best to stick with the accepted practice of implementing IDisposable, which is supported by the C# using construct, rather than a Cleanup method.