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.
FieldData.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="FieldData.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Contains a field value and related metadata.</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.ComponentModel;
10using System.ComponentModel.DataAnnotations;
12
14{
19 [Serializable()]
20 public class FieldData<T> : IFieldData<T>
21 {
22 [NonSerialized]
23 [NotUndoable]
24 private readonly bool _isChild = typeof(T).IsAssignableFrom(typeof(IMobileObject));
25 private T _data;
26 private bool _isDirty;
27
31 public FieldData() { }
32
39 public FieldData(string name)
40 {
41 Name = name;
42 }
43
47 public string Name { get; private set; }
48
52 public virtual T Value
53 {
54 get
55 {
56 return _data;
57 }
58 set
59 {
60 _data = value;
61 _isDirty = true;
62 }
63 }
64
65 object IFieldData.Value
66 {
67 get
68 {
69 return this.Value;
70 }
71 set
72 {
73 if (value == null)
74 this.Value = default(T);
75 else
76 this.Value = (T)value;
77 }
78 }
79
80 bool ITrackStatus.IsDeleted
81 {
82 get
83 {
84 if (_data is ITrackStatus child)
85 return child.IsDeleted;
86 else
87 return false;
88 }
89 }
90
91 bool ITrackStatus.IsSavable
92 {
93 get { return true; }
94 }
95
96 bool ITrackStatus.IsChild
97 {
98 get
99 {
100 if (_data is ITrackStatus child)
101 return child.IsChild;
102 else
103 return false;
104 }
105 }
106
111 public virtual bool IsSelfDirty
112 {
113 get { return IsDirty; }
114 }
115
120 public virtual bool IsDirty
121 {
122 get
123 {
124 if (_data is ITrackStatus child)
125 return child.IsDirty;
126 else
127 return _isDirty;
128 }
129 }
130
134 public virtual void MarkClean()
135 {
136 _isDirty = false;
137 }
138
140 {
141 get
142 {
143 if (_data is ITrackStatus child)
144 return child.IsNew;
145 else
146 return false;
147 }
148 }
149
150 bool ITrackStatus.IsSelfValid
151 {
152 get { return IsValid; }
153 }
154
155 bool ITrackStatus.IsValid
156 {
157 get { return IsValid; }
158 }
159
164 protected virtual bool IsValid
165 {
166 get
167 {
168 if (_data is ITrackStatus child)
169 return child.IsValid;
170 else
171 return true;
172 }
173 }
174
176 {
177 add { throw new NotImplementedException(); }
178 remove { throw new NotImplementedException(); }
179 }
180
185 [Browsable(false)]
186 [Display(AutoGenerateField = false)]
187 [System.ComponentModel.DataAnnotations.ScaffoldColumn(false)]
188 public bool IsBusy
189 {
190 get
191 {
192 bool isBusy = false;
193 if (_data is ITrackStatus child)
194 isBusy = child.IsBusy;
195
196 return isBusy;
197 }
198 }
199
201 {
202 get { return IsBusy; }
203 }
204
205 T IFieldData<T>.Value { get => Value; set => Value = value; }
206
207 string IFieldData.Name => Name;
208
209 bool ITrackStatus.IsDirty => IsDirty;
210
211 bool ITrackStatus.IsSelfDirty => IsDirty;
212
213 bool INotifyBusy.IsBusy => IsBusy;
214
215 [NotUndoable]
216 [NonSerialized]
217 private EventHandler<ErrorEventArgs> _unhandledAsyncException;
218
223 public event EventHandler<ErrorEventArgs> UnhandledAsyncException
224 {
225 add { _unhandledAsyncException = (EventHandler<ErrorEventArgs>)Delegate.Combine(_unhandledAsyncException, value); }
226 remove { _unhandledAsyncException = (EventHandler<ErrorEventArgs>)Delegate.Remove(_unhandledAsyncException, value); }
227 }
228
229 event EventHandler<ErrorEventArgs> INotifyUnhandledAsyncException.UnhandledAsyncException
230 {
231 add { _unhandledAsyncException = (EventHandler<ErrorEventArgs>)Delegate.Combine(_unhandledAsyncException, value); }
232 remove { _unhandledAsyncException = (EventHandler<ErrorEventArgs>)Delegate.Remove(_unhandledAsyncException, value); }
233 }
234
235 void IFieldData.MarkClean()
236 {
237 MarkClean();
238 }
239
241 {
242 if (!_isChild)
243 {
244 info.AddValue("_name", Name);
245 info.AddValue("_data", _data);
246 info.AddValue("_isDirty", _isDirty);
247 }
248 }
249
251 {
252 if (_isChild)
253 {
254 info.AddValue("_name", Name);
255 SerializationInfo childInfo = formatter.SerializeObject((IMobileObject)_data);
256 info.AddChild(Name, childInfo.ReferenceId, _isDirty);
257 }
258 }
259
261 {
262 if (!_isChild)
263 {
264 Name = info.GetValue<string>("_name");
265 _data = info.GetValue<T>("_data");
266 _isDirty = info.GetValue<bool>("_isDirty");
267 }
268 }
269
271 {
272 if (_isChild)
273 {
274 Name = info.GetValue<string>("_name");
275 SerializationInfo.ChildData childData = info.Children[Name];
276 _data = (T)formatter.GetObject(childData.ReferenceId);
277 }
278 }
279 }
280}
Contains a field value and related metadata.
Definition: FieldData.cs:21
FieldData(string name)
Creates a new instance of the object.
Definition: FieldData.cs:39
virtual T Value
Gets or sets the value of the field.
Definition: FieldData.cs:53
string Name
Gets the name of the field.
Definition: FieldData.cs:47
bool IsBusy
Gets a value indicating whether this object or any of its child objects are busy.
Definition: FieldData.cs:189
virtual bool IsSelfDirty
Gets a value indicating whether the field has been changed.
Definition: FieldData.cs:112
virtual void MarkClean()
Marks the field as unchanged.
Definition: FieldData.cs:134
EventHandler< ErrorEventArgs > UnhandledAsyncException
Event indicating that an exception occurred on a background thread.
Definition: FieldData.cs:224
virtual bool IsDirty
Gets a value indicating whether the field has been changed.
Definition: FieldData.cs:121
FieldData()
Creates a new instance of the object.
Definition: FieldData.cs:31
Serializes and deserializes objects at the field level.
IMobileObject GetObject(int referenceId)
Gets a deserialized object based on the object's reference id within the serialization stream.
SerializationInfo SerializeObject(object obj)
Serializes an object into a SerializationInfo object.
Object that contains information about a single child reference.
Object containing the serialization data for a specific object.
int ReferenceId
Reference number for this object.
Dictionary< string, ChildData > Children
Dictionary containing child reference data.
void AddChild(string name, int referenceId)
Adds a child to the list of child references.
void AddValue(string name, object value)
Adds a value to the serialization stream.
Defines the members required by a field data storage object.
Definition: IFieldDataT.cs:15
object Value
Gets or sets the field value.
Definition: IFieldData.cs:27
Interface defining an object that notifies when it is busy executing an asynchronous operation.
Definition: INotifyBusy.cs:17
bool IsSelfBusy
Gets a value indicating whether the object is busy running an asynchronous operation.
Definition: INotifyBusy.cs:34
BusyChangedEventHandler BusyChanged
Event raised when the object's busy status changes.
Definition: INotifyBusy.cs:22
Implemented by an object that perfoms asynchronous operations that may raise exceptions.
EventHandler< ErrorEventArgs > UnhandledAsyncException
Event indicating that an exception occurred during an asynchronous operation.
Defines the common properties required objects that track their own status.
Definition: ITrackStatus.cs:17
bool IsNew
Returns true if this is a new object, false if it is a pre-existing object.
bool IsValid
Returns true if the object and its child objects are currently valid, false if the object or any of i...
Definition: ITrackStatus.cs:37
Interface to be implemented by any object that supports serialization by the SerializationFormatterFa...
void GetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should serialize its child references.
void GetState(SerializationInfo info)
Method called by MobileFormatter when an object should serialize its data.
void SetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should deserialize its child references.
void SetState(SerializationInfo info)
Method called by MobileFormatter when an object should be deserialized.
delegate void BusyChangedEventHandler(object sender, BusyChangedEventArgs e)
Delegate for handling the BusyChanged event.
@ Serializable
Prevents updating or inserting until the transaction is complete.