|
1 using System; |
|
2 using System.Collections.Generic; |
|
3 using System.Linq; |
|
4 using System.Text; |
|
5 using System.ComponentModel; |
|
6 using System.Diagnostics; |
|
7 |
|
8 namespace FingersDance.ViewModel |
|
9 { |
|
10 /// <summary> |
|
11 /// Base class for all ViewModel classes in the application. |
|
12 /// It provides support for property change notifications |
|
13 /// and has a DisplayName property. This class is abstract. |
|
14 /// </summary> |
|
15 public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable |
|
16 { |
|
17 #region Constructor |
|
18 |
|
19 protected ViewModelBase() |
|
20 { |
|
21 } |
|
22 |
|
23 #endregion // Constructor |
|
24 |
|
25 #region DisplayName |
|
26 |
|
27 /// <summary> |
|
28 /// Returns the user-friendly name of this object. |
|
29 /// Child classes can set this property to a new value, |
|
30 /// or override it to determine the value on-demand. |
|
31 /// </summary> |
|
32 public virtual string DisplayName { get; protected set; } |
|
33 |
|
34 #endregion // DisplayName |
|
35 |
|
36 #region Debugging Aides |
|
37 |
|
38 /// <summary> |
|
39 /// Warns the developer if this object does not have |
|
40 /// a public property with the specified name. This |
|
41 /// method does not exist in a Release build. |
|
42 /// </summary> |
|
43 [Conditional("DEBUG")] |
|
44 [DebuggerStepThrough] |
|
45 public void VerifyPropertyName(string propertyName) |
|
46 { |
|
47 // Verify that the property name matches a real, |
|
48 // public, instance property on this object. |
|
49 if (TypeDescriptor.GetProperties(this)[propertyName] == null) |
|
50 { |
|
51 string msg = "Invalid property name: " + propertyName; |
|
52 |
|
53 if (this.ThrowOnInvalidPropertyName) |
|
54 throw new Exception(msg); |
|
55 else |
|
56 Debug.Fail(msg); |
|
57 } |
|
58 } |
|
59 |
|
60 /// <summary> |
|
61 /// Returns whether an exception is thrown, or if a Debug.Fail() is used |
|
62 /// when an invalid property name is passed to the VerifyPropertyName method. |
|
63 /// The default value is false, but subclasses used by unit tests might |
|
64 /// override this property's getter to return true. |
|
65 /// </summary> |
|
66 protected virtual bool ThrowOnInvalidPropertyName { get; private set; } |
|
67 |
|
68 #endregion // Debugging Aides |
|
69 |
|
70 #region INotifyPropertyChanged Members |
|
71 |
|
72 /// <summary> |
|
73 /// Raised when a property on this object has a new value. |
|
74 /// </summary> |
|
75 public event PropertyChangedEventHandler PropertyChanged; |
|
76 |
|
77 /// <summary> |
|
78 /// Raises this object's PropertyChanged event. |
|
79 /// </summary> |
|
80 /// <param name="propertyName">The property that has a new value.</param> |
|
81 protected virtual void OnPropertyChanged(string propertyName) |
|
82 { |
|
83 this.VerifyPropertyName(propertyName); |
|
84 |
|
85 PropertyChangedEventHandler handler = this.PropertyChanged; |
|
86 if (handler != null) |
|
87 { |
|
88 var e = new PropertyChangedEventArgs(propertyName); |
|
89 handler(this, e); |
|
90 } |
|
91 } |
|
92 |
|
93 #endregion // INotifyPropertyChanged Members |
|
94 |
|
95 #region IDisposable Members |
|
96 |
|
97 /// <summary> |
|
98 /// Invoked when this object is being removed from the application |
|
99 /// and will be subject to garbage collection. |
|
100 /// </summary> |
|
101 public void Dispose() |
|
102 { |
|
103 this.OnDispose(); |
|
104 } |
|
105 |
|
106 /// <summary> |
|
107 /// Child classes can override this method to perform |
|
108 /// clean-up logic, such as removing event handlers. |
|
109 /// </summary> |
|
110 protected virtual void OnDispose() |
|
111 { |
|
112 } |
|
113 |
|
114 #if DEBUG |
|
115 /// <summary> |
|
116 /// Useful for ensuring that ViewModel objects are properly garbage collected. |
|
117 /// </summary> |
|
118 ~ViewModelBase() |
|
119 { |
|
120 string msg = string.Format("{0} ({1}) ({2}) Finalized", this.GetType().Name, this.DisplayName, this.GetHashCode()); |
|
121 System.Diagnostics.Debug.WriteLine(msg); |
|
122 } |
|
123 #endif |
|
124 |
|
125 #endregion // IDisposable Member |
|
126 } |
|
127 } |