CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BindableBase.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BindableBase.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>This class implements INotifyPropertyChanged</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.ComponentModel;
10
11namespace Csla.Core
12{
18 [Serializable()]
19 public abstract class BindableBase :
21 INotifyPropertyChanged,
22 INotifyPropertyChanging
23 {
27 protected BindableBase()
28 { }
29
30 [NonSerialized()]
31 private PropertyChangedEventHandler _nonSerializableChangedHandlers;
32 private PropertyChangedEventHandler _serializableChangedHandlers;
33
37 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
38 "CA1062:ValidateArgumentsOfPublicMethods")]
39 public event PropertyChangedEventHandler PropertyChanged
40 {
41 add
42 {
43 if (ShouldHandlerSerialize(value))
44 _serializableChangedHandlers = (PropertyChangedEventHandler)
45 System.Delegate.Combine(_serializableChangedHandlers, value);
46 else
47 _nonSerializableChangedHandlers = (PropertyChangedEventHandler)
48 System.Delegate.Combine(_nonSerializableChangedHandlers, value);
49 }
50 remove
51 {
52 if (ShouldHandlerSerialize(value))
53 _serializableChangedHandlers = (PropertyChangedEventHandler)
54 System.Delegate.Remove(_serializableChangedHandlers, value);
55 else
56 _nonSerializableChangedHandlers = (PropertyChangedEventHandler)
57 System.Delegate.Remove(_nonSerializableChangedHandlers, value);
58 }
59 }
60
67 protected virtual bool ShouldHandlerSerialize(PropertyChangedEventHandler value)
68 {
69 return value.Method.IsPublic &&
70 value.Method.DeclaringType != null &&
71 (value.Method.DeclaringType.IsSerializable || value.Method.IsStatic);
72 }
73
84 [EditorBrowsable(EditorBrowsableState.Advanced)]
85 protected virtual void OnPropertyChanged(string propertyName)
86 {
87 if (_nonSerializableChangedHandlers != null)
88 _nonSerializableChangedHandlers.Invoke(this,
89 new PropertyChangedEventArgs(propertyName));
90 if (_serializableChangedHandlers != null)
91 _serializableChangedHandlers.Invoke(this,
92 new PropertyChangedEventArgs(propertyName));
93 }
94
105 [EditorBrowsable(EditorBrowsableState.Advanced)]
106 protected virtual void OnMetaPropertyChanged(string propertyName)
107 {
108 if (_nonSerializableChangedHandlers != null)
109 _nonSerializableChangedHandlers.Invoke(this,
110 new MetaPropertyChangedEventArgs(propertyName));
111 if (_serializableChangedHandlers != null)
112 _serializableChangedHandlers.Invoke(this,
113 new MetaPropertyChangedEventArgs(propertyName));
114 }
115
126 [EditorBrowsable(EditorBrowsableState.Advanced)]
127 protected virtual void OnPropertyChanged(IPropertyInfo propertyInfo)
128 {
129 OnPropertyChanged(propertyInfo.Name);
130 }
131
140 [EditorBrowsable(EditorBrowsableState.Advanced)]
141 protected virtual void OnIsDirtyChanged()
142 {
144 }
145
155 [EditorBrowsable(EditorBrowsableState.Advanced)]
156 protected virtual void OnUnknownPropertyChanged()
157 {
158 OnPropertyChanged(string.Empty);
159 }
160
161 [NonSerialized()]
162 private PropertyChangingEventHandler _nonSerializableChangingHandlers;
163 private PropertyChangingEventHandler _serializableChangingHandlers;
164
168 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design",
169 "CA1062:ValidateArgumentsOfPublicMethods")]
170 public event PropertyChangingEventHandler PropertyChanging
171 {
172 add
173 {
174 if (ShouldHandlerSerialize(value))
175 _serializableChangingHandlers = (PropertyChangingEventHandler)
176 System.Delegate.Combine(_serializableChangingHandlers, value);
177 else
178 _nonSerializableChangingHandlers = (PropertyChangingEventHandler)
179 System.Delegate.Combine(_nonSerializableChangingHandlers, value);
180 }
181 remove
182 {
183 if (ShouldHandlerSerialize(value))
184 _serializableChangingHandlers = (PropertyChangingEventHandler)
185 System.Delegate.Remove(_serializableChangingHandlers, value);
186 else
187 _nonSerializableChangingHandlers = (PropertyChangingEventHandler)
188 System.Delegate.Remove(_nonSerializableChangingHandlers, value);
189 }
190 }
191
200 [EditorBrowsable(EditorBrowsableState.Advanced)]
201 protected virtual void OnIsDirtyChanging()
202 {
204 }
205
215 [EditorBrowsable(EditorBrowsableState.Advanced)]
216 protected virtual void OnUnknownPropertyChanging()
217 {
218 OnPropertyChanging(string.Empty);
219 }
220
231 [EditorBrowsable(EditorBrowsableState.Advanced)]
232 protected virtual void OnPropertyChanging(string propertyName)
233 {
234 if (_nonSerializableChangingHandlers != null)
235 _nonSerializableChangingHandlers.Invoke(this,
236 new PropertyChangingEventArgs(propertyName));
237 if (_serializableChangingHandlers != null)
238 _serializableChangingHandlers.Invoke(this,
239 new PropertyChangingEventArgs(propertyName));
240 }
241
252 [EditorBrowsable(EditorBrowsableState.Advanced)]
253 protected virtual void OnPropertyChanging(IPropertyInfo propertyInfo)
254 {
255 OnPropertyChanging(propertyInfo.Name);
256 }
257
264 protected virtual bool ShouldHandlerSerialize(PropertyChangingEventHandler value)
265 {
266 return value.Method.IsPublic &&
267 value.Method.DeclaringType != null &&
268 (value.Method.DeclaringType.IsSerializable || value.Method.IsStatic);
269 }
270 }
271}
This class implements INotifyPropertyChanged and INotifyPropertyChanging in a serialization-safe mann...
Definition: BindableBase.cs:23
virtual void OnIsDirtyChanging()
Call this method to raise the PropertyChanging event for all object properties.
virtual void OnPropertyChanged(string propertyName)
Call this method to raise the PropertyChanged event for a specific property.
Definition: BindableBase.cs:85
virtual bool ShouldHandlerSerialize(PropertyChangingEventHandler value)
Override this method to change the default logic for determining if the event handler should be seria...
PropertyChangedEventHandler PropertyChanged
Implements a serialization-safe PropertyChanged event.
Definition: BindableBase.cs:40
virtual void OnPropertyChanged(IPropertyInfo propertyInfo)
Call this method to raise the PropertyChanged event for a specific property.
virtual void OnMetaPropertyChanged(string propertyName)
PropertyChangingEventHandler PropertyChanging
Implements a serialization-safe PropertyChanging event.
BindableBase()
Creates an instance of the type.
Definition: BindableBase.cs:27
virtual void OnUnknownPropertyChanged()
Call this method to raise the PropertyChanged event for all object properties.
virtual void OnPropertyChanging(IPropertyInfo propertyInfo)
Call this method to raise the PropertyChanging event for a specific property.
virtual bool ShouldHandlerSerialize(PropertyChangedEventHandler value)
Override this method to change the default logic for determining if the event handler should be seria...
Definition: BindableBase.cs:67
virtual void OnPropertyChanging(string propertyName)
Call this method to raise the PropertyChanging event for a specific property.
virtual void OnIsDirtyChanged()
Call this method to raise the PropertyChanged event for all object properties.
virtual void OnUnknownPropertyChanging()
Call this method to raise the PropertyChanging event for all object properties.
Inherit from this base class to easily create a serializable class.
Definition: MobileObject.cs:20
string Name
Gets the member name value.
Definition: IMemberInfo.cs:23
Maintains metadata about a property.
@ Serializable
Prevents updating or inserting until the transaction is complete.