views:

349

answers:

1

In the lastest version of MVVM-light (V3 SP1) both "Dispose()" and "Dipose(bool)" methods in ViewModel class are marked

Do not use this method anymore, it will be removed in a future version. Use ICleanup.Cleanup() instead

Does this mean that IDisposable interface must not be implemented in all ViewModel classes that are derived from GalaSoft.MvvmLight.ViewModelBase (and cleanup must be overrided)?

If yes, using can't be used for view-model instances... Probably I didn't understand something... Please clarify... What are the benefits of such cleaning up?

Thanks.

+3  A: 

Hi,

The issue is historical. At first I thought it would be a good idea to force all VMs to be IDisposable. However, IDisposable has a different intent: Once the VM is Disposed, it is expected (by convention) that it will be garbage collected as soon as possible. After talking to friends, I realize that forcing all VMs to be IDisposable was a mistake. This is why I replaced IDisposable by ICleanup. The intent of ICleanup is to provide a way to clean VMs (for example flushing their state to persistent storage, closing streams etc...) but not necessarily in a way that they will be garbage collected as soon as possible.

Nothing prevents you to make your VMs implement IDisposable. I just didn't want to keep this constraint in the ViewModelBase class, which is why this interface will be removed in V4.

The benefit of having ICleanup is that you can clean all your VMs in one call of ViewModelLocator.Cleanup(). It is a hint to VM developers saying that VMs should think of providing a cleanup method for their VMs.

Does that make sense? Cheers, Laurent

LBugnion
Thank you for comment, it definitely make sence if you need to have workable VM after its clening... But I don't see a reason to clean it up without disposing... Usually I am dispoing VM on its closing... why do I need to clean it up without closing?I will be appreciated with any feedback. thanks again.
Budda
@Budda what I believe LBugnion is saying is that the concept he was using for IDisposable was already overloaded with the idea of GC the object as soon as possible. However, a lot of us use the same VM object over and over again, so instead of disposing of the object ViewModelBase was given an ICleanUp Interface whose intent is for wiping the VM Clean so it can be used again. This can be useful if you are doing a VM first approach, WPF won't throw away the View and then Recreate it, instead it will be cleaned like the VM.
Agies
Thanks. It clear now
Budda