CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
MobileBindingList.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="MobileBindingList.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Inherit from this base class to easily</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.ComponentModel;
10using System.Collections.Generic;
12using Csla.Properties;
13using System.Reflection;
14using Csla.Reflection;
15using System.Diagnostics;
16
17namespace Csla.Core
18{
26#if TESTING
27 [System.Diagnostics.DebuggerStepThrough]
28#endif
30 public class MobileBindingList<T> : BindingList<T>, IMobileList
31 {
32 #region LoadListMode
33
34 [NonSerialized]
35 [NotUndoable]
36 private LoadListModeObject _loadListModeObject = null;
37
44 [DebuggerBrowsable(DebuggerBrowsableState.Never)]
46 {
47 get
48 {
49 if (_loadListModeObject == null)
50 {
51 _loadListModeObject = new LoadListModeObject(this);
52 SetLoadListMode(true);
53 }
54 return _loadListModeObject;
55 }
56 }
57 void IMobileList.SetLoadListMode(bool enabled)
58 {
59 _loadListModeObject = null;
60 SetLoadListMode(enabled);
61 if (_oldRLCE == null)
62 _oldRLCE = new Stack<bool>();
63 if (enabled)
64 {
65 _oldRLCE.Push(RaiseListChangedEvents);
66 RaiseListChangedEvents = false;
67 }
68 else
69 {
70 if (_oldRLCE.Count > 0)
71 RaiseListChangedEvents = _oldRLCE.Pop();
72 }
73 }
74
75 [NonSerialized]
76 [NotUndoable]
77 private Stack<bool> _oldRLCE;
78
83 protected virtual void SetLoadListMode(bool enabled)
84 {
85 }
86
93 [EditorBrowsable(EditorBrowsableState.Never)]
94#pragma warning disable CA1063 // Implement IDisposable Correctly
95 protected class LoadListModeObject : IDisposable
96#pragma warning restore CA1063 // Implement IDisposable Correctly
97 {
98 private readonly IMobileList _target;
104 {
105 _target = target;
106 _target.SetLoadListMode(true);
107 }
108
112#pragma warning disable CA1063 // Implement IDisposable Correctly
113 public void Dispose()
114#pragma warning restore CA1063 // Implement IDisposable Correctly
115 {
116 _target.SetLoadListMode(false);
117 GC.SuppressFinalize(this);
118 }
119 }
120
121 #endregion
122
123 #region IMobileObject Members
124
126 {
127 OnGetChildren(info, formatter);
128 }
129
131 {
132 OnGetState(info);
133 }
134
140 [EditorBrowsable(EditorBrowsableState.Advanced)]
141 protected virtual void OnGetState(SerializationInfo info)
142 {
143 info.AddValue("Csla.Core.MobileList.AllowEdit", AllowEdit);
144 info.AddValue("Csla.Core.MobileList.AllowNew", AllowNew);
145 info.AddValue("Csla.Core.MobileList.AllowRemove", AllowRemove);
146 info.AddValue("Csla.Core.MobileList.RaiseListChangedEvents", RaiseListChangedEvents);
147#if (ANDROID || IOS) || NETFX_CORE
148 info.AddValue("Csla.Core.MobileList._supportsChangeNotificationCore", SupportsChangeNotificationCore);
149#endif
150 }
151
158 [EditorBrowsable(EditorBrowsableState.Advanced)]
159 protected virtual void OnGetChildren(SerializationInfo info, MobileFormatter formatter)
160 {
161 if (!typeof(IMobileObject).IsAssignableFrom(typeof(T)))
162 throw new InvalidOperationException(Resources.CannotSerializeCollectionsNotOfIMobileObject);
163
164 List<int> references = new List<int>();
165 for (int x = 0; x < this.Count; x++)
166 {
167 T child = this[x];
168 if (child != null)
169 {
170 SerializationInfo childInfo = formatter.SerializeObject(child);
171 references.Add(childInfo.ReferenceId);
172 }
173 }
174 if (references.Count > 0)
175 info.AddValue("$list", references);
176 }
177
179 {
180 OnSetState(info);
181 }
182
184 {
185 OnSetChildren(info, formatter);
186 }
187
193 [EditorBrowsable(EditorBrowsableState.Advanced)]
194 protected virtual void OnSetState(SerializationInfo info)
195 {
196 AllowEdit = info.GetValue<bool>("Csla.Core.MobileList.AllowEdit");
197 AllowNew = info.GetValue<bool>("Csla.Core.MobileList.AllowNew");
198 AllowRemove = info.GetValue<bool>("Csla.Core.MobileList.AllowRemove");
199 RaiseListChangedEvents = info.GetValue<bool>("Csla.Core.MobileList.RaiseListChangedEvents");
200 }
201
208 [EditorBrowsable(EditorBrowsableState.Advanced)]
209 protected virtual void OnSetChildren(SerializationInfo info, MobileFormatter formatter)
210 {
211 if (!typeof(IMobileObject).IsAssignableFrom(typeof(T)))
212 throw new InvalidOperationException(Resources.CannotSerializeCollectionsNotOfIMobileObject);
213
214 bool originalRaiseListChangedEvents = this.RaiseListChangedEvents;
215
216 try
217 {
218 this.RaiseListChangedEvents = false;
219
220 if (info.Values.ContainsKey("$list"))
221 {
222 List<int> references = (List<int>)info.Values["$list"].Value;
223 foreach (int reference in references)
224 {
225 T child = (T)formatter.GetObject(reference);
226 this.Add(child);
227 }
228 }
229 }
230 finally
231 {
232 this.RaiseListChangedEvents = originalRaiseListChangedEvents;
233 }
234 }
235
236 #endregion
237 }
238}
Class that allows setting of property values on current business object without raising PropertyChang...
LoadListModeObject(IMobileList target)
Create instance of type
Inherit from this base class to easily create a serializable list class.
virtual void OnSetState(SerializationInfo info)
Override this method to set custom field values into the serialization stream.
virtual void OnGetState(SerializationInfo info)
Override this method to get custom field values from the serialization stream.
virtual void OnSetChildren(SerializationInfo info, MobileFormatter formatter)
Override this method to set custom child object values into the serialization stream.
virtual void OnGetChildren(SerializationInfo info, MobileFormatter formatter)
Override this method to get custom child object values from the serialization stream.
LoadListModeObject LoadListMode
By wrapping this property inside Using block you can set property values on current business object w...
virtual void SetLoadListMode(bool enabled)
Sets the load list mode for the list
A strongly-typed resource class, for looking up localized strings, etc.
static string CannotSerializeCollectionsNotOfIMobileObject
Looks up a localized string similar to Cannot serialize collections not of type IMobileObject.
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 containing the serialization data for a specific object.
Dictionary< string, FieldData > Values
Dictionary containg field data.
int ReferenceId
Reference number for this object.
void AddValue(string name, object value)
Adds a value to the serialization stream.
Extension of IMobileObject for list types
Definition: IMobileList.cs:14
void SetLoadListMode(bool enabled)
Sets the LoadListMode for the collection
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.
@ Serializable
Prevents updating or inserting until the transaction is complete.