Cleanup vs Dispose(bool) in MVVM-light

前端 未结 3 1460
轮回少年
轮回少年 2020-12-14 01:59

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

Do not use this method an

3条回答
  •  既然无缘
    2020-12-14 02:59

    I think I beg to differ somewhat with Laurent on this point. The idea behind IDisposable is that the object may have some cleanup that needs to take place and does not, per se, have anything to do with garbage collection. In fact, most of the time IDisposable is implemented in order to clean up unmanaged resources, such as file handles, sync objects or database connections, which are outside the purview of the GC. Besides, just because a base class implements IDisposable doesn't mean it has to have an actual implementation. That's can be relegated to the virtual Dispose(bool disposing) method that can be overriden by derived classes so they can perform cleanup.

    The issue, as Budda alludes to, is that IDisposable is by convention a one-way operation. Once an object has been disposed, it should throw an ObjectDisposedException on its public methods. If all you want to do is flush out resources so that you can reuse the object, then a Cleanup method makes sense. However, I would not necessarily remove the Dispose functionality, which serves a different purpose.

提交回复
热议问题