[tasque/viewmodel: 50/78] Make ViewModelBase IDisposable



commit 911f283607555b69235dca422d83151fe48c6727
Author: Antonius Riha <antoniusriha gmail com>
Date:   Mon Aug 6 22:23:54 2012 +0200

    Make ViewModelBase IDisposable
    
    Subclasses of ViewModelBase are likely to contain unmanaged resources.
    Some of them register events from Preferences, a long living object, while
    having a short lifespan themselves. To allow them to be GC'd make them
    IDisposable and unregister events (and dispose of all unmanaged resources)
    in an override Dispose method.
    
    OnPropertyChanged has been made virtual 'cause it's good practice

 src/libtasqueui/ViewModelBase.cs |   19 ++++++++++++++++---
 1 files changed, 16 insertions(+), 3 deletions(-)
---
diff --git a/src/libtasqueui/ViewModelBase.cs b/src/libtasqueui/ViewModelBase.cs
index 13f5293..1620d1c 100644
--- a/src/libtasqueui/ViewModelBase.cs
+++ b/src/libtasqueui/ViewModelBase.cs
@@ -23,19 +23,32 @@
 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
+using System;
 using System.ComponentModel;
 
 namespace Tasque.UIModel
 {
-	public abstract class ViewModelBase : INotifyPropertyChanged
+	public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
 	{
+		public void Dispose ()
+		{
+			Dispose (true);
+			GC.SuppressFinalize (this);
+		}
+		
+		protected virtual void Dispose (bool disposing) {}
+		
+		~ViewModelBase ()
+		{
+			Dispose (false);
+		}
+
 		public event PropertyChangedEventHandler PropertyChanged;
 
-		protected void OnPropertyChanged (string propertyName)
+		protected virtual void OnPropertyChanged (string propertyName)
 		{
 			if (PropertyChanged != null)
 				PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
 		}
 	}
 }
-



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]