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.
CslaBinaryWriter.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using Csla.Properties;
5using Csla.Reflection;
6
8{
14 {
15 private readonly Dictionary<string, int> keywordsDictionary;
16 private ApplicationContext ApplicationContext { get; set; }
17
22 public CslaBinaryWriter(ApplicationContext applicationContext)
23 {
24 ApplicationContext = applicationContext;
25 keywordsDictionary = new Dictionary<string, int>();
26 }
27
34 public void Write(Stream serializationStream, List<SerializationInfo> objectData)
35 {
36 this.keywordsDictionary.Clear();
37 using (var writer = new CslaNonClosingBinaryWriter(serializationStream))
38 {
39 writer.Write(objectData.Count);
40 foreach (var serializationInfo in objectData)
41 {
42 writer.Write(serializationInfo.ReferenceId);
43 WriteSystemString(serializationInfo.TypeName, writer);
44
45 writer.Write(serializationInfo.Children.Count);
46 foreach (var childData in serializationInfo.Children)
47 {
48 WriteSystemString(childData.Key, writer);
49 writer.Write(childData.Value.IsDirty);
50 writer.Write(childData.Value.ReferenceId);
51 }
52 writer.Write(serializationInfo.Values.Count);
53 foreach (var valueData in serializationInfo.Values)
54 {
55 WriteSystemString(valueData.Key, writer);
56 WriteSystemString(valueData.Value.EnumTypeName ?? string.Empty, writer);
57 writer.Write(valueData.Value.IsDirty);
58 Write(valueData.Value.Value, writer);
59 }
60 }
61 writer.Flush();
62 }
63 }
64
65 private void WriteSystemString(string systemString, BinaryWriter writer)
66 {
67 var checkResult = GetKey(systemString);
68 if (checkResult.IsNew)
69 {
70 Write(CslaKnownTypes.StringWithDictionaryKey, writer);
71 writer.Write(systemString);
72 writer.Write(checkResult.Key);
73 }
74 else
75 {
76 Write(CslaKnownTypes.StringDictionaryKey, writer);
77 writer.Write(checkResult.Key);
78 }
79 }
80
81 private DictionaryCheckResult GetKey(string value)
82 {
83 DictionaryCheckResult returnValue;
84
85 if (keywordsDictionary.TryGetValue(value, out var key))
86 {
87 returnValue = new DictionaryCheckResult(false, key);
88 }
89 else
90 {
91 returnValue = new DictionaryCheckResult(true, keywordsDictionary.Count);
92 keywordsDictionary.Add(value, returnValue.Key);
93 }
94 return returnValue;
95 }
96
97 private void Write(object target, BinaryWriter writer)
98 {
99 if (target == null)
100 {
101 Write(CslaKnownTypes.Null, writer);
102 }
103 else if (target is IMobileObject)
104 {
105 using (MemoryStream buffer = new MemoryStream())
106 {
107 var formatter = SerializationFormatterFactory.GetFormatter(ApplicationContext);
108 formatter.Serialize(buffer, target);
109 var data = buffer.ToArray();
110 Write(CslaKnownTypes.IMobileObject, writer);
111 writer.Write(data.Length);
112 writer.Write(data);
113 }
114 }
115 else if (target is CslaKnownTypes)
116 {
117 writer.Write((byte)((CslaKnownTypes)target));
118 }
119 else if (target is TimeSpan)
120 {
121 Write(CslaKnownTypes.TimeSpan, writer);
122 writer.Write(((TimeSpan)target).Ticks);
123 }
124 else if (target is DateTimeOffset)
125 {
126 Write(CslaKnownTypes.DateTimeOffset, writer);
127 writer.Write(((DateTimeOffset)target).Ticks);
128 writer.Write(((DateTimeOffset)target).Offset.Ticks);
129 }
130 else if (target is byte[])
131 {
132 Write(CslaKnownTypes.ByteArray, writer);
133 writer.Write(((byte[])target).Length);
134 writer.Write((byte[])target);
135 }
136 else if (target is byte[][] outerArray)
137 {
138 Write(CslaKnownTypes.ByteArrayArray, writer);
139 writer.Write(outerArray.Length);
140 foreach (var item in outerArray)
141 {
142 writer.Write(item.Length);
143 writer.Write(item);
144 }
145 }
146 else if (target is char[])
147 {
148 Write(CslaKnownTypes.CharArray, writer);
149 writer.Write(((char[])target).Length);
150 writer.Write((char[])target);
151 }
152 else if (target is Guid)
153 {
154 Write(CslaKnownTypes.Guid, writer);
155 writer.Write(((Guid)target).ToByteArray());
156 }
157 else if (target is List<int>)
158 {
159 Write(CslaKnownTypes.ListOfInt, writer);
160 writer.Write(((List<int>)target).Count);
161 foreach (var oneInt in ((List<int>)target))
162 {
163 writer.Write(oneInt);
164 }
165 }
166 else
167 {
168 var typeCode = Type.GetTypeCode(target.GetType());
169 switch (typeCode)
170 {
171 case TypeCode.Boolean:
172 Write(CslaKnownTypes.Boolean, writer);
173 writer.Write((bool)target);
174 break;
175 case TypeCode.Char:
176 Write(CslaKnownTypes.Char, writer);
177 writer.Write((char)target);
178 break;
179 case TypeCode.SByte:
180 Write(CslaKnownTypes.SByte, writer);
181 writer.Write((sbyte)target);
182 break;
183 case TypeCode.Byte:
184 Write(CslaKnownTypes.Byte, writer);
185 writer.Write((byte)target);
186 break;
187 case TypeCode.Int16:
188 Write(CslaKnownTypes.Int16, writer);
189 writer.Write((short)target);
190 break;
191 case TypeCode.UInt16:
192 Write(CslaKnownTypes.UInt16, writer);
193 writer.Write((ushort)target);
194 break;
195 case TypeCode.Int32:
196 Write(CslaKnownTypes.Int32, writer);
197 writer.Write((int)target);
198 break;
199 case TypeCode.UInt32:
200 Write(CslaKnownTypes.UInt32, writer);
201 writer.Write((uint)target);
202 break;
203 case TypeCode.Int64:
204 Write(CslaKnownTypes.Int64, writer);
205 writer.Write((long)target);
206 break;
207 case TypeCode.UInt64:
208 Write(CslaKnownTypes.UInt64, writer);
209 writer.Write((ulong)target);
210 break;
211 case TypeCode.Single:
212 Write(CslaKnownTypes.Single, writer);
213 writer.Write((float)target);
214 break;
215 case TypeCode.Double:
216 Write(CslaKnownTypes.Double, writer);
217 writer.Write((double)target);
218 break;
219 case TypeCode.Decimal:
220 Write(CslaKnownTypes.Decimal, writer);
221 var bits = Decimal.GetBits((decimal)target);
222 foreach (var bit in bits)
223 {
224 writer.Write(bit);
225 }
226 break;
227 case TypeCode.DateTime:
228 Write(CslaKnownTypes.DateTime, writer);
229 var value = ((DateTime)target).Ticks;
230 writer.Write(value);
231 break;
232 case TypeCode.String:
233 Write(CslaKnownTypes.String, writer);
234 writer.Write((string)target);
235 break;
236 default:
237 throw new NotSupportedException(
238 $"{Resources.BinaryWriterObjectSerializationException} ({target.GetType().FullName})");
239 }
240 }
241 }
242 }
243}
Provides consistent context information between the client and server DataPortal objects.
This is a class that is responsible for serializing SerializationInfo objects into a Stream for sendi...
CslaBinaryWriter(ApplicationContext applicationContext)
Create new instance of CslaBinaryWriter class
void Write(Stream serializationStream, List< SerializationInfo > objectData)
Write a list of SerializationInfo objects into stream, typically MemoryStream.
This class is used to get around the issue in .NET framework, where underlying stream is closed by a ...
Represents a class that can be used to write a list of SerializationInfo objects into a stream,...
Definition: ICslaWriter.cs:12
CslaKnownTypes
This enumeration contains the list of known types that CslaBinaryReader and CslaBinaryWriterknow abou...