0
|
1 using System;
|
|
2 using System.Collections;
|
|
3 using System.Collections.Generic;
|
|
4 using System.Data;
|
|
5 using System.Data.Linq;
|
|
6 using System.Data.SqlTypes;
|
|
7 using System.Diagnostics;
|
|
8 using System.Linq;
|
|
9 using System.IO;
|
|
10 using System.Reflection;
|
|
11 using System.Diagnostics.CodeAnalysis;
|
|
12 using System.Threading;
|
|
13 using System.Xml;
|
|
14
|
|
15 #if !SILVERLIGHT
|
|
16 using System.Xml.Linq;
|
|
17 #endif
|
|
18
|
|
19 using BLToolkit.Common;
|
|
20 using BLToolkit.Properties;
|
|
21 using BLToolkit.Reflection;
|
|
22 using BLToolkit.Reflection.Extension;
|
|
23 using BLToolkit.Reflection.MetadataProvider;
|
|
24
|
|
25 #region ReSharper disable
|
|
26 // ReSharper disable SuggestUseVarKeywordEvident
|
|
27 // ReSharper disable UseObjectOrCollectionInitializer
|
|
28 // ReSharper disable SuggestUseVarKeywordEverywhere
|
|
29 // ReSharper disable RedundantTypeArgumentsOfMethod
|
|
30 #endregion
|
|
31
|
|
32 using KeyValue = System.Collections.Generic.KeyValuePair<System.Type,System.Type>;
|
|
33 using Convert = BLToolkit.Common.Convert;
|
|
34
|
|
35 namespace BLToolkit.Mapping
|
|
36 {
|
|
37 public class MappingSchema
|
|
38 {
|
|
39 #region Constructors
|
|
40
|
|
41 public MappingSchema()
|
|
42 {
|
|
43 InitNullValues();
|
|
44 }
|
|
45
|
|
46 #endregion
|
|
47
|
|
48 #region ObjectMapper Support
|
|
49
|
|
50 private readonly Dictionary<Type,ObjectMapper> _mappers = new Dictionary<Type,ObjectMapper>();
|
|
51 private readonly Dictionary<Type,ObjectMapper> _pendingMappers = new Dictionary<Type,ObjectMapper>();
|
|
52
|
|
53 public ObjectMapper GetObjectMapper(Type type)
|
|
54 {
|
|
55 ObjectMapper om;
|
|
56
|
|
57 lock (_mappers)
|
|
58 {
|
|
59 if (_mappers.TryGetValue(type, out om))
|
|
60 return om;
|
|
61
|
|
62 // This object mapper is initializing right now.
|
|
63 // Note that only one thread can access to _pendingMappers each time.
|
|
64 //
|
|
65 if (_pendingMappers.TryGetValue(type, out om))
|
|
66 return om;
|
|
67
|
|
68 om = CreateObjectMapper(type);
|
|
69
|
|
70 if (om == null)
|
|
71 throw new MappingException(
|
|
72 string.Format("Cannot create object mapper for the '{0}' type.", type.FullName));
|
|
73
|
|
74 _pendingMappers.Add(type, om);
|
|
75
|
|
76 try
|
|
77 {
|
|
78 om.Init(this, type);
|
|
79 }
|
|
80 finally
|
|
81 {
|
|
82 _pendingMappers.Remove(type);
|
|
83 }
|
|
84
|
|
85 // Officially publish this ready to use object mapper.
|
|
86 //
|
|
87 SetObjectMapperInternal(type, om);
|
|
88
|
|
89 return om;
|
|
90 }
|
|
91 }
|
|
92
|
|
93 private void SetObjectMapperInternal(Type type, ObjectMapper om)
|
|
94 {
|
|
95 _mappers.Add(type, om);
|
|
96
|
|
97 if (type.IsAbstract)
|
|
98 {
|
|
99 var actualType = TypeAccessor.GetAccessor(type).Type;
|
|
100
|
|
101 if (!_mappers.ContainsKey(actualType))
|
|
102 _mappers.Add(actualType, om);
|
|
103 }
|
|
104 }
|
|
105
|
|
106 public void SetObjectMapper(Type type, ObjectMapper om)
|
|
107 {
|
|
108 if (type == null) throw new ArgumentNullException("type");
|
|
109
|
|
110 lock (_mappers)
|
|
111 SetObjectMapperInternal(type, om);
|
|
112 }
|
|
113
|
|
114 protected virtual ObjectMapper CreateObjectMapper(Type type)
|
|
115 {
|
|
116 Attribute attr = TypeHelper.GetFirstAttribute(type, typeof(ObjectMapperAttribute));
|
|
117 return attr == null? CreateObjectMapperInstance(type): ((ObjectMapperAttribute)attr).ObjectMapper;
|
|
118 }
|
|
119
|
|
120 protected virtual ObjectMapper CreateObjectMapperInstance(Type type)
|
|
121 {
|
|
122 return new ObjectMapper();
|
|
123 }
|
|
124
|
|
125 #endregion
|
|
126
|
|
127 #region MetadataProvider
|
|
128
|
|
129 private MetadataProviderBase _metadataProvider;
|
|
130 public MetadataProviderBase MetadataProvider
|
|
131 {
|
|
132 [DebuggerStepThrough]
|
|
133 get { return _metadataProvider ?? (_metadataProvider = CreateMetadataProvider()); }
|
|
134 set { _metadataProvider = value; }
|
|
135 }
|
|
136
|
|
137 protected virtual MetadataProviderBase CreateMetadataProvider()
|
|
138 {
|
|
139 return MetadataProviderBase.CreateProvider();
|
|
140 }
|
|
141
|
|
142 #endregion
|
|
143
|
|
144 #region Public Members
|
|
145
|
|
146 public virtual ExtensionList Extensions { get; set; }
|
|
147
|
|
148 #endregion
|
|
149
|
|
150 #region Convert
|
|
151
|
|
152 public virtual void InitNullValues()
|
|
153 {
|
|
154 DefaultSByteNullValue = (SByte) GetNullValue(typeof(SByte));
|
|
155 DefaultInt16NullValue = (Int16) GetNullValue(typeof(Int16));
|
|
156 DefaultInt32NullValue = (Int32) GetNullValue(typeof(Int32));
|
|
157 DefaultInt64NullValue = (Int64) GetNullValue(typeof(Int64));
|
|
158 DefaultByteNullValue = (Byte) GetNullValue(typeof(Byte));
|
|
159 DefaultUInt16NullValue = (UInt16) GetNullValue(typeof(UInt16));
|
|
160 DefaultUInt32NullValue = (UInt32) GetNullValue(typeof(UInt32));
|
|
161 DefaultUInt64NullValue = (UInt64) GetNullValue(typeof(UInt64));
|
|
162 DefaultCharNullValue = (Char) GetNullValue(typeof(Char));
|
|
163 DefaultSingleNullValue = (Single) GetNullValue(typeof(Single));
|
|
164 DefaultDoubleNullValue = (Double) GetNullValue(typeof(Double));
|
|
165 DefaultBooleanNullValue = (Boolean) GetNullValue(typeof(Boolean));
|
|
166
|
|
167 DefaultStringNullValue = (String) GetNullValue(typeof(String));
|
|
168 DefaultDateTimeNullValue = (DateTime) GetNullValue(typeof(DateTime));
|
|
169 DefaultDateTimeOffsetNullValue = (DateTimeOffset)GetNullValue(typeof(DateTimeOffset));
|
|
170 DefaultLinqBinaryNullValue = (Binary) GetNullValue(typeof(Binary));
|
|
171 DefaultDecimalNullValue = (Decimal) GetNullValue(typeof(Decimal));
|
|
172 DefaultGuidNullValue = (Guid) GetNullValue(typeof(Guid));
|
|
173 DefaultStreamNullValue = (Stream) GetNullValue(typeof(Stream));
|
|
174 #if !SILVERLIGHT
|
|
175 DefaultXmlReaderNullValue = (XmlReader) GetNullValue(typeof(XmlReader));
|
|
176 DefaultXmlDocumentNullValue = (XmlDocument) GetNullValue(typeof(XmlDocument));
|
|
177 DefaultXElementNullValue = (XElement) GetNullValue(typeof(XElement));
|
|
178 #endif
|
|
179 }
|
|
180
|
|
181 #region Primitive Types
|
|
182
|
|
183 [CLSCompliant(false)]
|
|
184 public sbyte DefaultSByteNullValue { get; set; }
|
|
185
|
|
186 [CLSCompliant(false)]
|
|
187 public virtual SByte ConvertToSByte(object value)
|
|
188 {
|
|
189 return
|
|
190 value is SByte ? (SByte)value :
|
|
191 value is Byte ? (SByte)(Byte)value :
|
|
192 value == null ? DefaultSByteNullValue :
|
|
193 Convert.ToSByte(value);
|
|
194 }
|
|
195
|
|
196 public short DefaultInt16NullValue { get; set; }
|
|
197
|
|
198 public virtual Int16 ConvertToInt16(object value)
|
|
199 {
|
|
200 return
|
|
201 value is Int16? (Int16)value:
|
|
202 value == null || value is DBNull? DefaultInt16NullValue:
|
|
203 Convert.ToInt16(value);
|
|
204 }
|
|
205
|
|
206 public int DefaultInt32NullValue { get; set; }
|
|
207
|
|
208 public virtual Int32 ConvertToInt32(object value)
|
|
209 {
|
|
210 return
|
|
211 value is Int32? (Int32)value:
|
|
212 value == null || value is DBNull? DefaultInt32NullValue:
|
|
213 Convert.ToInt32(value);
|
|
214 }
|
|
215
|
|
216 public long DefaultInt64NullValue { get; set; }
|
|
217
|
|
218 public virtual Int64 ConvertToInt64(object value)
|
|
219 {
|
|
220 return
|
|
221 value is Int64? (Int64)value:
|
|
222 value == null || value is DBNull? DefaultInt64NullValue:
|
|
223 Convert.ToInt64(value);
|
|
224 }
|
|
225
|
|
226 public byte DefaultByteNullValue { get; set; }
|
|
227
|
|
228 public virtual Byte ConvertToByte(object value)
|
|
229 {
|
|
230 return
|
|
231 value is Byte? (Byte)value:
|
|
232 value == null || value is DBNull? DefaultByteNullValue:
|
|
233 Convert.ToByte(value);
|
|
234 }
|
|
235
|
|
236 [CLSCompliant(false)]
|
|
237 public ushort DefaultUInt16NullValue { get; set; }
|
|
238
|
|
239 [CLSCompliant(false)]
|
|
240 public virtual UInt16 ConvertToUInt16(object value)
|
|
241 {
|
|
242 return
|
|
243 value is UInt16? (UInt16)value:
|
|
244 value is Int16? (UInt16)(Int16)value:
|
|
245 value == null || value is DBNull? DefaultUInt16NullValue:
|
|
246 Convert.ToUInt16(value);
|
|
247 }
|
|
248
|
|
249 [CLSCompliant(false)]
|
|
250 public uint DefaultUInt32NullValue { get; set; }
|
|
251
|
|
252 [CLSCompliant(false)]
|
|
253 public virtual UInt32 ConvertToUInt32(object value)
|
|
254 {
|
|
255 return
|
|
256 value is UInt32? (UInt32)value:
|
|
257 value is Int32? (UInt32)(Int32)value:
|
|
258 value == null || value is DBNull? DefaultUInt32NullValue:
|
|
259 Convert.ToUInt32(value);
|
|
260 }
|
|
261
|
|
262 [CLSCompliant(false)]
|
|
263 public ulong DefaultUInt64NullValue { get; set; }
|
|
264
|
|
265 [CLSCompliant(false)]
|
|
266 public virtual UInt64 ConvertToUInt64(object value)
|
|
267 {
|
|
268 return
|
|
269 value is UInt64? (UInt64)value:
|
|
270 value is Int64? (UInt64)(Int64)value:
|
|
271 value == null || value is DBNull? DefaultUInt64NullValue:
|
|
272 Convert.ToUInt64(value);
|
|
273 }
|
|
274
|
|
275 public char DefaultCharNullValue { get; set; }
|
|
276
|
|
277 public virtual Char ConvertToChar(object value)
|
|
278 {
|
|
279 return
|
|
280 value is Char? (Char)value:
|
|
281 value == null || value is DBNull? DefaultCharNullValue:
|
|
282 Convert.ToChar(value);
|
|
283 }
|
|
284
|
|
285 public float DefaultSingleNullValue { get; set; }
|
|
286
|
|
287 public virtual Single ConvertToSingle(object value)
|
|
288 {
|
|
289 return
|
|
290 value is Single? (Single)value:
|
|
291 value == null || value is DBNull? DefaultSingleNullValue:
|
|
292 Convert.ToSingle(value);
|
|
293 }
|
|
294
|
|
295 public double DefaultDoubleNullValue { get; set; }
|
|
296
|
|
297 public virtual Double ConvertToDouble(object value)
|
|
298 {
|
|
299 return
|
|
300 value is Double? (Double)value:
|
|
301 value == null || value is DBNull? DefaultDoubleNullValue:
|
|
302 Convert.ToDouble(value);
|
|
303 }
|
|
304
|
|
305 public bool DefaultBooleanNullValue { get; set; }
|
|
306
|
|
307 public virtual Boolean ConvertToBoolean(object value)
|
|
308 {
|
|
309 return
|
|
310 value is Boolean? (Boolean)value:
|
|
311 value == null || value is DBNull? DefaultBooleanNullValue:
|
|
312 Convert.ToBoolean(value);
|
|
313 }
|
|
314
|
|
315 #endregion
|
|
316
|
|
317 #region Simple Types
|
|
318
|
|
319 public string DefaultStringNullValue { get; set; }
|
|
320
|
|
321 public virtual String ConvertToString(object value)
|
|
322 {
|
|
323 return
|
|
324 value is String? (String)value :
|
|
325 value == null || value is DBNull? DefaultStringNullValue:
|
|
326 Convert.ToString(value);
|
|
327 }
|
|
328
|
|
329 public DateTime DefaultDateTimeNullValue { get; set; }
|
|
330
|
|
331 public virtual DateTime ConvertToDateTime(object value)
|
|
332 {
|
|
333 return
|
|
334 value is DateTime? (DateTime)value:
|
|
335 value == null || value is DBNull? DefaultDateTimeNullValue:
|
|
336 Convert.ToDateTime(value);
|
|
337 }
|
|
338
|
|
339 public virtual TimeSpan ConvertToTimeSpan(object value)
|
|
340 {
|
|
341 return ConvertToDateTime(value).TimeOfDay;
|
|
342 }
|
|
343
|
|
344 public DateTimeOffset DefaultDateTimeOffsetNullValue { get; set; }
|
|
345
|
|
346 public virtual DateTimeOffset ConvertToDateTimeOffset(object value)
|
|
347 {
|
|
348 return
|
|
349 value is DateTimeOffset? (DateTimeOffset)value:
|
|
350 value == null || value is DBNull? DefaultDateTimeOffsetNullValue:
|
|
351 Convert.ToDateTimeOffset(value);
|
|
352 }
|
|
353
|
|
354 public Binary DefaultLinqBinaryNullValue { get; set; }
|
|
355
|
|
356 public virtual Binary ConvertToLinqBinary(object value)
|
|
357 {
|
|
358 return
|
|
359 value is Binary ? (Binary)value:
|
|
360 value is byte[] ? new Binary((byte[])value) :
|
|
361 value == null || value is DBNull? DefaultLinqBinaryNullValue:
|
|
362 Convert.ToLinqBinary(value);
|
|
363 }
|
|
364
|
|
365 public decimal DefaultDecimalNullValue { get; set; }
|
|
366
|
|
367 public virtual Decimal ConvertToDecimal(object value)
|
|
368 {
|
|
369 return
|
|
370 value is Decimal? (Decimal)value:
|
|
371 value == null || value is DBNull? DefaultDecimalNullValue:
|
|
372 Convert.ToDecimal(value);
|
|
373 }
|
|
374
|
|
375 public Guid DefaultGuidNullValue { get; set; }
|
|
376
|
|
377 public virtual Guid ConvertToGuid(object value)
|
|
378 {
|
|
379 return
|
|
380 value is Guid? (Guid)value:
|
|
381 value == null || value is DBNull? DefaultGuidNullValue:
|
|
382 Convert.ToGuid(value);
|
|
383 }
|
|
384
|
|
385 public Stream DefaultStreamNullValue { get; set; }
|
|
386
|
|
387 public virtual Stream ConvertToStream(object value)
|
|
388 {
|
|
389 return
|
|
390 value is Stream? (Stream)value:
|
|
391 value == null || value is DBNull? DefaultStreamNullValue:
|
|
392 Convert.ToStream(value);
|
|
393 }
|
|
394
|
|
395 #if !SILVERLIGHT
|
|
396
|
|
397 public XmlReader DefaultXmlReaderNullValue { get; set; }
|
|
398
|
|
399 public virtual XmlReader ConvertToXmlReader(object value)
|
|
400 {
|
|
401 return
|
|
402 value is XmlReader? (XmlReader)value:
|
|
403 value == null || value is DBNull? DefaultXmlReaderNullValue:
|
|
404 Convert.ToXmlReader(value);
|
|
405 }
|
|
406
|
|
407 public XmlDocument DefaultXmlDocumentNullValue { get; set; }
|
|
408
|
|
409 public virtual XmlDocument ConvertToXmlDocument(object value)
|
|
410 {
|
|
411 return
|
|
412 value is XmlDocument? (XmlDocument)value:
|
|
413 value == null || value is DBNull? DefaultXmlDocumentNullValue:
|
|
414 Convert.ToXmlDocument(value);
|
|
415 }
|
|
416
|
|
417 public XElement DefaultXElementNullValue { get; set; }
|
|
418
|
|
419 public virtual XElement ConvertToXElement(object value)
|
|
420 {
|
|
421 return
|
|
422 value is XElement ? (XElement)value :
|
|
423 value == null || value is DBNull ? DefaultXElementNullValue :
|
|
424 XElement.Parse(value.ToString());
|
|
425 }
|
|
426
|
|
427 #endif
|
|
428
|
|
429 public virtual byte[] ConvertToByteArray(object value)
|
|
430 {
|
|
431 return
|
|
432 value is byte[]? (byte[])value:
|
|
433 value == null || value is DBNull? null:
|
|
434 Convert.ToByteArray(value);
|
|
435 }
|
|
436
|
|
437 public virtual char[] ConvertToCharArray(object value)
|
|
438 {
|
|
439 return
|
|
440 value is char[]? (char[])value:
|
|
441 value == null || value is DBNull? null:
|
|
442 Convert.ToCharArray(value);
|
|
443 }
|
|
444
|
|
445 #endregion
|
|
446
|
|
447 #region Nullable Types
|
|
448
|
|
449 [CLSCompliant(false)]
|
|
450 public virtual SByte? ConvertToNullableSByte(object value)
|
|
451 {
|
|
452 return
|
|
453 value is SByte? (SByte?)value:
|
|
454 value is Byte? (SByte?)(Byte)value:
|
|
455 value == null || value is DBNull? null:
|
|
456 Convert.ToNullableSByte(value);
|
|
457 }
|
|
458
|
|
459 public virtual Int16? ConvertToNullableInt16(object value)
|
|
460 {
|
|
461 return
|
|
462 value is Int16? (Int16?)value:
|
|
463 value == null || value is DBNull? null:
|
|
464 Convert.ToNullableInt16(value);
|
|
465 }
|
|
466
|
|
467 public virtual Int32? ConvertToNullableInt32(object value)
|
|
468 {
|
|
469 return
|
|
470 value is Int32? (Int32?)value:
|
|
471 value == null || value is DBNull? null:
|
|
472 Convert.ToNullableInt32(value);
|
|
473 }
|
|
474
|
|
475 public virtual Int64? ConvertToNullableInt64(object value)
|
|
476 {
|
|
477 return
|
|
478 value is Int64? (Int64?)value:
|
|
479 value == null || value is DBNull? null:
|
|
480 Convert.ToNullableInt64(value);
|
|
481 }
|
|
482
|
|
483 public virtual Byte? ConvertToNullableByte(object value)
|
|
484 {
|
|
485 return
|
|
486 value is Byte? (Byte?)value:
|
|
487 value == null || value is DBNull? null:
|
|
488 Convert.ToNullableByte(value);
|
|
489 }
|
|
490
|
|
491 [CLSCompliant(false)]
|
|
492 public virtual UInt16? ConvertToNullableUInt16(object value)
|
|
493 {
|
|
494 return
|
|
495 value is UInt16? (UInt16?)value:
|
|
496 value is Int16? (UInt16?)(Int16)value:
|
|
497 value == null || value is DBNull? null:
|
|
498 Convert.ToNullableUInt16(value);
|
|
499 }
|
|
500
|
|
501 [CLSCompliant(false)]
|
|
502 public virtual UInt32? ConvertToNullableUInt32(object value)
|
|
503 {
|
|
504 return
|
|
505 value is UInt32? (UInt32?)value:
|
|
506 value is Int32? (UInt32?)(Int32)value:
|
|
507 value == null || value is DBNull? null:
|
|
508 Convert.ToNullableUInt32(value);
|
|
509 }
|
|
510
|
|
511 [CLSCompliant(false)]
|
|
512 public virtual UInt64? ConvertToNullableUInt64(object value)
|
|
513 {
|
|
514 return
|
|
515 value is UInt64? (UInt64?)value:
|
|
516 value is Int64? (UInt64?)(Int64)value:
|
|
517 value == null || value is DBNull? null:
|
|
518 Convert.ToNullableUInt64(value);
|
|
519 }
|
|
520
|
|
521 public virtual Char? ConvertToNullableChar(object value)
|
|
522 {
|
|
523 return
|
|
524 value is Char? (Char?)value:
|
|
525 value == null || value is DBNull? null:
|
|
526 Convert.ToNullableChar(value);
|
|
527 }
|
|
528
|
|
529 public virtual Double? ConvertToNullableDouble(object value)
|
|
530 {
|
|
531 return
|
|
532 value is Double? (Double?)value:
|
|
533 value == null || value is DBNull? null:
|
|
534 Convert.ToNullableDouble(value);
|
|
535 }
|
|
536
|
|
537 public virtual Single? ConvertToNullableSingle(object value)
|
|
538 {
|
|
539 return
|
|
540 value is Single? (Single?)value:
|
|
541 value == null || value is DBNull? null:
|
|
542 Convert.ToNullableSingle(value);
|
|
543 }
|
|
544
|
|
545 public virtual Boolean? ConvertToNullableBoolean(object value)
|
|
546 {
|
|
547 return
|
|
548 value is Boolean? (Boolean?)value:
|
|
549 value == null || value is DBNull? null:
|
|
550 Convert.ToNullableBoolean(value);
|
|
551 }
|
|
552
|
|
553 public virtual DateTime? ConvertToNullableDateTime(object value)
|
|
554 {
|
|
555 return
|
|
556 value is DateTime? (DateTime?)value:
|
|
557 value == null || value is DBNull? null:
|
|
558 Convert.ToNullableDateTime(value);
|
|
559 }
|
|
560
|
|
561 public virtual TimeSpan? ConvertToNullableTimeSpan(object value)
|
|
562 {
|
|
563 DateTime? dt = ConvertToNullableDateTime(value);
|
|
564 return dt == null? null : (TimeSpan?)dt.Value.TimeOfDay;
|
|
565 }
|
|
566
|
|
567 public virtual DateTimeOffset? ConvertToNullableDateTimeOffset(object value)
|
|
568 {
|
|
569 return
|
|
570 value is DateTimeOffset? (DateTimeOffset?)value:
|
|
571 value == null || value is DBNull? null:
|
|
572 Convert.ToNullableDateTimeOffset(value);
|
|
573 }
|
|
574
|
|
575 public virtual Decimal? ConvertToNullableDecimal(object value)
|
|
576 {
|
|
577 return
|
|
578 value is Decimal? (Decimal?)value:
|
|
579 value == null || value is DBNull? null:
|
|
580 Convert.ToNullableDecimal(value);
|
|
581 }
|
|
582
|
|
583 public virtual Guid? ConvertToNullableGuid(object value)
|
|
584 {
|
|
585 return
|
|
586 value is Guid? (Guid?)value:
|
|
587 value == null || value is DBNull? null:
|
|
588 Convert.ToNullableGuid(value);
|
|
589 }
|
|
590
|
|
591 #endregion
|
|
592
|
|
593 #region SqlTypes
|
|
594
|
|
595 #if !SILVERLIGHT
|
|
596
|
|
597 public virtual SqlByte ConvertToSqlByte(object value)
|
|
598 {
|
|
599 return
|
|
600 value == null || value is DBNull? SqlByte.Null :
|
|
601 value is SqlByte? (SqlByte)value:
|
|
602 Convert.ToSqlByte(value);
|
|
603 }
|
|
604
|
|
605 public virtual SqlInt16 ConvertToSqlInt16(object value)
|
|
606 {
|
|
607 return
|
|
608 value == null || value is DBNull? SqlInt16.Null:
|
|
609 value is SqlInt16? (SqlInt16)value:
|
|
610 Convert.ToSqlInt16(value);
|
|
611 }
|
|
612
|
|
613 public virtual SqlInt32 ConvertToSqlInt32(object value)
|
|
614 {
|
|
615 return
|
|
616 value == null || value is DBNull? SqlInt32.Null:
|
|
617 value is SqlInt32? (SqlInt32)value:
|
|
618 Convert.ToSqlInt32(value);
|
|
619 }
|
|
620
|
|
621 public virtual SqlInt64 ConvertToSqlInt64(object value)
|
|
622 {
|
|
623 return
|
|
624 value == null || value is DBNull? SqlInt64.Null:
|
|
625 value is SqlInt64? (SqlInt64)value:
|
|
626 Convert.ToSqlInt64(value);
|
|
627 }
|
|
628
|
|
629 public virtual SqlSingle ConvertToSqlSingle(object value)
|
|
630 {
|
|
631 return
|
|
632 value == null || value is DBNull? SqlSingle.Null:
|
|
633 value is SqlSingle? (SqlSingle)value:
|
|
634 Convert.ToSqlSingle(value);
|
|
635 }
|
|
636
|
|
637 public virtual SqlBoolean ConvertToSqlBoolean(object value)
|
|
638 {
|
|
639 return
|
|
640 value == null || value is DBNull? SqlBoolean.Null:
|
|
641 value is SqlBoolean? (SqlBoolean)value:
|
|
642 Convert.ToSqlBoolean(value);
|
|
643 }
|
|
644
|
|
645 public virtual SqlDouble ConvertToSqlDouble(object value)
|
|
646 {
|
|
647 return
|
|
648 value == null || value is DBNull? SqlDouble.Null:
|
|
649 value is SqlDouble? (SqlDouble)value:
|
|
650 Convert.ToSqlDouble(value);
|
|
651 }
|
|
652
|
|
653 public virtual SqlDateTime ConvertToSqlDateTime(object value)
|
|
654 {
|
|
655 return
|
|
656 value == null || value is DBNull? SqlDateTime.Null:
|
|
657 value is SqlDateTime? (SqlDateTime)value:
|
|
658 Convert.ToSqlDateTime(value);
|
|
659 }
|
|
660
|
|
661 public virtual SqlDecimal ConvertToSqlDecimal(object value)
|
|
662 {
|
|
663 return
|
|
664 value == null || value is DBNull? SqlDecimal.Null:
|
|
665 value is SqlDecimal? (SqlDecimal)value:
|
|
666 value is SqlMoney? ((SqlMoney)value).ToSqlDecimal():
|
|
667 Convert.ToSqlDecimal(value);
|
|
668 }
|
|
669
|
|
670 public virtual SqlMoney ConvertToSqlMoney(object value)
|
|
671 {
|
|
672 return
|
|
673 value == null || value is DBNull? SqlMoney.Null:
|
|
674 value is SqlMoney? (SqlMoney)value:
|
|
675 value is SqlDecimal? ((SqlDecimal)value).ToSqlMoney():
|
|
676 Convert.ToSqlMoney(value);
|
|
677 }
|
|
678
|
|
679 public virtual SqlString ConvertToSqlString(object value)
|
|
680 {
|
|
681 return
|
|
682 value == null || value is DBNull? SqlString.Null:
|
|
683 value is SqlString? (SqlString)value:
|
|
684 Convert.ToSqlString(value);
|
|
685 }
|
|
686
|
|
687 public virtual SqlBinary ConvertToSqlBinary(object value)
|
|
688 {
|
|
689 return
|
|
690 value == null || value is DBNull? SqlBinary.Null:
|
|
691 value is SqlBinary? (SqlBinary)value:
|
|
692 Convert.ToSqlBinary(value);
|
|
693 }
|
|
694
|
|
695 public virtual SqlGuid ConvertToSqlGuid(object value)
|
|
696 {
|
|
697 return
|
|
698 value == null || value is DBNull? SqlGuid.Null:
|
|
699 value is SqlGuid? (SqlGuid)value:
|
|
700 Convert.ToSqlGuid(value);
|
|
701 }
|
|
702
|
|
703 public virtual SqlBytes ConvertToSqlBytes(object value)
|
|
704 {
|
|
705 return
|
|
706 value == null || value is DBNull? SqlBytes.Null:
|
|
707 value is SqlBytes? (SqlBytes)value:
|
|
708 Convert.ToSqlBytes(value);
|
|
709 }
|
|
710
|
|
711 public virtual SqlChars ConvertToSqlChars(object value)
|
|
712 {
|
|
713 return
|
|
714 value == null || value is DBNull? SqlChars.Null:
|
|
715 value is SqlChars? (SqlChars)value:
|
|
716 Convert.ToSqlChars(value);
|
|
717 }
|
|
718
|
|
719 public virtual SqlXml ConvertToSqlXml(object value)
|
|
720 {
|
|
721 return
|
|
722 value == null || value is DBNull? SqlXml.Null:
|
|
723 value is SqlXml? (SqlXml)value:
|
|
724 Convert.ToSqlXml(value);
|
|
725 }
|
|
726
|
|
727 #endif
|
|
728
|
|
729 #endregion
|
|
730
|
|
731 #region General case
|
|
732
|
|
733 public virtual T GetDefaultNullValue<T>()
|
|
734 {
|
|
735 switch (Type.GetTypeCode(typeof(T)))
|
|
736 {
|
|
737 case TypeCode.Boolean: return (T)(object)DefaultBooleanNullValue;
|
|
738 case TypeCode.Byte: return (T)(object)DefaultByteNullValue;
|
|
739 case TypeCode.Char: return (T)(object)DefaultCharNullValue;
|
|
740 case TypeCode.DateTime: return (T)(object)DefaultDateTimeNullValue;
|
|
741 case TypeCode.Decimal: return (T)(object)DefaultDecimalNullValue;
|
|
742 case TypeCode.Double: return (T)(object)DefaultDoubleNullValue;
|
|
743 case TypeCode.Int16: return (T)(object)DefaultInt16NullValue;
|
|
744 case TypeCode.Int32: return (T)(object)DefaultInt32NullValue;
|
|
745 case TypeCode.Int64: return (T)(object)DefaultInt64NullValue;
|
|
746 case TypeCode.SByte: return (T)(object)DefaultSByteNullValue;
|
|
747 case TypeCode.Single: return (T)(object)DefaultSingleNullValue;
|
|
748 case TypeCode.String: return (T)(object)DefaultStringNullValue;
|
|
749 case TypeCode.UInt16: return (T)(object)DefaultUInt16NullValue;
|
|
750 case TypeCode.UInt32: return (T)(object)DefaultUInt32NullValue;
|
|
751 case TypeCode.UInt64: return (T)(object)DefaultUInt64NullValue;
|
|
752 }
|
|
753
|
|
754 if (typeof(Guid) == typeof(T)) return (T)(object)DefaultGuidNullValue;
|
|
755 if (typeof(Stream) == typeof(T)) return (T)(object)DefaultStreamNullValue;
|
|
756 #if !SILVERLIGHT
|
|
757 if (typeof(XmlReader) == typeof(T)) return (T)(object)DefaultXmlReaderNullValue;
|
|
758 if (typeof(XmlDocument) == typeof(T)) return (T)(object)DefaultXmlDocumentNullValue;
|
|
759 if (typeof(XElement) == typeof(T)) return (T)(object)DefaultXElementNullValue;
|
|
760 #endif
|
|
761 if (typeof(DateTimeOffset) == typeof(T)) return (T)(object)DefaultDateTimeOffsetNullValue;
|
|
762
|
|
763 return default(T);
|
|
764 }
|
|
765
|
|
766 public virtual T ConvertTo<T,TP>(TP value)
|
|
767 {
|
|
768 return Equals(value, default(TP))?
|
|
769 GetDefaultNullValue<T>():
|
|
770 Convert<T,TP>.From(value);
|
|
771 }
|
|
772
|
|
773 public virtual object ConvertChangeType(object value, Type conversionType)
|
|
774 {
|
|
775 return ConvertChangeType(value, conversionType, TypeHelper.IsNullable(conversionType));
|
|
776 }
|
|
777
|
|
778 public virtual object ConvertChangeType(object value, Type conversionType, bool isNullable)
|
|
779 {
|
|
780 if (conversionType.IsArray)
|
|
781 {
|
|
782 if (null == value)
|
|
783 return null;
|
|
784
|
|
785 Type srcType = value.GetType();
|
|
786
|
|
787 if (srcType == conversionType)
|
|
788 return value;
|
|
789
|
|
790 if (srcType.IsArray)
|
|
791 {
|
|
792 Type srcElementType = srcType.GetElementType();
|
|
793 Type dstElementType = conversionType.GetElementType();
|
|
794
|
|
795 if (srcElementType.IsArray != dstElementType.IsArray
|
|
796 || (srcElementType.IsArray &&
|
|
797 srcElementType.GetArrayRank() != dstElementType.GetArrayRank()))
|
|
798 {
|
|
799 throw new InvalidCastException(string.Format(
|
|
800 Resources.MappingSchema_IncompatibleArrayTypes,
|
|
801 srcType.FullName, conversionType.FullName));
|
|
802 }
|
|
803
|
|
804 Array srcArray = (Array)value;
|
|
805 Array dstArray;
|
|
806
|
|
807 int rank = srcArray.Rank;
|
|
808
|
|
809 if (rank == 1 && 0 == srcArray.GetLowerBound(0))
|
|
810 {
|
|
811 int arrayLength = srcArray.Length;
|
|
812
|
|
813 dstArray = Array.CreateInstance(dstElementType, arrayLength);
|
|
814
|
|
815 // Int32 is assignable from UInt32, SByte from Byte and so on.
|
|
816 //
|
|
817 if (dstElementType.IsAssignableFrom(srcElementType))
|
|
818 Array.Copy(srcArray, dstArray, arrayLength);
|
|
819 else
|
|
820 for (int i = 0; i < arrayLength; ++i)
|
|
821 dstArray.SetValue(ConvertChangeType(srcArray.GetValue(i), dstElementType, isNullable), i);
|
|
822 }
|
|
823 else
|
|
824 {
|
|
825 #if SILVERLIGHT
|
|
826 throw new InvalidOperationException();
|
|
827 #else
|
|
828 var arrayLength = 1;
|
|
829 var dimensions = new int[rank];
|
|
830 var indices = new int[rank];
|
|
831 var lbounds = new int[rank];
|
|
832
|
|
833 for (int i = 0; i < rank; ++i)
|
|
834 {
|
|
835 arrayLength *= (dimensions[i] = srcArray.GetLength(i));
|
|
836 lbounds[i] = srcArray.GetLowerBound(i);
|
|
837 }
|
|
838
|
|
839 dstArray = Array.CreateInstance(dstElementType, dimensions, lbounds);
|
|
840
|
|
841 for (int i = 0; i < arrayLength; ++i)
|
|
842 {
|
|
843 var index = i;
|
|
844
|
|
845 for (var j = rank - 1; j >= 0; --j)
|
|
846 {
|
|
847 indices[j] = index % dimensions[j] + lbounds[j];
|
|
848 index /= dimensions[j];
|
|
849 }
|
|
850
|
|
851 dstArray.SetValue(ConvertChangeType(srcArray.GetValue(indices), dstElementType, isNullable), indices);
|
|
852 }
|
|
853
|
|
854 #endif
|
|
855 }
|
|
856
|
|
857 return dstArray;
|
|
858 }
|
|
859 }
|
|
860 else if (conversionType.IsEnum)
|
|
861 {
|
|
862 return MapValueToEnum(value, conversionType);
|
|
863 }
|
|
864
|
|
865 if (isNullable)
|
|
866 {
|
|
867 if (TypeHelper.IsNullable(conversionType))
|
|
868 {
|
|
869 // Return a null reference or boxed not null value.
|
|
870 //
|
|
871 return value == null || value is DBNull? null:
|
|
872 ConvertChangeType(value, conversionType.GetGenericArguments()[0]);
|
|
873 }
|
|
874
|
|
875 Type type = conversionType.IsEnum? Enum.GetUnderlyingType(conversionType): conversionType;
|
|
876
|
|
877 switch (Type.GetTypeCode(type))
|
|
878 {
|
|
879 case TypeCode.Boolean: return ConvertToNullableBoolean (value);
|
|
880 case TypeCode.Byte: return ConvertToNullableByte (value);
|
|
881 case TypeCode.Char: return ConvertToNullableChar (value);
|
|
882 case TypeCode.DateTime: return ConvertToNullableDateTime(value);
|
|
883 case TypeCode.Decimal: return ConvertToNullableDecimal (value);
|
|
884 case TypeCode.Double: return ConvertToNullableDouble (value);
|
|
885 case TypeCode.Int16: return ConvertToNullableInt16 (value);
|
|
886 case TypeCode.Int32: return ConvertToNullableInt32 (value);
|
|
887 case TypeCode.Int64: return ConvertToNullableInt64 (value);
|
|
888 case TypeCode.SByte: return ConvertToNullableSByte (value);
|
|
889 case TypeCode.Single: return ConvertToNullableSingle (value);
|
|
890 case TypeCode.UInt16: return ConvertToNullableUInt16 (value);
|
|
891 case TypeCode.UInt32: return ConvertToNullableUInt32 (value);
|
|
892 case TypeCode.UInt64: return ConvertToNullableUInt64 (value);
|
|
893 }
|
|
894
|
|
895 if (typeof(Guid) == conversionType) return ConvertToNullableGuid(value);
|
|
896 if (typeof(DateTimeOffset) == conversionType) return ConvertToNullableDateTimeOffset(value);
|
|
897 if (typeof(TimeSpan) == conversionType) return ConvertToNullableTimeSpan(value);
|
|
898 }
|
|
899
|
|
900 switch (Type.GetTypeCode(conversionType))
|
|
901 {
|
|
902 case TypeCode.Boolean: return ConvertToBoolean (value);
|
|
903 case TypeCode.Byte: return ConvertToByte (value);
|
|
904 case TypeCode.Char: return ConvertToChar (value);
|
|
905 case TypeCode.DateTime: return ConvertToDateTime(value);
|
|
906 case TypeCode.Decimal: return ConvertToDecimal (value);
|
|
907 case TypeCode.Double: return ConvertToDouble (value);
|
|
908 case TypeCode.Int16: return ConvertToInt16 (value);
|
|
909 case TypeCode.Int32: return ConvertToInt32 (value);
|
|
910 case TypeCode.Int64: return ConvertToInt64 (value);
|
|
911 case TypeCode.SByte: return ConvertToSByte (value);
|
|
912 case TypeCode.Single: return ConvertToSingle (value);
|
|
913 case TypeCode.String: return ConvertToString (value);
|
|
914 case TypeCode.UInt16: return ConvertToUInt16 (value);
|
|
915 case TypeCode.UInt32: return ConvertToUInt32 (value);
|
|
916 case TypeCode.UInt64: return ConvertToUInt64 (value);
|
|
917 }
|
|
918
|
|
919 if (typeof(Guid) == conversionType) return ConvertToGuid (value);
|
|
920 if (typeof(Stream) == conversionType) return ConvertToStream (value);
|
|
921 #if !SILVERLIGHT
|
|
922 if (typeof(XmlReader) == conversionType) return ConvertToXmlReader (value);
|
|
923 if (typeof(XmlDocument) == conversionType) return ConvertToXmlDocument (value);
|
|
924 if (typeof(XElement) == conversionType) return ConvertToXElement (value);
|
|
925 #endif
|
|
926 if (typeof(byte[]) == conversionType) return ConvertToByteArray (value);
|
|
927 if (typeof(Binary) == conversionType) return ConvertToLinqBinary (value);
|
|
928 if (typeof(DateTimeOffset) == conversionType) return ConvertToDateTimeOffset(value);
|
|
929 if (typeof(char[]) == conversionType) return ConvertToCharArray (value);
|
|
930 if (typeof(TimeSpan) == conversionType) return ConvertToTimeSpan (value);
|
|
931
|
|
932 #if !SILVERLIGHT
|
|
933
|
|
934 if (typeof(SqlInt32) == conversionType) return ConvertToSqlInt32 (value);
|
|
935 if (typeof(SqlString) == conversionType) return ConvertToSqlString (value);
|
|
936 if (typeof(SqlDecimal) == conversionType) return ConvertToSqlDecimal (value);
|
|
937 if (typeof(SqlDateTime) == conversionType) return ConvertToSqlDateTime (value);
|
|
938 if (typeof(SqlBoolean) == conversionType) return ConvertToSqlBoolean (value);
|
|
939 if (typeof(SqlMoney) == conversionType) return ConvertToSqlMoney (value);
|
|
940 if (typeof(SqlGuid) == conversionType) return ConvertToSqlGuid (value);
|
|
941 if (typeof(SqlDouble) == conversionType) return ConvertToSqlDouble (value);
|
|
942 if (typeof(SqlByte) == conversionType) return ConvertToSqlByte (value);
|
|
943 if (typeof(SqlInt16) == conversionType) return ConvertToSqlInt16 (value);
|
|
944 if (typeof(SqlInt64) == conversionType) return ConvertToSqlInt64 (value);
|
|
945 if (typeof(SqlSingle) == conversionType) return ConvertToSqlSingle (value);
|
|
946 if (typeof(SqlBinary) == conversionType) return ConvertToSqlBinary (value);
|
|
947 if (typeof(SqlBytes) == conversionType) return ConvertToSqlBytes (value);
|
|
948 if (typeof(SqlChars) == conversionType) return ConvertToSqlChars (value);
|
|
949 if (typeof(SqlXml) == conversionType) return ConvertToSqlXml (value);
|
|
950
|
|
951 #endif
|
|
952
|
|
953 return System.Convert.ChangeType(value, conversionType, Thread.CurrentThread.CurrentCulture);
|
|
954 }
|
|
955
|
|
956 #endregion
|
|
957
|
|
958 #endregion
|
|
959
|
|
960 #region Factory Members
|
|
961
|
|
962 public virtual DataReaderMapper CreateDataReaderMapper(IDataReader dataReader)
|
|
963 {
|
|
964 return new DataReaderMapper(this, dataReader);
|
|
965 }
|
|
966
|
|
967 public virtual DataReaderListMapper CreateDataReaderListMapper(IDataReader reader)
|
|
968 {
|
|
969 return new DataReaderListMapper(CreateDataReaderMapper(reader));
|
|
970 }
|
|
971
|
|
972 public virtual DataReaderMapper CreateDataReaderMapper(
|
|
973 IDataReader dataReader,
|
|
974 NameOrIndexParameter nameOrIndex)
|
|
975 {
|
|
976 return new ScalarDataReaderMapper(this, dataReader, nameOrIndex);
|
|
977 }
|
|
978
|
|
979 public virtual DataReaderListMapper CreateDataReaderListMapper(
|
|
980 IDataReader reader,
|
|
981 NameOrIndexParameter nameOrIndex)
|
|
982 {
|
|
983 return new DataReaderListMapper(CreateDataReaderMapper(reader, nameOrIndex));
|
|
984 }
|
|
985
|
|
986 #if !SILVERLIGHT
|
|
987
|
|
988 public virtual DataRowMapper CreateDataRowMapper(
|
|
989 DataRow row,
|
|
990 DataRowVersion version)
|
|
991 {
|
|
992 return new DataRowMapper(row, version);
|
|
993 }
|
|
994
|
|
995 public virtual DataTableMapper CreateDataTableMapper(
|
|
996 DataTable dataTable,
|
|
997 DataRowVersion version)
|
|
998 {
|
|
999 return new DataTableMapper(dataTable, CreateDataRowMapper(null, version));
|
|
1000 }
|
|
1001
|
|
1002 #endif
|
|
1003
|
|
1004 public virtual DictionaryMapper CreateDictionaryMapper(IDictionary dictionary)
|
|
1005 {
|
|
1006 return new DictionaryMapper(dictionary);
|
|
1007 }
|
|
1008
|
|
1009 public virtual DictionaryListMapper CreateDictionaryListMapper(
|
|
1010 IDictionary dic,
|
|
1011 NameOrIndexParameter keyFieldNameOrIndex,
|
|
1012 ObjectMapper objectMapper)
|
|
1013 {
|
|
1014 return new DictionaryListMapper(dic, keyFieldNameOrIndex, objectMapper);
|
|
1015 }
|
|
1016
|
|
1017 public virtual DictionaryIndexListMapper CreateDictionaryListMapper(
|
|
1018 IDictionary dic,
|
|
1019 MapIndex index,
|
|
1020 ObjectMapper objectMapper)
|
|
1021 {
|
|
1022 return new DictionaryIndexListMapper(dic, index, objectMapper);
|
|
1023 }
|
|
1024
|
|
1025 public virtual DictionaryListMapper<TK,T> CreateDictionaryListMapper<TK,T>(
|
|
1026 IDictionary<TK,T> dic,
|
|
1027 NameOrIndexParameter keyFieldNameOrIndex,
|
|
1028 ObjectMapper objectMapper)
|
|
1029 {
|
|
1030 return new DictionaryListMapper<TK,T>(dic, keyFieldNameOrIndex, objectMapper);
|
|
1031 }
|
|
1032
|
|
1033 public virtual DictionaryIndexListMapper<T> CreateDictionaryListMapper<T>(
|
|
1034 IDictionary<CompoundValue,T> dic,
|
|
1035 MapIndex index,
|
|
1036 ObjectMapper objectMapper)
|
|
1037 {
|
|
1038 return new DictionaryIndexListMapper<T>(dic, index, objectMapper);
|
|
1039 }
|
|
1040
|
|
1041 public virtual EnumeratorMapper CreateEnumeratorMapper(IEnumerator enumerator)
|
|
1042 {
|
|
1043 return new EnumeratorMapper(enumerator);
|
|
1044 }
|
|
1045
|
|
1046 public virtual ObjectListMapper CreateObjectListMapper(IList list, ObjectMapper objectMapper)
|
|
1047 {
|
|
1048 return new ObjectListMapper(list, objectMapper);
|
|
1049 }
|
|
1050
|
|
1051 public virtual ScalarListMapper CreateScalarListMapper(IList list, Type type)
|
|
1052 {
|
|
1053 return new ScalarListMapper(list, type);
|
|
1054 }
|
|
1055
|
|
1056 public virtual SimpleDestinationListMapper CreateScalarDestinationListMapper(IList list, Type type)
|
|
1057 {
|
|
1058 return new SimpleDestinationListMapper(CreateScalarListMapper(list, type));
|
|
1059 }
|
|
1060
|
|
1061 public virtual SimpleSourceListMapper CreateScalarSourceListMapper(IList list, Type type)
|
|
1062 {
|
|
1063 return new SimpleSourceListMapper(CreateScalarListMapper(list, type));
|
|
1064 }
|
|
1065
|
|
1066 public virtual ScalarListMapper<T> CreateScalarListMapper<T>(IList<T> list)
|
|
1067 {
|
|
1068 return new ScalarListMapper<T>(this, list);
|
|
1069 }
|
|
1070
|
|
1071 public virtual SimpleDestinationListMapper CreateScalarDestinationListMapper<T>(IList<T> list)
|
|
1072 {
|
|
1073 return new SimpleDestinationListMapper(CreateScalarListMapper<T>(list));
|
|
1074 }
|
|
1075
|
|
1076 #endregion
|
|
1077
|
|
1078 #region GetNullValue
|
|
1079
|
|
1080 public virtual object GetNullValue(Type type)
|
|
1081 {
|
|
1082 return TypeAccessor.GetNullValue(type);
|
|
1083 }
|
|
1084
|
|
1085 public virtual bool IsNull(object value)
|
|
1086 {
|
|
1087 return TypeAccessor.IsNull(value);
|
|
1088 }
|
|
1089
|
|
1090 #endregion
|
|
1091
|
|
1092 #region GetMapValues
|
|
1093
|
|
1094 private readonly Dictionary<Type,MapValue[]> _mapValues = new Dictionary<Type,MapValue[]>();
|
|
1095
|
|
1096 public virtual MapValue[] GetMapValues([JetBrains.Annotations.NotNull] Type type)
|
|
1097 {
|
|
1098 if (type == null) throw new ArgumentNullException("type");
|
|
1099
|
|
1100 lock (_mapValues)
|
|
1101 {
|
|
1102 MapValue[] mapValues;
|
|
1103
|
|
1104 if (_mapValues.TryGetValue(type, out mapValues))
|
|
1105 return mapValues;
|
|
1106
|
|
1107 var typeExt = TypeExtension.GetTypeExtension(type, Extensions);
|
|
1108 bool isSet;
|
|
1109
|
|
1110 mapValues = MetadataProvider.GetMapValues(typeExt, type, out isSet);
|
|
1111
|
|
1112 _mapValues.Add(type, mapValues);
|
|
1113
|
|
1114 return mapValues;
|
|
1115 }
|
|
1116 }
|
|
1117
|
|
1118 private readonly Dictionary<MemberAccessor, MapValue[]> _memberMapValues = new Dictionary<MemberAccessor, MapValue[]>();
|
|
1119
|
|
1120 private Type GetMapValueType(MapValue[] mapValues)
|
|
1121 {
|
|
1122 if (mapValues != null)
|
|
1123 {
|
|
1124 var value = mapValues.SelectMany(mv => mv.MapValues).FirstOrDefault();
|
|
1125 if (value != null)
|
|
1126 {
|
|
1127 return value.GetType();
|
|
1128 }
|
|
1129 }
|
|
1130 return null;
|
|
1131 }
|
|
1132
|
|
1133 public virtual MapValue[] GetMapValues([JetBrains.Annotations.NotNull] MemberAccessor memberAccessor)
|
|
1134 {
|
|
1135 if (memberAccessor == null) throw new ArgumentNullException("memberAccessor");
|
|
1136
|
|
1137 lock (_memberMapValues)
|
|
1138 {
|
|
1139 MapValue[] mapValues;
|
|
1140
|
|
1141 if (_memberMapValues.TryGetValue(memberAccessor, out mapValues))
|
|
1142 return mapValues;
|
|
1143
|
|
1144 var typeExt = TypeExtension.GetTypeExtension(memberAccessor.Type, Extensions);
|
|
1145 bool isSet;
|
|
1146
|
|
1147 mapValues = MetadataProvider.GetMapValues(typeExt, memberAccessor, out isSet);
|
|
1148
|
|
1149 _memberMapValues.Add(memberAccessor, mapValues);
|
|
1150
|
|
1151 return mapValues;
|
|
1152 }
|
|
1153 }
|
|
1154
|
|
1155 #endregion
|
|
1156
|
|
1157 #region GetDefaultValue
|
|
1158
|
|
1159 private readonly Dictionary<Type,object> _defaultValues = new Dictionary<Type,object>();
|
|
1160
|
|
1161 public virtual object GetDefaultValue([JetBrains.Annotations.NotNull] Type type)
|
|
1162 {
|
|
1163 if (type == null) throw new ArgumentNullException("type");
|
|
1164
|
|
1165 lock (_defaultValues)
|
|
1166 {
|
|
1167 object defaultValue;
|
|
1168
|
|
1169 if (_defaultValues.TryGetValue(type, out defaultValue))
|
|
1170 return defaultValue;
|
|
1171
|
|
1172 var typeExt = TypeExtension.GetTypeExtension(type, Extensions);
|
|
1173 bool isSet;
|
|
1174
|
|
1175 defaultValue = MetadataProvider.GetDefaultValue(this, typeExt, type, out isSet);
|
|
1176
|
|
1177 _defaultValues.Add(type, defaultValue = TypeExtension.ChangeType(defaultValue, type));
|
|
1178
|
|
1179 return defaultValue;
|
|
1180 }
|
|
1181 }
|
|
1182
|
|
1183 #endregion
|
|
1184
|
|
1185 #region GetDataSource, GetDataDestination
|
|
1186
|
|
1187 [CLSCompliant(false)]
|
|
1188 public virtual IMapDataSource GetDataSource(object obj)
|
|
1189 {
|
|
1190 if (obj == null) throw new ArgumentNullException("obj");
|
|
1191
|
|
1192 if (obj is IMapDataSource)
|
|
1193 return (IMapDataSource)obj;
|
|
1194
|
|
1195 if (obj is IDataReader)
|
|
1196 return CreateDataReaderMapper((IDataReader)obj);
|
|
1197
|
|
1198 #if !SILVERLIGHT
|
|
1199
|
|
1200 if (obj is DataRow)
|
|
1201 return CreateDataRowMapper((DataRow)obj, DataRowVersion.Default);
|
|
1202
|
|
1203 if (obj is DataRowView)
|
|
1204 return CreateDataRowMapper(
|
|
1205 ((DataRowView)obj).Row,
|
|
1206 ((DataRowView)obj).RowVersion);
|
|
1207
|
|
1208 if (obj is DataTable)
|
|
1209 return CreateDataRowMapper(((DataTable)(obj)).Rows[0], DataRowVersion.Default);
|
|
1210
|
|
1211 #endif
|
|
1212
|
|
1213 if (obj is IDictionary)
|
|
1214 return CreateDictionaryMapper((IDictionary)obj);
|
|
1215
|
|
1216 return GetObjectMapper(obj.GetType());
|
|
1217 }
|
|
1218
|
|
1219 [CLSCompliant(false)]
|
|
1220 public virtual IMapDataDestination GetDataDestination(object obj)
|
|
1221 {
|
|
1222 if (obj == null) throw new ArgumentNullException("obj");
|
|
1223
|
|
1224 if (obj is IMapDataDestination)
|
|
1225 return (IMapDataDestination)obj;
|
|
1226
|
|
1227 #if !SILVERLIGHT
|
|
1228
|
|
1229 if (obj is DataRow)
|
|
1230 return CreateDataRowMapper((DataRow)obj, DataRowVersion.Default);
|
|
1231
|
|
1232 if (obj is DataRowView)
|
|
1233 return CreateDataRowMapper(
|
|
1234 ((DataRowView)obj).Row,
|
|
1235 ((DataRowView)obj).RowVersion);
|
|
1236
|
|
1237 if (obj is DataTable)
|
|
1238 {
|
|
1239 DataTable dt = obj as DataTable;
|
|
1240 DataRow dr = dt.NewRow();
|
|
1241
|
|
1242 dt.Rows.Add(dr);
|
|
1243
|
|
1244 return CreateDataRowMapper(dr, DataRowVersion.Default);
|
|
1245 }
|
|
1246
|
|
1247 #endif
|
|
1248
|
|
1249 if (obj is IDictionary)
|
|
1250 return CreateDictionaryMapper((IDictionary)obj);
|
|
1251
|
|
1252 return GetObjectMapper(obj.GetType());
|
|
1253 }
|
|
1254
|
|
1255 [CLSCompliant(false)]
|
|
1256 public virtual IMapDataSourceList GetDataSourceList(object obj)
|
|
1257 {
|
|
1258 if (obj == null) throw new ArgumentNullException("obj");
|
|
1259
|
|
1260 if (obj is IMapDataSourceList)
|
|
1261 return (IMapDataSourceList)obj;
|
|
1262
|
|
1263 if (obj is IDataReader)
|
|
1264 return CreateDataReaderListMapper((IDataReader)obj);
|
|
1265
|
|
1266 Type type = obj.GetType().GetElementType();
|
|
1267
|
|
1268 return TypeHelper.IsScalar(type)?
|
|
1269 (IMapDataSourceList)CreateScalarSourceListMapper((IList)obj, type):
|
|
1270 CreateObjectListMapper((IList)obj, CreateObjectMapper(type));
|
|
1271 }
|
|
1272
|
|
1273 [CLSCompliant(false)]
|
|
1274 public virtual IMapDataDestinationList GetDataDestinationList(object obj)
|
|
1275 {
|
|
1276 if (obj == null) throw new ArgumentNullException("obj");
|
|
1277
|
|
1278 if (obj is IMapDataDestinationList)
|
|
1279 return (IMapDataDestinationList)obj;
|
|
1280
|
|
1281 Type type = obj.GetType().GetElementType();
|
|
1282
|
|
1283 return TypeHelper.IsScalar(type)?
|
|
1284 (IMapDataDestinationList)CreateScalarDestinationListMapper((IList)obj, type):
|
|
1285 CreateObjectListMapper((IList)obj, CreateObjectMapper(type));
|
|
1286 }
|
|
1287
|
|
1288 #endregion
|
|
1289
|
|
1290 #region ValueMapper
|
|
1291
|
|
1292 [CLSCompliant(false)]
|
|
1293 public virtual IValueMapper DefaultValueMapper
|
|
1294 {
|
|
1295 get { return ValueMapping.DefaultMapper; }
|
|
1296 }
|
|
1297
|
|
1298 internal readonly Dictionary<Type,IValueMapper> SameTypeMappers = new Dictionary<Type,IValueMapper>();
|
|
1299 internal readonly Dictionary<KeyValue,IValueMapper> DifferentTypeMappers = new Dictionary<KeyValue,IValueMapper>();
|
|
1300
|
|
1301 [CLSCompliant(false)]
|
|
1302 public void SetValueMapper(
|
|
1303 Type sourceType,
|
|
1304 Type destType,
|
|
1305 IValueMapper mapper)
|
|
1306 {
|
|
1307 if (sourceType == null) sourceType = typeof(object);
|
|
1308 if (destType == null) destType = typeof(object);
|
|
1309
|
|
1310 if (sourceType == destType)
|
|
1311 {
|
|
1312 lock (SameTypeMappers)
|
|
1313 {
|
|
1314 if (mapper == null)
|
|
1315 SameTypeMappers.Remove(sourceType);
|
|
1316 else if (SameTypeMappers.ContainsKey(sourceType))
|
|
1317 SameTypeMappers[sourceType] = mapper;
|
|
1318 else
|
|
1319 SameTypeMappers.Add(sourceType, mapper);
|
|
1320 }
|
|
1321 }
|
|
1322 else
|
|
1323 {
|
|
1324 KeyValue key = new KeyValue(sourceType, destType);
|
|
1325
|
|
1326 lock (DifferentTypeMappers)
|
|
1327 {
|
|
1328 if (mapper == null)
|
|
1329 DifferentTypeMappers.Remove(key);
|
|
1330 else if (DifferentTypeMappers.ContainsKey(key))
|
|
1331 DifferentTypeMappers[key] = mapper;
|
|
1332 else
|
|
1333 DifferentTypeMappers.Add(key, mapper);
|
|
1334 }
|
|
1335 }
|
|
1336 }
|
|
1337
|
|
1338 [CLSCompliant(false)]
|
|
1339 protected internal virtual IValueMapper GetValueMapper(
|
|
1340 Type sourceType,
|
|
1341 Type destType)
|
|
1342 {
|
|
1343 return ValueMapping.GetMapper(sourceType, destType);
|
|
1344 }
|
|
1345
|
|
1346 [CLSCompliant(false)]
|
|
1347 internal protected IValueMapper[] GetValueMappers(
|
|
1348 IMapDataSource source,
|
|
1349 IMapDataDestination dest,
|
|
1350 int[] index)
|
|
1351 {
|
|
1352 IValueMapper[] mappers = new IValueMapper[index.Length];
|
|
1353
|
|
1354 for (int i = 0; i < index.Length; i++)
|
|
1355 {
|
|
1356 int n = index[i];
|
|
1357
|
|
1358 if (n < 0)
|
|
1359 continue;
|
|
1360
|
|
1361 if (!source.SupportsTypedValues(i) || !dest.SupportsTypedValues(n))
|
|
1362 {
|
|
1363 mappers[i] = DefaultValueMapper;
|
|
1364 continue;
|
|
1365 }
|
|
1366
|
|
1367 Type sourceType = source.GetFieldType(i);
|
|
1368 Type destType = dest. GetFieldType(n);
|
|
1369
|
|
1370 if (sourceType == null) sourceType = typeof(object);
|
|
1371 if (destType == null) destType = typeof(object);
|
|
1372
|
|
1373 IValueMapper t;
|
|
1374
|
|
1375 if (sourceType == destType)
|
|
1376 {
|
|
1377 lock (SameTypeMappers)
|
|
1378 if (!SameTypeMappers.TryGetValue(sourceType, out t))
|
|
1379 SameTypeMappers.Add(sourceType, t = GetValueMapper(sourceType, destType));
|
|
1380 }
|
|
1381 else
|
|
1382 {
|
|
1383 var key = new KeyValue(sourceType, destType);
|
|
1384
|
|
1385 lock (DifferentTypeMappers)
|
|
1386 if (!DifferentTypeMappers.TryGetValue(key, out t))
|
|
1387 DifferentTypeMappers[key] = t = GetValueMapper(sourceType, destType);
|
|
1388 }
|
|
1389
|
|
1390 mappers[i] = t;
|
|
1391 }
|
|
1392
|
|
1393 return mappers;
|
|
1394 }
|
|
1395
|
|
1396 #endregion
|
|
1397
|
|
1398 #region Base Mapping
|
|
1399
|
|
1400 [CLSCompliant(false)]
|
|
1401 internal protected static int[] GetIndex(
|
|
1402 IMapDataSource source,
|
|
1403 IMapDataDestination dest)
|
|
1404 {
|
|
1405 int count = source.Count;
|
|
1406 int[] index = new int[count];
|
|
1407
|
|
1408 for (int i = 0; i < count; i++)
|
|
1409 index[i] = dest.GetOrdinal(source.GetName(i));
|
|
1410
|
|
1411 return index;
|
|
1412 }
|
|
1413
|
|
1414 [CLSCompliant(false), Obsolete]
|
|
1415 protected static void MapInternal(
|
|
1416 IMapDataSource source, object sourceObject,
|
|
1417 IMapDataDestination dest, object destObject,
|
|
1418 int[] index)
|
|
1419 {
|
|
1420 for (int i = 0; i < index.Length; i++)
|
|
1421 {
|
|
1422 int n = index[i];
|
|
1423
|
|
1424 if (n >= 0)
|
|
1425 dest.SetValue(destObject, n, source.GetValue(sourceObject, i));
|
|
1426 }
|
|
1427 }
|
|
1428
|
|
1429 [CLSCompliant(false)]
|
|
1430 internal protected static void MapInternal(
|
|
1431 IMapDataSource source, object sourceObject,
|
|
1432 IMapDataDestination dest, object destObject,
|
|
1433 int[] index,
|
|
1434 IValueMapper[] mappers)
|
|
1435 {
|
|
1436 for (int i = 0; i < index.Length; i++)
|
|
1437 {
|
|
1438 int n = index[i];
|
|
1439
|
|
1440 if (n >= 0)
|
|
1441 mappers[i].Map(source, sourceObject, i, dest, destObject, n);
|
|
1442 }
|
|
1443 }
|
|
1444
|
|
1445 [CLSCompliant(false)]
|
|
1446 protected virtual void MapInternal(
|
|
1447 InitContext initContext,
|
|
1448 IMapDataSource source, object sourceObject,
|
|
1449 IMapDataDestination dest, object destObject,
|
|
1450 params object[] parameters)
|
|
1451 {
|
|
1452 ISupportMapping smSource = sourceObject as ISupportMapping;
|
|
1453 ISupportMapping smDest = destObject as ISupportMapping;
|
|
1454
|
|
1455 if (smSource != null)
|
|
1456 {
|
|
1457 if (initContext == null)
|
|
1458 {
|
|
1459 initContext = new InitContext();
|
|
1460
|
|
1461 initContext.MappingSchema = this;
|
|
1462 initContext.DataSource = source;
|
|
1463 initContext.SourceObject = sourceObject;
|
|
1464 initContext.ObjectMapper = dest as ObjectMapper;
|
|
1465 initContext.Parameters = parameters;
|
|
1466 }
|
|
1467
|
|
1468 initContext.IsSource = true;
|
|
1469 smSource.BeginMapping(initContext);
|
|
1470 initContext.IsSource = false;
|
|
1471
|
|
1472 if (initContext.StopMapping)
|
|
1473 return;
|
|
1474 }
|
|
1475
|
|
1476 if (smDest != null)
|
|
1477 {
|
|
1478 if (initContext == null)
|
|
1479 {
|
|
1480 initContext = new InitContext();
|
|
1481
|
|
1482 initContext.MappingSchema = this;
|
|
1483 initContext.DataSource = source;
|
|
1484 initContext.SourceObject = sourceObject;
|
|
1485 initContext.ObjectMapper = dest as ObjectMapper;
|
|
1486 initContext.Parameters = parameters;
|
|
1487 }
|
|
1488
|
|
1489 smDest.BeginMapping(initContext);
|
|
1490
|
|
1491 if (initContext.StopMapping)
|
|
1492 return;
|
|
1493
|
|
1494 if (dest != initContext.ObjectMapper && initContext.ObjectMapper != null)
|
|
1495 dest = initContext.ObjectMapper;
|
|
1496 }
|
|
1497
|
|
1498 int[] index = GetIndex (source, dest);
|
|
1499 IValueMapper[] mappers = GetValueMappers(source, dest, index);
|
|
1500
|
|
1501 MapInternal(source, sourceObject, dest, destObject, index, mappers);
|
|
1502
|
|
1503 if (smDest != null)
|
|
1504 smDest.EndMapping(initContext);
|
|
1505
|
|
1506 if (smSource != null)
|
|
1507 {
|
|
1508 initContext.IsSource = true;
|
|
1509 smSource.EndMapping(initContext);
|
|
1510 initContext.IsSource = false;
|
|
1511 }
|
|
1512 }
|
|
1513
|
|
1514 protected virtual object MapInternal(InitContext initContext)
|
|
1515 {
|
|
1516 object dest = initContext.ObjectMapper.CreateInstance(initContext);
|
|
1517
|
|
1518 if (initContext.StopMapping == false)
|
|
1519 {
|
|
1520 MapInternal(initContext,
|
|
1521 initContext.DataSource, initContext.SourceObject,
|
|
1522 initContext.ObjectMapper, dest,
|
|
1523 initContext.Parameters);
|
|
1524 }
|
|
1525
|
|
1526 return dest;
|
|
1527 }
|
|
1528
|
|
1529 [CLSCompliant(false)]
|
|
1530 public void MapSourceToDestination(
|
|
1531 IMapDataSource source, object sourceObject,
|
|
1532 IMapDataDestination dest, object destObject,
|
|
1533 params object[] parameters)
|
|
1534 {
|
|
1535 MapInternal(null, source, sourceObject, dest, destObject, parameters);
|
|
1536 }
|
|
1537
|
|
1538 public void MapSourceToDestination(
|
|
1539 object sourceObject,
|
|
1540 object destObject,
|
|
1541 params object[] parameters)
|
|
1542 {
|
|
1543 IMapDataSource source = GetDataSource (sourceObject);
|
|
1544 IMapDataDestination dest = GetDataDestination(destObject);
|
|
1545
|
|
1546 MapInternal(null, source, sourceObject, dest, destObject, parameters);
|
|
1547 }
|
|
1548
|
|
1549 private static readonly ObjectMapper _nullMapper = new ObjectMapper();
|
|
1550
|
|
1551 private class MapInfo
|
|
1552 {
|
|
1553 public int[] Index;
|
|
1554 public IValueMapper[] Mappers;
|
|
1555 }
|
|
1556
|
|
1557 [CLSCompliant(false)]
|
|
1558 public virtual void MapSourceListToDestinationList(
|
|
1559 IMapDataSourceList dataSourceList,
|
|
1560 IMapDataDestinationList dataDestinationList,
|
|
1561 params object[] parameters)
|
|
1562 {
|
|
1563 if (dataSourceList == null) throw new ArgumentNullException("dataSourceList");
|
|
1564 if (dataDestinationList == null) throw new ArgumentNullException("dataDestinationList");
|
|
1565
|
|
1566 Dictionary<ObjectMapper,MapInfo> infos = new Dictionary<ObjectMapper,MapInfo>();
|
|
1567
|
|
1568 InitContext ctx = new InitContext();
|
|
1569
|
|
1570 ctx.MappingSchema = this;
|
|
1571 ctx.Parameters = parameters;
|
|
1572
|
|
1573 dataSourceList. InitMapping(ctx); if (ctx.StopMapping) return;
|
|
1574 dataDestinationList.InitMapping(ctx); if (ctx.StopMapping) return;
|
|
1575
|
|
1576 int[] index = null;
|
|
1577 IValueMapper[] mappers = null;
|
|
1578 ObjectMapper current = _nullMapper;
|
|
1579 IMapDataDestination dest = dataDestinationList.GetDataDestination(ctx);
|
|
1580 ObjectMapper om = dest as ObjectMapper;
|
|
1581
|
|
1582 while (dataSourceList.SetNextDataSource(ctx))
|
|
1583 {
|
|
1584 ctx.ObjectMapper = om;
|
|
1585 ctx.StopMapping = false;
|
|
1586
|
|
1587 object destObject = dataDestinationList.GetNextObject(ctx);
|
|
1588
|
|
1589 if (ctx.StopMapping) continue;
|
|
1590
|
|
1591 ISupportMapping smSource = ctx.SourceObject as ISupportMapping;
|
|
1592 ISupportMapping smDest = destObject as ISupportMapping;
|
|
1593
|
|
1594 if (smSource != null)
|
|
1595 {
|
|
1596 ctx.IsSource = true;
|
|
1597 smSource.BeginMapping(ctx);
|
|
1598 ctx.IsSource = false;
|
|
1599
|
|
1600 if (ctx.StopMapping)
|
|
1601 continue;
|
|
1602 }
|
|
1603
|
|
1604 if (smDest != null)
|
|
1605 {
|
|
1606 smDest.BeginMapping(ctx);
|
|
1607
|
|
1608 if (ctx.StopMapping)
|
|
1609 continue;
|
|
1610 }
|
|
1611
|
|
1612 IMapDataDestination currentDest = current ?? dest;
|
|
1613
|
|
1614 if (current != ctx.ObjectMapper)
|
|
1615 {
|
|
1616 current = ctx.ObjectMapper;
|
|
1617 currentDest = current ?? dest;
|
|
1618
|
|
1619 if (current != null)
|
|
1620 {
|
|
1621 MapInfo info;
|
|
1622 if (!infos.TryGetValue(current, out info))
|
|
1623 {
|
|
1624 info = new MapInfo();
|
|
1625
|
|
1626 info.Index = GetIndex(ctx.DataSource, currentDest);
|
|
1627 info.Mappers = GetValueMappers(ctx.DataSource, currentDest, info.Index);
|
|
1628
|
|
1629 infos.Add(current, info);
|
|
1630 }
|
|
1631
|
|
1632 index = info.Index;
|
|
1633 mappers = info.Mappers;
|
|
1634 }
|
|
1635 else
|
|
1636 {
|
|
1637 index = GetIndex(ctx.DataSource, currentDest);
|
|
1638 mappers = GetValueMappers(ctx.DataSource, currentDest, index);
|
|
1639 }
|
|
1640 }
|
|
1641
|
|
1642 MapInternal(
|
|
1643 ctx.DataSource,
|
|
1644 ctx.SourceObject,
|
|
1645 currentDest,
|
|
1646 destObject,
|
|
1647 index,
|
|
1648 mappers);
|
|
1649
|
|
1650 if (smDest != null)
|
|
1651 smDest.EndMapping(ctx);
|
|
1652
|
|
1653 if (smSource != null)
|
|
1654 {
|
|
1655 ctx.IsSource = true;
|
|
1656 smSource.EndMapping(ctx);
|
|
1657 ctx.IsSource = false;
|
|
1658 }
|
|
1659 }
|
|
1660
|
|
1661 dataDestinationList.EndMapping(ctx);
|
|
1662 dataSourceList. EndMapping(ctx);
|
|
1663 }
|
|
1664
|
|
1665 #endregion
|
|
1666
|
|
1667 #region ValueToEnum, EnumToValue
|
|
1668
|
|
1669 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
|
|
1670 public virtual object MapValueToEnum(object value, Type type)
|
|
1671 {
|
|
1672 if (value == null || value == DBNull.Value)
|
|
1673 return GetNullValue(type);
|
|
1674
|
|
1675 MapValue[] mapValues = GetMapValues(type);
|
|
1676
|
|
1677 var mapValueType = GetMapValueType(mapValues);
|
|
1678 if (mapValueType != null && value.GetType() != mapValueType)
|
|
1679 {
|
|
1680 value = ConvertChangeType(value, mapValueType);
|
|
1681 }
|
|
1682
|
|
1683 if (mapValues != null)
|
|
1684 {
|
|
1685 var comp = (IComparable)value;
|
|
1686
|
|
1687 foreach (MapValue mv in mapValues)
|
|
1688 foreach (object mapValue in mv.MapValues)
|
|
1689 {
|
|
1690 try
|
|
1691 {
|
|
1692 if (comp.CompareTo(mapValue) == 0)
|
|
1693 return mv.OrigValue;
|
|
1694 }
|
|
1695 catch (ArgumentException ex)
|
|
1696 {
|
|
1697 Debug.WriteLine(ex.Message, MethodBase.GetCurrentMethod().Name);
|
|
1698 }
|
|
1699 }
|
|
1700 }
|
|
1701
|
|
1702 InvalidCastException exInvalidCast = null;
|
|
1703
|
|
1704 var enumType = TypeHelper.UnwrapNullableType(type);
|
|
1705 try
|
|
1706 {
|
|
1707 value = ConvertChangeType(value, Enum.GetUnderlyingType(enumType));
|
|
1708
|
|
1709 if (Enum.IsDefined(enumType, value))
|
|
1710 {
|
|
1711 // Regular (known) enum field w/o explicit mapping defined.
|
|
1712 //
|
|
1713 return Enum.ToObject(enumType, value);
|
|
1714 }
|
|
1715 }
|
|
1716 catch (InvalidCastException ex)
|
|
1717 {
|
|
1718 exInvalidCast = ex;
|
|
1719 }
|
|
1720
|
|
1721 // Default value.
|
|
1722 //
|
|
1723 object defaultValue = GetDefaultValue(type);
|
|
1724
|
|
1725 if (defaultValue != null)
|
|
1726 return defaultValue;
|
|
1727
|
|
1728 if (exInvalidCast != null)
|
|
1729 {
|
|
1730 // Rethrow an InvalidCastException when no default value specified.
|
|
1731 //
|
|
1732 throw exInvalidCast;
|
|
1733 }
|
|
1734
|
|
1735 // At this point we have an undefined enum value.
|
|
1736 //
|
|
1737 return Enum.ToObject(enumType, value);
|
|
1738 }
|
|
1739
|
|
1740 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
|
|
1741 public virtual object MapValueToEnum(object value, MemberAccessor ma)
|
|
1742 {
|
|
1743 if (value == null || value is DBNull)
|
|
1744 return GetNullValue(ma.Type);
|
|
1745
|
|
1746 MapValue[] mapValues = GetMapValues(ma);
|
|
1747
|
|
1748 var mapValueType = GetMapValueType(mapValues);
|
|
1749 if (mapValueType != null && value.GetType() != mapValueType)
|
|
1750 {
|
|
1751 value = ConvertChangeType(value, mapValueType);
|
|
1752 }
|
|
1753
|
|
1754 if (mapValues != null)
|
|
1755 {
|
|
1756 var comp = (IComparable)value;
|
|
1757
|
|
1758 foreach (MapValue mv in mapValues)
|
|
1759 foreach (object mapValue in mv.MapValues)
|
|
1760 {
|
|
1761 try
|
|
1762 {
|
|
1763 if (comp.CompareTo(mapValue) == 0)
|
|
1764 return mv.OrigValue;
|
|
1765 }
|
|
1766 catch (ArgumentException ex)
|
|
1767 {
|
|
1768 Debug.WriteLine(ex.Message, MethodBase.GetCurrentMethod().Name);
|
|
1769 }
|
|
1770 }
|
|
1771 }
|
|
1772
|
|
1773 InvalidCastException exInvalidCast = null;
|
|
1774
|
|
1775 var enumType = TypeHelper.UnwrapNullableType(ma.Type);
|
|
1776 try
|
|
1777 {
|
|
1778 value = ConvertChangeType(value, Enum.GetUnderlyingType(enumType));
|
|
1779
|
|
1780 if (Enum.IsDefined(enumType, value))
|
|
1781 {
|
|
1782 // Regular (known) enum field w/o explicit mapping defined.
|
|
1783 //
|
|
1784 return Enum.ToObject(enumType, value);
|
|
1785 }
|
|
1786 }
|
|
1787 catch (InvalidCastException ex)
|
|
1788 {
|
|
1789 exInvalidCast = ex;
|
|
1790 }
|
|
1791
|
|
1792 // Default value.
|
|
1793 //
|
|
1794 object defaultValue = GetDefaultValue(ma.Type);
|
|
1795
|
|
1796 if (defaultValue != null)
|
|
1797 return defaultValue;
|
|
1798
|
|
1799 if (exInvalidCast != null)
|
|
1800 {
|
|
1801 // Rethrow an InvalidCastException when no default value specified.
|
|
1802 //
|
|
1803 throw exInvalidCast;
|
|
1804 }
|
|
1805
|
|
1806 // At this point we have an undefined enum value.
|
|
1807 //
|
|
1808 return Enum.ToObject(enumType, value);
|
|
1809 }
|
|
1810
|
|
1811 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
|
|
1812 public virtual object MapEnumToValue(object value, [JetBrains.Annotations.NotNull] Type type, bool convertToUnderlyingType)
|
|
1813 {
|
|
1814 if (value == null)
|
|
1815 return null;
|
|
1816
|
|
1817 if (type == null) throw new ArgumentNullException("type");
|
|
1818
|
|
1819 type = value.GetType();
|
|
1820
|
|
1821 object nullValue = GetNullValue(type);
|
|
1822
|
|
1823 if (nullValue != null)
|
|
1824 {
|
|
1825 IComparable comp = (IComparable)value;
|
|
1826
|
|
1827 try
|
|
1828 {
|
|
1829 if (comp.CompareTo(nullValue) == 0)
|
|
1830 return null;
|
|
1831 }
|
|
1832 catch
|
|
1833 {
|
|
1834 }
|
|
1835 }
|
|
1836
|
|
1837 MapValue[] mapValues = GetMapValues(type);
|
|
1838
|
|
1839 if (mapValues != null)
|
|
1840 {
|
|
1841 IComparable comp = (IComparable)value;
|
|
1842
|
|
1843 foreach (MapValue mv in mapValues)
|
|
1844 {
|
|
1845 try
|
|
1846 {
|
|
1847 if (comp.CompareTo(mv.OrigValue) == 0)
|
|
1848 return mv.MapValues[0];
|
|
1849 }
|
|
1850 catch
|
|
1851 {
|
|
1852 }
|
|
1853 }
|
|
1854 }
|
|
1855
|
|
1856 return convertToUnderlyingType ?
|
|
1857 System.Convert.ChangeType(value, Enum.GetUnderlyingType(type), Thread.CurrentThread.CurrentCulture) :
|
|
1858 value;
|
|
1859 }
|
|
1860
|
|
1861 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
|
|
1862 public virtual object MapEnumToValue(object value, [JetBrains.Annotations.NotNull] MemberAccessor memberAccessor, bool convertToUnderlyingType)
|
|
1863 {
|
|
1864 if (value == null)
|
|
1865 return null;
|
|
1866
|
|
1867 if (memberAccessor == null) throw new ArgumentNullException("memberAccessor");
|
|
1868
|
|
1869 if (value is IEnumerable)
|
|
1870 {
|
|
1871 #if SILVERLIGHT
|
|
1872 var result = new List<object>();
|
|
1873
|
|
1874 foreach (var item in (IEnumerable)value)
|
|
1875 {
|
|
1876 result.Add(MapEnumToValue(item, memberAccessor, convertToUnderlyingType));
|
|
1877 }
|
|
1878
|
|
1879 var type = typeof(object);
|
|
1880
|
|
1881 foreach (var var in result)
|
|
1882 {
|
|
1883 if (var != null)
|
|
1884 {
|
|
1885 type = var.GetType();
|
|
1886 break;
|
|
1887 }
|
|
1888 }
|
|
1889
|
|
1890 var arr = Array.CreateInstance(type, result.Count);
|
|
1891
|
|
1892 Array.Copy(result.ToArray(), arr, arr.Length);
|
|
1893
|
|
1894 return arr;
|
|
1895 #else
|
|
1896 var result = new ArrayList();
|
|
1897
|
|
1898 foreach (var item in (IEnumerable)value)
|
|
1899 {
|
|
1900 result.Add(MapEnumToValue(item, memberAccessor, convertToUnderlyingType));
|
|
1901 }
|
|
1902
|
|
1903 var type = typeof(object);
|
|
1904
|
|
1905 foreach (var var in result)
|
|
1906 {
|
|
1907 if (var != null)
|
|
1908 {
|
|
1909 type = var.GetType();
|
|
1910 break;
|
|
1911 }
|
|
1912 }
|
|
1913
|
|
1914 return result.ToArray(type);
|
|
1915 #endif
|
|
1916 }
|
|
1917
|
|
1918 object nullValue = GetNullValue(memberAccessor.Type);
|
|
1919
|
|
1920 if (nullValue != null)
|
|
1921 {
|
|
1922 IComparable comp = (IComparable)value;
|
|
1923
|
|
1924 try
|
|
1925 {
|
|
1926 if (comp.CompareTo(nullValue) == 0)
|
|
1927 return null;
|
|
1928 }
|
|
1929 catch
|
|
1930 {
|
|
1931 }
|
|
1932 }
|
|
1933
|
|
1934 MapValue[] mapValues = GetMapValues(memberAccessor);
|
|
1935
|
|
1936 if (mapValues != null)
|
|
1937 {
|
|
1938 IComparable comp = (IComparable)value;
|
|
1939
|
|
1940 foreach (MapValue mv in mapValues)
|
|
1941 {
|
|
1942 try
|
|
1943 {
|
|
1944 if (comp.CompareTo(mv.OrigValue) == 0)
|
|
1945 return mv.MapValues[0];
|
|
1946 }
|
|
1947 catch
|
|
1948 {
|
|
1949 }
|
|
1950 }
|
|
1951 }
|
|
1952
|
|
1953 var memberAccessorType = TypeHelper.UnwrapNullableType(memberAccessor.Type);
|
|
1954
|
|
1955 return convertToUnderlyingType ?
|
|
1956 System.Convert.ChangeType(value, Enum.GetUnderlyingType(memberAccessorType), Thread.CurrentThread.CurrentCulture) :
|
|
1957 value;
|
|
1958 }
|
|
1959
|
|
1960 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
|
|
1961 public virtual object MapEnumToValue(object value, bool convertToUnderlyingType)
|
|
1962 {
|
|
1963 if (value == null)
|
|
1964 return null;
|
|
1965
|
|
1966 return MapEnumToValue(value, value.GetType(), convertToUnderlyingType);
|
|
1967 }
|
|
1968
|
|
1969 public object MapEnumToValue(object value)
|
|
1970 {
|
|
1971 return MapEnumToValue(value, false);
|
|
1972 }
|
|
1973
|
|
1974 public virtual object MapEnumToValue(object value, Type type)
|
|
1975 {
|
|
1976 return MapEnumToValue(value, type, false);
|
|
1977 }
|
|
1978
|
|
1979 public T MapValueToEnum<T>(object value)
|
|
1980 {
|
|
1981 return (T)MapValueToEnum(value, typeof(T));
|
|
1982 }
|
|
1983
|
|
1984 #endregion
|
|
1985
|
|
1986 #region Object
|
|
1987
|
|
1988 #region MapObjectToObject
|
|
1989
|
|
1990 public object MapObjectToObject(
|
|
1991 object sourceObject,
|
|
1992 object destObject,
|
|
1993 params object[] parameters)
|
|
1994 {
|
|
1995 if (sourceObject == null) throw new ArgumentNullException("sourceObject");
|
|
1996 if (destObject == null) throw new ArgumentNullException("destObject");
|
|
1997
|
|
1998 MapInternal(
|
|
1999 null,
|
|
2000 GetObjectMapper(sourceObject.GetType()), sourceObject,
|
|
2001 GetObjectMapper(destObject. GetType()), destObject,
|
|
2002 parameters);
|
|
2003
|
|
2004 return destObject;
|
|
2005 }
|
|
2006
|
|
2007 public object MapObjectToObject(
|
|
2008 object sourceObject,
|
|
2009 Type destObjectType,
|
|
2010 params object[] parameters)
|
|
2011 {
|
|
2012 if (sourceObject == null) throw new ArgumentNullException("sourceObject");
|
|
2013
|
|
2014 InitContext ctx = new InitContext();
|
|
2015
|
|
2016 ctx.MappingSchema = this;
|
|
2017 ctx.DataSource = GetObjectMapper(sourceObject.GetType());
|
|
2018 ctx.SourceObject = sourceObject;
|
|
2019 ctx.ObjectMapper = GetObjectMapper(destObjectType);
|
|
2020 ctx.Parameters = parameters;
|
|
2021
|
|
2022 return MapInternal(ctx);
|
|
2023 }
|
|
2024
|
|
2025 public T MapObjectToObject<T>(
|
|
2026 object sourceObject,
|
|
2027 params object[] parameters)
|
|
2028 {
|
|
2029 return (T)MapObjectToObject(sourceObject, typeof(T), parameters);
|
|
2030 }
|
|
2031
|
|
2032 #endregion
|
|
2033
|
|
2034 #region MapObjectToDataRow
|
|
2035
|
|
2036 #if !SILVERLIGHT
|
|
2037
|
|
2038 public DataRow MapObjectToDataRow(
|
|
2039 object sourceObject,
|
|
2040 DataRow destRow)
|
|
2041 {
|
|
2042 if (sourceObject == null) throw new ArgumentNullException("sourceObject");
|
|
2043
|
|
2044 MapInternal(
|
|
2045 null,
|
|
2046 GetObjectMapper (sourceObject.GetType()), sourceObject,
|
|
2047 CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
|
|
2048 null);
|
|
2049
|
|
2050 return destRow;
|
|
2051 }
|
|
2052
|
|
2053 public DataRow MapObjectToDataRow(
|
|
2054 object sourceObject,
|
|
2055 DataTable destTable)
|
|
2056 {
|
|
2057 if (destTable == null) throw new ArgumentNullException("destTable");
|
|
2058 if (sourceObject == null) throw new ArgumentNullException("sourceObject");
|
|
2059
|
|
2060 DataRow destRow = destTable.NewRow();
|
|
2061
|
|
2062 destTable.Rows.Add(destRow);
|
|
2063
|
|
2064 MapInternal(
|
|
2065 null,
|
|
2066 GetObjectMapper (sourceObject.GetType()), sourceObject,
|
|
2067 CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
|
|
2068 null);
|
|
2069
|
|
2070 return destRow;
|
|
2071 }
|
|
2072
|
|
2073 #endif
|
|
2074
|
|
2075 #endregion
|
|
2076
|
|
2077 #region MapObjectToDictionary
|
|
2078
|
|
2079 public IDictionary MapObjectToDictionary(
|
|
2080 object sourceObject,
|
|
2081 IDictionary destDictionary)
|
|
2082 {
|
|
2083 if (sourceObject == null) throw new ArgumentNullException("sourceObject");
|
|
2084
|
|
2085 MapInternal(
|
|
2086 null,
|
|
2087 GetObjectMapper (sourceObject.GetType()), sourceObject,
|
|
2088 CreateDictionaryMapper(destDictionary), destDictionary,
|
|
2089 null);
|
|
2090
|
|
2091 return destDictionary;
|
|
2092 }
|
|
2093
|
|
2094 public IDictionary MapObjectToDictionary(object sourceObject)
|
|
2095 {
|
|
2096 if (sourceObject == null) throw new ArgumentNullException("sourceObject");
|
|
2097
|
|
2098 ObjectMapper om = GetObjectMapper(sourceObject.GetType());
|
|
2099
|
|
2100 var destDictionary = new Dictionary<object,object>(om.Count);
|
|
2101
|
|
2102 MapInternal(
|
|
2103 null,
|
|
2104 om, sourceObject,
|
|
2105 CreateDictionaryMapper(destDictionary), destDictionary,
|
|
2106 null);
|
|
2107
|
|
2108 return destDictionary;
|
|
2109 }
|
|
2110
|
|
2111 #endregion
|
|
2112
|
|
2113 #endregion
|
|
2114
|
|
2115 #region DataRow
|
|
2116
|
|
2117 #if !SILVERLIGHT
|
|
2118
|
|
2119 #region MapDataRowToObject
|
|
2120
|
|
2121 public object MapDataRowToObject(
|
|
2122 DataRow dataRow,
|
|
2123 object destObject,
|
|
2124 params object[] parameters)
|
|
2125 {
|
|
2126 if (destObject == null) throw new ArgumentNullException("destObject");
|
|
2127
|
|
2128 MapInternal(
|
|
2129 null,
|
|
2130 CreateDataRowMapper(dataRow, DataRowVersion.Default), dataRow,
|
|
2131 GetObjectMapper(destObject. GetType()), destObject,
|
|
2132 parameters);
|
|
2133
|
|
2134 return destObject;
|
|
2135 }
|
|
2136
|
|
2137 public object MapDataRowToObject(
|
|
2138 DataRow dataRow,
|
|
2139 DataRowVersion version,
|
|
2140 object destObject,
|
|
2141 params object[] parameters)
|
|
2142 {
|
|
2143 if (destObject == null) throw new ArgumentNullException("destObject");
|
|
2144
|
|
2145 MapInternal(
|
|
2146 null,
|
|
2147 CreateDataRowMapper(dataRow, version), dataRow,
|
|
2148 GetObjectMapper(destObject. GetType()), destObject,
|
|
2149 parameters);
|
|
2150
|
|
2151 return destObject;
|
|
2152 }
|
|
2153
|
|
2154 public object MapDataRowToObject(
|
|
2155 DataRow dataRow,
|
|
2156 Type destObjectType,
|
|
2157 params object[] parameters)
|
|
2158 {
|
|
2159 InitContext ctx = new InitContext();
|
|
2160
|
|
2161 ctx.MappingSchema = this;
|
|
2162 ctx.DataSource = CreateDataRowMapper(dataRow, DataRowVersion.Default);
|
|
2163 ctx.SourceObject = dataRow;
|
|
2164 ctx.ObjectMapper = GetObjectMapper(destObjectType);
|
|
2165 ctx.Parameters = parameters;
|
|
2166
|
|
2167 return MapInternal(ctx);
|
|
2168 }
|
|
2169
|
|
2170 public object MapDataRowToObject(
|
|
2171 DataRow dataRow,
|
|
2172 DataRowVersion version,
|
|
2173 Type destObjectType,
|
|
2174 params object[] parameters)
|
|
2175 {
|
|
2176 InitContext ctx = new InitContext();
|
|
2177
|
|
2178 ctx.MappingSchema = this;
|
|
2179 ctx.DataSource = CreateDataRowMapper(dataRow, version);
|
|
2180 ctx.SourceObject = dataRow;
|
|
2181 ctx.ObjectMapper = GetObjectMapper(destObjectType);
|
|
2182 ctx.Parameters = parameters;
|
|
2183
|
|
2184 return MapInternal(ctx);
|
|
2185 }
|
|
2186
|
|
2187 public T MapDataRowToObject<T>(
|
|
2188 DataRow dataRow,
|
|
2189 params object[] parameters)
|
|
2190 {
|
|
2191 return (T)MapDataRowToObject(dataRow, typeof(T), parameters);
|
|
2192 }
|
|
2193
|
|
2194 public T MapDataRowToObject<T>(
|
|
2195 DataRow dataRow,
|
|
2196 DataRowVersion version,
|
|
2197 params object[] parameters)
|
|
2198 {
|
|
2199 return (T)MapDataRowToObject(dataRow, version, typeof(T), parameters);
|
|
2200 }
|
|
2201
|
|
2202 #endregion
|
|
2203
|
|
2204 #region MapDataRowToDataRow
|
|
2205
|
|
2206 public DataRow MapDataRowToDataRow(
|
|
2207 DataRow sourceRow,
|
|
2208 DataRow destRow)
|
|
2209 {
|
|
2210 MapInternal(
|
|
2211 null,
|
|
2212 CreateDataRowMapper(sourceRow, DataRowVersion.Default), sourceRow,
|
|
2213 CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
|
|
2214 null);
|
|
2215
|
|
2216 return destRow;
|
|
2217 }
|
|
2218
|
|
2219 public DataRow MapDataRowToDataRow(
|
|
2220 DataRow sourceRow,
|
|
2221 DataRowVersion version,
|
|
2222 DataRow destRow)
|
|
2223 {
|
|
2224 MapInternal(
|
|
2225 null,
|
|
2226 CreateDataRowMapper(sourceRow, version), sourceRow,
|
|
2227 CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
|
|
2228 null);
|
|
2229
|
|
2230 return destRow;
|
|
2231 }
|
|
2232
|
|
2233 public DataRow MapDataRowToDataRow(
|
|
2234 DataRow sourceRow,
|
|
2235 DataTable destTable)
|
|
2236 {
|
|
2237 if (destTable == null) throw new ArgumentNullException("destTable");
|
|
2238
|
|
2239 DataRow destRow = destTable.NewRow();
|
|
2240
|
|
2241 destTable.Rows.Add(destRow);
|
|
2242
|
|
2243 MapInternal(
|
|
2244 null,
|
|
2245 CreateDataRowMapper(sourceRow, DataRowVersion.Default), sourceRow,
|
|
2246 CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
|
|
2247 null);
|
|
2248
|
|
2249 return destRow;
|
|
2250 }
|
|
2251
|
|
2252 public DataRow MapDataRowToDataRow(
|
|
2253 DataRow sourceRow,
|
|
2254 DataRowVersion version,
|
|
2255 DataTable destTable)
|
|
2256 {
|
|
2257 if (destTable == null) throw new ArgumentNullException("destTable");
|
|
2258
|
|
2259 DataRow destRow = destTable.NewRow();
|
|
2260
|
|
2261 destTable.Rows.Add(destRow);
|
|
2262
|
|
2263 MapInternal(
|
|
2264 null,
|
|
2265 CreateDataRowMapper(sourceRow, version), sourceRow,
|
|
2266 CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
|
|
2267 null);
|
|
2268
|
|
2269 return destRow;
|
|
2270 }
|
|
2271
|
|
2272 #endregion
|
|
2273
|
|
2274 #region MapDataRowToDictionary
|
|
2275
|
|
2276 public IDictionary MapDataRowToDictionary(
|
|
2277 DataRow sourceRow,
|
|
2278 IDictionary destDictionary)
|
|
2279 {
|
|
2280 MapInternal(
|
|
2281 null,
|
|
2282 CreateDataRowMapper (sourceRow, DataRowVersion.Default), sourceRow,
|
|
2283 CreateDictionaryMapper(destDictionary), destDictionary,
|
|
2284 null);
|
|
2285
|
|
2286 return destDictionary;
|
|
2287 }
|
|
2288
|
|
2289 public Hashtable MapDataRowToDictionary(DataRow sourceRow)
|
|
2290 {
|
|
2291 if (sourceRow == null) throw new ArgumentNullException("sourceRow");
|
|
2292
|
|
2293 Hashtable destDictionary = new Hashtable(sourceRow.Table.Columns.Count);
|
|
2294
|
|
2295 MapInternal(
|
|
2296 null,
|
|
2297 CreateDataRowMapper (sourceRow, DataRowVersion.Default), sourceRow,
|
|
2298 CreateDictionaryMapper(destDictionary), destDictionary,
|
|
2299 null);
|
|
2300
|
|
2301 return destDictionary;
|
|
2302 }
|
|
2303
|
|
2304 public IDictionary MapDataRowToDictionary(
|
|
2305 DataRow sourceRow,
|
|
2306 DataRowVersion version,
|
|
2307 IDictionary destDictionary)
|
|
2308 {
|
|
2309 MapInternal(
|
|
2310 null,
|
|
2311 CreateDataRowMapper (sourceRow, version), sourceRow,
|
|
2312 CreateDictionaryMapper(destDictionary), destDictionary,
|
|
2313 null);
|
|
2314
|
|
2315 return destDictionary;
|
|
2316 }
|
|
2317
|
|
2318 public Hashtable MapDataRowToDictionary(
|
|
2319 DataRow sourceRow,
|
|
2320 DataRowVersion version)
|
|
2321 {
|
|
2322 if (sourceRow == null) throw new ArgumentNullException("sourceRow");
|
|
2323
|
|
2324 Hashtable destDictionary = new Hashtable(sourceRow.Table.Columns.Count);
|
|
2325
|
|
2326 MapInternal(
|
|
2327 null,
|
|
2328 CreateDataRowMapper (sourceRow, version), sourceRow,
|
|
2329 CreateDictionaryMapper(destDictionary), destDictionary,
|
|
2330 null);
|
|
2331
|
|
2332 return destDictionary;
|
|
2333 }
|
|
2334
|
|
2335 #endregion
|
|
2336
|
|
2337 #endif
|
|
2338
|
|
2339 #endregion
|
|
2340
|
|
2341 #region DataReader
|
|
2342
|
|
2343 #region MapDataReaderToObject
|
|
2344
|
|
2345 public object MapDataReaderToObject(
|
|
2346 IDataReader dataReader,
|
|
2347 object destObject,
|
|
2348 params object[] parameters)
|
|
2349 {
|
|
2350 if (destObject == null) throw new ArgumentNullException("destObject");
|
|
2351
|
|
2352 MapInternal(
|
|
2353 null,
|
|
2354 CreateDataReaderMapper(dataReader), dataReader,
|
|
2355 GetObjectMapper(destObject. GetType()), destObject,
|
|
2356 parameters);
|
|
2357
|
|
2358 return destObject;
|
|
2359 }
|
|
2360
|
|
2361 //NOTE changed to virtual
|
|
2362 public virtual object MapDataReaderToObject(
|
|
2363 IDataReader dataReader,
|
|
2364 Type destObjectType,
|
|
2365 params object[] parameters)
|
|
2366 {
|
|
2367 InitContext ctx = new InitContext();
|
|
2368
|
|
2369 ctx.MappingSchema = this;
|
|
2370 ctx.DataSource = CreateDataReaderMapper(dataReader);
|
|
2371 ctx.SourceObject = dataReader;
|
|
2372 ctx.ObjectMapper = GetObjectMapper(destObjectType);
|
|
2373 ctx.Parameters = parameters;
|
|
2374
|
|
2375 return MapInternal(ctx);
|
|
2376 }
|
|
2377
|
|
2378 public T MapDataReaderToObject<T>(
|
|
2379 IDataReader dataReader,
|
|
2380 params object[] parameters)
|
|
2381 {
|
|
2382 return (T)MapDataReaderToObject(dataReader, typeof(T), parameters);
|
|
2383 }
|
|
2384
|
|
2385 #endregion
|
|
2386
|
|
2387 #region MapDataReaderToDataRow
|
|
2388
|
|
2389 #if !SILVERLIGHT
|
|
2390
|
|
2391 public DataRow MapDataReaderToDataRow(IDataReader dataReader, DataRow destRow)
|
|
2392 {
|
|
2393 MapInternal(
|
|
2394 null,
|
|
2395 CreateDataReaderMapper(dataReader), dataReader,
|
|
2396 CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
|
|
2397 null);
|
|
2398
|
|
2399 return destRow;
|
|
2400 }
|
|
2401
|
|
2402 public DataRow MapDataReaderToDataRow(
|
|
2403 IDataReader dataReader,
|
|
2404 DataTable destTable)
|
|
2405 {
|
|
2406 if (destTable == null) throw new ArgumentNullException("destTable");
|
|
2407
|
|
2408 DataRow destRow = destTable.NewRow();
|
|
2409
|
|
2410 destTable.Rows.Add(destRow);
|
|
2411
|
|
2412 MapInternal(
|
|
2413 null,
|
|
2414 CreateDataReaderMapper(dataReader), dataReader,
|
|
2415 CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
|
|
2416 null);
|
|
2417
|
|
2418 return destRow;
|
|
2419 }
|
|
2420
|
|
2421 #endif
|
|
2422
|
|
2423 #endregion
|
|
2424
|
|
2425 #region MapDataReaderToDictionary
|
|
2426
|
|
2427 public IDictionary MapDataReaderToDictionary(
|
|
2428 IDataReader dataReader,
|
|
2429 IDictionary destDictionary)
|
|
2430 {
|
|
2431 MapInternal(
|
|
2432 null,
|
|
2433 CreateDataReaderMapper(dataReader), dataReader,
|
|
2434 CreateDictionaryMapper(destDictionary), destDictionary,
|
|
2435 null);
|
|
2436
|
|
2437 return destDictionary;
|
|
2438 }
|
|
2439
|
|
2440 public IDictionary MapDataReaderToDictionary(IDataReader dataReader)
|
|
2441 {
|
|
2442 if (dataReader == null) throw new ArgumentNullException("dataReader");
|
|
2443
|
|
2444 var destDictionary = new Dictionary<object,object>(dataReader.FieldCount);
|
|
2445
|
|
2446 MapInternal(
|
|
2447 null,
|
|
2448 CreateDataReaderMapper(dataReader), dataReader,
|
|
2449 CreateDictionaryMapper(destDictionary), destDictionary,
|
|
2450 null);
|
|
2451
|
|
2452 return destDictionary;
|
|
2453 }
|
|
2454
|
|
2455 #endregion
|
|
2456
|
|
2457 #endregion
|
|
2458
|
|
2459 #region Dictionary
|
|
2460
|
|
2461 #region MapDictionaryToObject
|
|
2462
|
|
2463 public object MapDictionaryToObject(
|
|
2464 IDictionary sourceDictionary,
|
|
2465 object destObject,
|
|
2466 params object[] parameters)
|
|
2467 {
|
|
2468 if (destObject == null) throw new ArgumentNullException("destObject");
|
|
2469
|
|
2470 MapInternal(
|
|
2471 null,
|
|
2472 CreateDictionaryMapper(sourceDictionary), sourceDictionary,
|
|
2473 GetObjectMapper (destObject. GetType()), destObject,
|
|
2474 parameters);
|
|
2475
|
|
2476 return destObject;
|
|
2477 }
|
|
2478
|
|
2479 public object MapDictionaryToObject(
|
|
2480 IDictionary sourceDictionary,
|
|
2481 Type destObjectType,
|
|
2482 params object[] parameters)
|
|
2483 {
|
|
2484 InitContext ctx = new InitContext();
|
|
2485
|
|
2486 ctx.MappingSchema = this;
|
|
2487 ctx.DataSource = CreateDictionaryMapper(sourceDictionary);
|
|
2488 ctx.SourceObject = sourceDictionary;
|
|
2489 ctx.ObjectMapper = GetObjectMapper(destObjectType);
|
|
2490 ctx.Parameters = parameters;
|
|
2491
|
|
2492 return MapInternal(ctx);
|
|
2493 }
|
|
2494
|
|
2495 public T MapDictionaryToObject<T>(IDictionary sourceDictionary, params object[] parameters)
|
|
2496 {
|
|
2497 return (T)MapDictionaryToObject(sourceDictionary, typeof(T), parameters);
|
|
2498 }
|
|
2499
|
|
2500 #endregion
|
|
2501
|
|
2502 #region MapDictionaryToDataRow
|
|
2503
|
|
2504 #if !SILVERLIGHT
|
|
2505
|
|
2506 public DataRow MapDictionaryToDataRow(
|
|
2507 IDictionary sourceDictionary,
|
|
2508 DataRow destRow)
|
|
2509 {
|
|
2510 MapInternal(
|
|
2511 null,
|
|
2512 CreateDictionaryMapper(sourceDictionary), sourceDictionary,
|
|
2513 CreateDataRowMapper (destRow, DataRowVersion.Default), destRow,
|
|
2514 null);
|
|
2515
|
|
2516 return destRow;
|
|
2517 }
|
|
2518
|
|
2519 public DataRow MapDictionaryToDataRow(
|
|
2520 IDictionary sourceDictionary,
|
|
2521 DataTable destTable)
|
|
2522 {
|
|
2523 if (destTable == null) throw new ArgumentNullException("destTable");
|
|
2524
|
|
2525 DataRow destRow = destTable.NewRow();
|
|
2526
|
|
2527 destTable.Rows.Add(destRow);
|
|
2528
|
|
2529 MapInternal(
|
|
2530 null,
|
|
2531 CreateDictionaryMapper(sourceDictionary), sourceDictionary,
|
|
2532 CreateDataRowMapper (destRow, DataRowVersion.Default), destRow,
|
|
2533 null);
|
|
2534
|
|
2535 return destRow;
|
|
2536 }
|
|
2537
|
|
2538 #endif
|
|
2539
|
|
2540 #endregion
|
|
2541
|
|
2542 #endregion
|
|
2543
|
|
2544 #region List
|
|
2545
|
|
2546 #region MapListToList
|
|
2547
|
|
2548 public IList MapListToList(
|
|
2549 ICollection sourceList,
|
|
2550 IList destList,
|
|
2551 Type destObjectType,
|
|
2552 params object[] parameters)
|
|
2553 {
|
|
2554 if (sourceList == null) throw new ArgumentNullException("sourceList");
|
|
2555
|
|
2556 MapSourceListToDestinationList(
|
|
2557 CreateEnumeratorMapper(sourceList.GetEnumerator()),
|
|
2558 CreateObjectListMapper(destList, GetObjectMapper(destObjectType)),
|
|
2559 parameters);
|
|
2560
|
|
2561 return destList;
|
|
2562 }
|
|
2563
|
|
2564 public IList MapListToList(
|
|
2565 ICollection sourceList,
|
|
2566 Type destObjectType,
|
|
2567 params object[] parameters)
|
|
2568 {
|
|
2569 if (sourceList == null) throw new ArgumentNullException("sourceList");
|
|
2570
|
|
2571 var destList = new List<object>();
|
|
2572
|
|
2573 MapSourceListToDestinationList(
|
|
2574 CreateEnumeratorMapper(sourceList.GetEnumerator()),
|
|
2575 CreateObjectListMapper(destList, GetObjectMapper(destObjectType)),
|
|
2576 parameters);
|
|
2577
|
|
2578 return destList;
|
|
2579 }
|
|
2580
|
|
2581 public List<T> MapListToList<T>(
|
|
2582 ICollection sourceList,
|
|
2583 List<T> destList,
|
|
2584 params object[] parameters)
|
|
2585 {
|
|
2586 MapSourceListToDestinationList(
|
|
2587 CreateEnumeratorMapper(sourceList.GetEnumerator()),
|
|
2588 CreateObjectListMapper(destList, GetObjectMapper(typeof(T))),
|
|
2589 parameters);
|
|
2590
|
|
2591 return destList;
|
|
2592 }
|
|
2593
|
|
2594 public List<T> MapListToList<T>(
|
|
2595 ICollection sourceList,
|
|
2596 params object[] parameters)
|
|
2597 {
|
|
2598 List<T> destList = new List<T>();
|
|
2599
|
|
2600 MapSourceListToDestinationList(
|
|
2601 CreateEnumeratorMapper(sourceList.GetEnumerator()),
|
|
2602 CreateObjectListMapper(destList, GetObjectMapper(typeof(T))),
|
|
2603 parameters);
|
|
2604
|
|
2605 return destList;
|
|
2606 }
|
|
2607
|
|
2608 #endregion
|
|
2609
|
|
2610 #region MapListToDataTable
|
|
2611
|
|
2612 #if !SILVERLIGHT
|
|
2613
|
|
2614 public DataTable MapListToDataTable(
|
|
2615 ICollection sourceList,
|
|
2616 DataTable destTable)
|
|
2617 {
|
|
2618 if (sourceList == null) throw new ArgumentNullException("sourceList");
|
|
2619
|
|
2620 MapSourceListToDestinationList(
|
|
2621 CreateEnumeratorMapper(sourceList.GetEnumerator()),
|
|
2622 CreateDataTableMapper (destTable, DataRowVersion.Default),
|
|
2623 null);
|
|
2624
|
|
2625 return destTable;
|
|
2626 }
|
|
2627
|
|
2628 [SuppressMessage("Microsoft.Globalization", "CA1306:SetLocaleForDataTypes")]
|
|
2629 public DataTable MapListToDataTable(ICollection sourceList)
|
|
2630 {
|
|
2631 if (sourceList == null) throw new ArgumentNullException("sourceList");
|
|
2632
|
|
2633 DataTable destTable = new DataTable();
|
|
2634
|
|
2635 MapSourceListToDestinationList(
|
|
2636 CreateEnumeratorMapper(sourceList.GetEnumerator()),
|
|
2637 CreateDataTableMapper (destTable, DataRowVersion.Default),
|
|
2638 null);
|
|
2639
|
|
2640 return destTable;
|
|
2641 }
|
|
2642
|
|
2643 #endif
|
|
2644
|
|
2645 #endregion
|
|
2646
|
|
2647 #region MapListToDictionary
|
|
2648
|
|
2649 public IDictionary MapListToDictionary(
|
|
2650 ICollection sourceList,
|
|
2651 IDictionary destDictionary,
|
|
2652 NameOrIndexParameter keyFieldNameOrIndex,
|
|
2653 Type destObjectType,
|
|
2654 params object[] parameters)
|
|
2655 {
|
|
2656 if (sourceList == null) throw new ArgumentNullException("sourceList");
|
|
2657
|
|
2658 MapSourceListToDestinationList(
|
|
2659 CreateEnumeratorMapper (sourceList.GetEnumerator()),
|
|
2660 CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
|
|
2661 parameters);
|
|
2662
|
|
2663 return destDictionary;
|
|
2664 }
|
|
2665
|
|
2666 public IDictionary MapListToDictionary(
|
|
2667 ICollection sourceList,
|
|
2668 NameOrIndexParameter keyFieldNameOrIndex,
|
|
2669 Type destObjectType,
|
|
2670 params object[] parameters)
|
|
2671 {
|
|
2672 if (sourceList == null) throw new ArgumentNullException("sourceList");
|
|
2673
|
|
2674 IDictionary destDictionary = new Dictionary<object,object>();
|
|
2675
|
|
2676 MapSourceListToDestinationList(
|
|
2677 CreateEnumeratorMapper (sourceList.GetEnumerator()),
|
|
2678 CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
|
|
2679 parameters);
|
|
2680
|
|
2681 return destDictionary;
|
|
2682 }
|
|
2683
|
|
2684 public IDictionary<TK,T> MapListToDictionary<TK,T>(
|
|
2685 ICollection sourceList,
|
|
2686 IDictionary<TK,T> destDictionary,
|
|
2687 NameOrIndexParameter keyFieldNameOrIndex,
|
|
2688 params object[] parameters)
|
|
2689 {
|
|
2690 MapSourceListToDestinationList(
|
|
2691 CreateEnumeratorMapper (sourceList.GetEnumerator()),
|
|
2692 CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
|
|
2693 parameters);
|
|
2694
|
|
2695 return destDictionary;
|
|
2696 }
|
|
2697
|
|
2698 public Dictionary<TK,T> MapListToDictionary<TK,T>(
|
|
2699 ICollection sourceList,
|
|
2700 NameOrIndexParameter keyFieldNameOrIndex,
|
|
2701 params object[] parameters)
|
|
2702 {
|
|
2703 Dictionary<TK,T> destDictionary = new Dictionary<TK,T>();
|
|
2704
|
|
2705 MapSourceListToDestinationList(
|
|
2706 CreateEnumeratorMapper (sourceList.GetEnumerator()),
|
|
2707 CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
|
|
2708 parameters);
|
|
2709
|
|
2710 return destDictionary;
|
|
2711 }
|
|
2712
|
|
2713 #endregion
|
|
2714
|
|
2715 #region MapListToDictionaryIndex
|
|
2716
|
|
2717 public IDictionary MapListToDictionary(
|
|
2718 ICollection sourceList,
|
|
2719 IDictionary destDictionary,
|
|
2720 MapIndex index,
|
|
2721 Type destObjectType,
|
|
2722 params object[] parameters)
|
|
2723 {
|
|
2724 if (sourceList == null) throw new ArgumentNullException("sourceList");
|
|
2725
|
|
2726 MapSourceListToDestinationList(
|
|
2727 CreateEnumeratorMapper (sourceList.GetEnumerator()),
|
|
2728 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
|
|
2729 parameters);
|
|
2730
|
|
2731 return destDictionary;
|
|
2732 }
|
|
2733
|
|
2734 public IDictionary MapListToDictionary(
|
|
2735 ICollection sourceList,
|
|
2736 MapIndex index,
|
|
2737 Type destObjectType,
|
|
2738 params object[] parameters)
|
|
2739 {
|
|
2740 if (sourceList == null) throw new ArgumentNullException("sourceList");
|
|
2741
|
|
2742 IDictionary destDictionary = new Dictionary<object,object>();
|
|
2743
|
|
2744 MapSourceListToDestinationList(
|
|
2745 CreateEnumeratorMapper (sourceList.GetEnumerator()),
|
|
2746 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
|
|
2747 parameters);
|
|
2748
|
|
2749 return destDictionary;
|
|
2750 }
|
|
2751
|
|
2752 public IDictionary<CompoundValue,T> MapListToDictionary<T>(
|
|
2753 ICollection sourceList,
|
|
2754 IDictionary<CompoundValue,T> destDictionary,
|
|
2755 MapIndex index,
|
|
2756 params object[] parameters)
|
|
2757 {
|
|
2758 MapSourceListToDestinationList(
|
|
2759 CreateEnumeratorMapper (sourceList.GetEnumerator()),
|
|
2760 CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
|
|
2761 parameters);
|
|
2762
|
|
2763 return destDictionary;
|
|
2764 }
|
|
2765
|
|
2766 public Dictionary<CompoundValue,T> MapListToDictionary<T>(
|
|
2767 ICollection sourceList,
|
|
2768 MapIndex index,
|
|
2769 params object[] parameters)
|
|
2770 {
|
|
2771 Dictionary<CompoundValue, T> destDictionary = new Dictionary<CompoundValue,T>();
|
|
2772
|
|
2773 MapSourceListToDestinationList(
|
|
2774 CreateEnumeratorMapper (sourceList.GetEnumerator()),
|
|
2775 CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
|
|
2776 parameters);
|
|
2777
|
|
2778 return destDictionary;
|
|
2779 }
|
|
2780
|
|
2781 #endregion
|
|
2782
|
|
2783 #endregion
|
|
2784
|
|
2785 #region Table
|
|
2786
|
|
2787 #if !SILVERLIGHT
|
|
2788
|
|
2789
|
|
2790 #region MapDataTableToDataTable
|
|
2791
|
|
2792 public DataTable MapDataTableToDataTable(
|
|
2793 DataTable sourceTable,
|
|
2794 DataTable destTable)
|
|
2795 {
|
|
2796 MapSourceListToDestinationList(
|
|
2797 CreateDataTableMapper(sourceTable, DataRowVersion.Default),
|
|
2798 CreateDataTableMapper(destTable, DataRowVersion.Default),
|
|
2799 null);
|
|
2800
|
|
2801 return destTable;
|
|
2802 }
|
|
2803
|
|
2804 public DataTable MapDataTableToDataTable(
|
|
2805 DataTable sourceTable,
|
|
2806 DataRowVersion version,
|
|
2807 DataTable destTable)
|
|
2808 {
|
|
2809 MapSourceListToDestinationList(
|
|
2810 CreateDataTableMapper(sourceTable, version),
|
|
2811 CreateDataTableMapper(destTable, DataRowVersion.Default),
|
|
2812 null);
|
|
2813
|
|
2814 return destTable;
|
|
2815 }
|
|
2816
|
|
2817 public DataTable MapDataTableToDataTable(DataTable sourceTable)
|
|
2818 {
|
|
2819 if (sourceTable == null) throw new ArgumentNullException("sourceTable");
|
|
2820
|
|
2821 DataTable destTable = sourceTable.Clone();
|
|
2822
|
|
2823 MapSourceListToDestinationList(
|
|
2824 CreateDataTableMapper(sourceTable, DataRowVersion.Default),
|
|
2825 CreateDataTableMapper(destTable, DataRowVersion.Default),
|
|
2826 null);
|
|
2827
|
|
2828 return destTable;
|
|
2829 }
|
|
2830
|
|
2831 public DataTable MapDataTableToDataTable(
|
|
2832 DataTable sourceTable,
|
|
2833 DataRowVersion version)
|
|
2834 {
|
|
2835 if (sourceTable == null) throw new ArgumentNullException("sourceTable");
|
|
2836
|
|
2837 DataTable destTable = sourceTable.Clone();
|
|
2838
|
|
2839 MapSourceListToDestinationList(
|
|
2840 CreateDataTableMapper(sourceTable, version),
|
|
2841 CreateDataTableMapper(destTable, DataRowVersion.Default),
|
|
2842 null);
|
|
2843
|
|
2844 return destTable;
|
|
2845 }
|
|
2846
|
|
2847 #endregion
|
|
2848
|
|
2849 #region MapDataTableToList
|
|
2850
|
|
2851 public IList MapDataTableToList(
|
|
2852 DataTable sourceTable,
|
|
2853 IList list,
|
|
2854 Type destObjectType,
|
|
2855 params object[] parameters)
|
|
2856 {
|
|
2857 MapSourceListToDestinationList(
|
|
2858 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
2859 CreateObjectListMapper(list, GetObjectMapper(destObjectType)),
|
|
2860 parameters);
|
|
2861
|
|
2862 return list;
|
|
2863 }
|
|
2864
|
|
2865 public IList MapDataTableToList(
|
|
2866 DataTable sourceTable,
|
|
2867 DataRowVersion version,
|
|
2868 IList list,
|
|
2869 Type destObjectType,
|
|
2870 params object[] parameters)
|
|
2871 {
|
|
2872 MapSourceListToDestinationList(
|
|
2873 CreateDataTableMapper (sourceTable, version),
|
|
2874 CreateObjectListMapper(list, GetObjectMapper(destObjectType)),
|
|
2875 parameters);
|
|
2876
|
|
2877 return list;
|
|
2878 }
|
|
2879
|
|
2880 public ArrayList MapDataTableToList(
|
|
2881 DataTable sourceTable,
|
|
2882 Type destObjectType,
|
|
2883 params object[] parameters)
|
|
2884 {
|
|
2885 ArrayList list = new ArrayList();
|
|
2886
|
|
2887 MapSourceListToDestinationList(
|
|
2888 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
2889 CreateObjectListMapper(list, GetObjectMapper(destObjectType)),
|
|
2890 parameters);
|
|
2891
|
|
2892 return list;
|
|
2893 }
|
|
2894
|
|
2895 public ArrayList MapDataTableToList(
|
|
2896 DataTable sourceTable,
|
|
2897 DataRowVersion version,
|
|
2898 Type destObjectType,
|
|
2899 params object[] parameters)
|
|
2900 {
|
|
2901 ArrayList list = new ArrayList();
|
|
2902
|
|
2903 MapSourceListToDestinationList(
|
|
2904 CreateDataTableMapper (sourceTable, version),
|
|
2905 CreateObjectListMapper(list, GetObjectMapper(destObjectType)),
|
|
2906 parameters);
|
|
2907
|
|
2908 return list;
|
|
2909 }
|
|
2910
|
|
2911 public List<T> MapDataTableToList<T>(
|
|
2912 DataTable sourceTable,
|
|
2913 List<T> list,
|
|
2914 params object[] parameters)
|
|
2915 {
|
|
2916 MapSourceListToDestinationList(
|
|
2917 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
2918 CreateObjectListMapper(list, GetObjectMapper(typeof(T))),
|
|
2919 parameters);
|
|
2920
|
|
2921 return list;
|
|
2922 }
|
|
2923
|
|
2924 public List<T> MapDataTableToList<T>(
|
|
2925 DataTable sourceTable,
|
|
2926 DataRowVersion version,
|
|
2927 List<T> list,
|
|
2928 params object[] parameters)
|
|
2929 {
|
|
2930 MapSourceListToDestinationList(
|
|
2931 CreateDataTableMapper (sourceTable, version),
|
|
2932 CreateObjectListMapper(list, GetObjectMapper(typeof(T))),
|
|
2933 parameters);
|
|
2934
|
|
2935 return list;
|
|
2936 }
|
|
2937
|
|
2938 public List<T> MapDataTableToList<T>(
|
|
2939 DataTable sourceTable,
|
|
2940 params object[] parameters)
|
|
2941 {
|
|
2942 List<T> list = new List<T>();
|
|
2943
|
|
2944 MapSourceListToDestinationList(
|
|
2945 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
2946 CreateObjectListMapper(list, GetObjectMapper(typeof(T))),
|
|
2947 parameters);
|
|
2948
|
|
2949 return list;
|
|
2950 }
|
|
2951
|
|
2952 public List<T> MapDataTableToList<T>(
|
|
2953 DataTable sourceTable,
|
|
2954 DataRowVersion version,
|
|
2955 params object[] parameters)
|
|
2956 {
|
|
2957 List<T> list = new List<T>();
|
|
2958
|
|
2959 MapSourceListToDestinationList(
|
|
2960 CreateDataTableMapper (sourceTable, version),
|
|
2961 CreateObjectListMapper(list, GetObjectMapper(typeof(T))),
|
|
2962 parameters);
|
|
2963
|
|
2964 return list;
|
|
2965 }
|
|
2966
|
|
2967 #endregion
|
|
2968
|
|
2969 #region MapDataTableToDictionary
|
|
2970
|
|
2971 public IDictionary MapDataTableToDictionary(
|
|
2972 DataTable sourceTable,
|
|
2973 IDictionary destDictionary,
|
|
2974 NameOrIndexParameter keyFieldNameOrIndex,
|
|
2975 Type destObjectType,
|
|
2976 params object[] parameters)
|
|
2977 {
|
|
2978 MapSourceListToDestinationList(
|
|
2979 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
2980 CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
|
|
2981 parameters);
|
|
2982
|
|
2983 return destDictionary;
|
|
2984 }
|
|
2985
|
|
2986 public Hashtable MapDataTableToDictionary(
|
|
2987 DataTable sourceTable,
|
|
2988 NameOrIndexParameter keyFieldNameOrIndex,
|
|
2989 Type destObjectType,
|
|
2990 params object[] parameters)
|
|
2991 {
|
|
2992 Hashtable destDictionary = new Hashtable();
|
|
2993
|
|
2994 MapSourceListToDestinationList(
|
|
2995 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
2996 CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
|
|
2997 parameters);
|
|
2998
|
|
2999 return destDictionary;
|
|
3000 }
|
|
3001
|
|
3002 public IDictionary<TK,T> MapDataTableToDictionary<TK,T>(
|
|
3003 DataTable sourceTable,
|
|
3004 IDictionary<TK,T> destDictionary,
|
|
3005 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3006 params object[] parameters)
|
|
3007 {
|
|
3008 MapSourceListToDestinationList(
|
|
3009 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
3010 CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
|
|
3011 parameters);
|
|
3012
|
|
3013 return destDictionary;
|
|
3014 }
|
|
3015
|
|
3016 public Dictionary<TK,T> MapDataTableToDictionary<TK,T>(
|
|
3017 DataTable sourceTable,
|
|
3018 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3019 params object[] parameters)
|
|
3020 {
|
|
3021 Dictionary<TK,T> destDictionary = new Dictionary<TK,T>();
|
|
3022
|
|
3023 MapSourceListToDestinationList(
|
|
3024 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
3025 CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
|
|
3026 parameters);
|
|
3027
|
|
3028 return destDictionary;
|
|
3029 }
|
|
3030
|
|
3031 #endregion
|
|
3032
|
|
3033 #region MapDataTableToDictionary (Index)
|
|
3034
|
|
3035 public IDictionary MapDataTableToDictionary(
|
|
3036 DataTable sourceTable,
|
|
3037 IDictionary destDictionary,
|
|
3038 MapIndex index,
|
|
3039 Type destObjectType,
|
|
3040 params object[] parameters)
|
|
3041 {
|
|
3042 MapSourceListToDestinationList(
|
|
3043 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
3044 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
|
|
3045 parameters);
|
|
3046
|
|
3047 return destDictionary;
|
|
3048 }
|
|
3049
|
|
3050 public Hashtable MapDataTableToDictionary(
|
|
3051 DataTable sourceTable,
|
|
3052 MapIndex index,
|
|
3053 Type destObjectType,
|
|
3054 params object[] parameters)
|
|
3055 {
|
|
3056 Hashtable destDictionary = new Hashtable();
|
|
3057
|
|
3058 MapSourceListToDestinationList(
|
|
3059 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
3060 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
|
|
3061 parameters);
|
|
3062
|
|
3063 return destDictionary;
|
|
3064 }
|
|
3065
|
|
3066 public IDictionary<CompoundValue,T> MapDataTableToDictionary<T>(
|
|
3067 DataTable sourceTable,
|
|
3068 IDictionary<CompoundValue,T> destDictionary,
|
|
3069 MapIndex index,
|
|
3070 params object[] parameters)
|
|
3071 {
|
|
3072 MapSourceListToDestinationList(
|
|
3073 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
3074 CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
|
|
3075 parameters);
|
|
3076
|
|
3077 return destDictionary;
|
|
3078 }
|
|
3079
|
|
3080 public Dictionary<CompoundValue,T> MapDataTableToDictionary<T>(
|
|
3081 DataTable sourceTable,
|
|
3082 MapIndex index,
|
|
3083 params object[] parameters)
|
|
3084 {
|
|
3085 Dictionary<CompoundValue,T> destDictionary = new Dictionary<CompoundValue,T>();
|
|
3086
|
|
3087 MapSourceListToDestinationList(
|
|
3088 CreateDataTableMapper (sourceTable, DataRowVersion.Default),
|
|
3089 CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
|
|
3090 parameters);
|
|
3091
|
|
3092 return destDictionary;
|
|
3093 }
|
|
3094
|
|
3095 #endregion
|
|
3096
|
|
3097 #endif
|
|
3098
|
|
3099 #endregion
|
|
3100
|
|
3101 #region DataReader
|
|
3102
|
|
3103 #region MapDataReaderToList
|
|
3104
|
|
3105 public virtual IList MapDataReaderToList(
|
|
3106 IDataReader reader,
|
|
3107 IList list,
|
|
3108 Type destObjectType,
|
|
3109 params object[] parameters)
|
|
3110 {
|
|
3111 MapSourceListToDestinationList(
|
|
3112 CreateDataReaderListMapper(reader),
|
|
3113 CreateObjectListMapper (list, GetObjectMapper(destObjectType)),
|
|
3114 parameters);
|
|
3115
|
|
3116 return list;
|
|
3117 }
|
|
3118
|
|
3119 public IList MapDataReaderToList(
|
|
3120 IDataReader reader,
|
|
3121 Type destObjectType,
|
|
3122 params object[] parameters)
|
|
3123 {
|
|
3124 IList list = new List<object>();
|
|
3125
|
|
3126 MapSourceListToDestinationList(
|
|
3127 CreateDataReaderListMapper(reader),
|
|
3128 CreateObjectListMapper (list, GetObjectMapper(destObjectType)),
|
|
3129 parameters);
|
|
3130
|
|
3131 return list;
|
|
3132 }
|
|
3133
|
|
3134 //NOTE changed to virtual
|
|
3135 public virtual IList<T> MapDataReaderToList<T>(
|
|
3136 IDataReader reader,
|
|
3137 IList<T> list,
|
|
3138 params object[] parameters)
|
|
3139 {
|
|
3140 MapSourceListToDestinationList(
|
|
3141 CreateDataReaderListMapper(reader),
|
|
3142 CreateObjectListMapper ((IList)list, GetObjectMapper(typeof(T))),
|
|
3143 parameters);
|
|
3144
|
|
3145 return list;
|
|
3146 }
|
|
3147
|
|
3148 public List<T> MapDataReaderToList<T>(
|
|
3149 IDataReader reader,
|
|
3150 params object[] parameters)
|
|
3151 {
|
|
3152 List<T> list = new List<T>();
|
|
3153
|
|
3154 MapSourceListToDestinationList(
|
|
3155 CreateDataReaderListMapper(reader),
|
|
3156 CreateObjectListMapper (list, GetObjectMapper(typeof(T))),
|
|
3157 parameters);
|
|
3158
|
|
3159 return list;
|
|
3160 }
|
|
3161
|
|
3162 #endregion
|
|
3163
|
|
3164 #region MapDataReaderToScalarList
|
|
3165
|
|
3166 public IList MapDataReaderToScalarList(
|
|
3167 IDataReader reader,
|
|
3168 NameOrIndexParameter nameOrIndex,
|
|
3169 IList list,
|
|
3170 Type type)
|
|
3171 {
|
|
3172 MapSourceListToDestinationList(
|
|
3173 CreateDataReaderListMapper(reader, nameOrIndex),
|
|
3174 CreateScalarDestinationListMapper(list, type),
|
|
3175 null);
|
|
3176
|
|
3177 return list;
|
|
3178 }
|
|
3179
|
|
3180 public IList MapDataReaderToScalarList(
|
|
3181 IDataReader reader,
|
|
3182 NameOrIndexParameter nameOrIndex,
|
|
3183 Type type)
|
|
3184 {
|
|
3185 IList list = new List<object>();
|
|
3186
|
|
3187 MapSourceListToDestinationList(
|
|
3188 CreateDataReaderListMapper(reader, nameOrIndex),
|
|
3189 CreateScalarDestinationListMapper(list, type),
|
|
3190 null);
|
|
3191
|
|
3192 return list;
|
|
3193 }
|
|
3194
|
|
3195 public IList<T> MapDataReaderToScalarList<T>(
|
|
3196 IDataReader reader,
|
|
3197 NameOrIndexParameter nameOrIndex,
|
|
3198 IList<T> list)
|
|
3199 {
|
|
3200 MapSourceListToDestinationList(
|
|
3201 CreateDataReaderListMapper(reader, nameOrIndex),
|
|
3202 CreateScalarDestinationListMapper(list),
|
|
3203 null);
|
|
3204
|
|
3205 return list;
|
|
3206 }
|
|
3207
|
|
3208 public List<T> MapDataReaderToScalarList<T>(
|
|
3209 IDataReader reader,
|
|
3210 NameOrIndexParameter nameOrIndex)
|
|
3211 {
|
|
3212 List<T> list = new List<T>();
|
|
3213
|
|
3214 MapSourceListToDestinationList(
|
|
3215 CreateDataReaderListMapper(reader, nameOrIndex),
|
|
3216 CreateScalarDestinationListMapper(list),
|
|
3217 null);
|
|
3218
|
|
3219 return list;
|
|
3220 }
|
|
3221
|
|
3222 #endregion
|
|
3223
|
|
3224 #region MapDataReaderToDataTable
|
|
3225
|
|
3226 #if !SILVERLIGHT
|
|
3227
|
|
3228 public DataTable MapDataReaderToDataTable(
|
|
3229 IDataReader reader,
|
|
3230 DataTable destTable)
|
|
3231 {
|
|
3232 MapSourceListToDestinationList(
|
|
3233 CreateDataReaderListMapper(reader),
|
|
3234 CreateDataTableMapper (destTable, DataRowVersion.Default),
|
|
3235 null);
|
|
3236
|
|
3237 return destTable;
|
|
3238 }
|
|
3239
|
|
3240 [SuppressMessage("Microsoft.Globalization", "CA1306:SetLocaleForDataTypes")]
|
|
3241 public DataTable MapDataReaderToDataTable(IDataReader reader)
|
|
3242 {
|
|
3243 DataTable destTable = new DataTable();
|
|
3244
|
|
3245 MapSourceListToDestinationList(
|
|
3246 CreateDataReaderListMapper(reader),
|
|
3247 CreateDataTableMapper (destTable, DataRowVersion.Default),
|
|
3248 null);
|
|
3249
|
|
3250 return destTable;
|
|
3251 }
|
|
3252
|
|
3253 #endif
|
|
3254
|
|
3255 #endregion
|
|
3256
|
|
3257 #region MapDataReaderToDictionary
|
|
3258
|
|
3259 public IDictionary MapDataReaderToDictionary(
|
|
3260 IDataReader reader,
|
|
3261 IDictionary destDictionary,
|
|
3262 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3263 Type destObjectType,
|
|
3264 params object[] parameters)
|
|
3265 {
|
|
3266 MapSourceListToDestinationList(
|
|
3267 CreateDataReaderListMapper(reader),
|
|
3268 CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
|
|
3269 parameters);
|
|
3270
|
|
3271 return destDictionary;
|
|
3272 }
|
|
3273
|
|
3274 public IDictionary MapDataReaderToDictionary(
|
|
3275 IDataReader reader,
|
|
3276 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3277 Type destObjectType,
|
|
3278 params object[] parameters)
|
|
3279 {
|
|
3280 IDictionary dest = new Dictionary<object,object>();
|
|
3281
|
|
3282 MapSourceListToDestinationList(
|
|
3283 CreateDataReaderListMapper(reader),
|
|
3284 CreateDictionaryListMapper(dest, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
|
|
3285 parameters);
|
|
3286
|
|
3287 return dest;
|
|
3288 }
|
|
3289
|
|
3290 public IDictionary<TK,T> MapDataReaderToDictionary<TK,T>(
|
|
3291 IDataReader reader,
|
|
3292 IDictionary<TK,T> destDictionary,
|
|
3293 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3294 Type destObjectType,
|
|
3295 params object[] parameters)
|
|
3296 {
|
|
3297 MapSourceListToDestinationList(
|
|
3298 CreateDataReaderListMapper (reader),
|
|
3299 CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
|
|
3300 parameters);
|
|
3301
|
|
3302 return destDictionary;
|
|
3303 }
|
|
3304
|
|
3305 public IDictionary<TK,T> MapDataReaderToDictionary<TK,T>(
|
|
3306 IDataReader reader,
|
|
3307 IDictionary<TK,T> destDictionary,
|
|
3308 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3309 params object[] parameters)
|
|
3310 {
|
|
3311 MapSourceListToDestinationList(
|
|
3312 CreateDataReaderListMapper (reader),
|
|
3313 CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
|
|
3314 parameters);
|
|
3315
|
|
3316 return destDictionary;
|
|
3317 }
|
|
3318
|
|
3319 public Dictionary<TK,T> MapDataReaderToDictionary<TK,T>(
|
|
3320 IDataReader reader,
|
|
3321 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3322 params object[] parameters)
|
|
3323 {
|
|
3324 Dictionary<TK,T> dest = new Dictionary<TK,T>();
|
|
3325
|
|
3326 MapSourceListToDestinationList(
|
|
3327 CreateDataReaderListMapper (reader),
|
|
3328 CreateDictionaryListMapper<TK,T>(dest, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
|
|
3329 parameters);
|
|
3330
|
|
3331 return dest;
|
|
3332 }
|
|
3333
|
|
3334 #endregion
|
|
3335
|
|
3336 #region MapDataReaderToDictionary (Index)
|
|
3337
|
|
3338 public IDictionary MapDataReaderToDictionary(
|
|
3339 IDataReader reader,
|
|
3340 IDictionary destDictionary,
|
|
3341 MapIndex index,
|
|
3342 Type destObjectType,
|
|
3343 params object[] parameters)
|
|
3344 {
|
|
3345 MapSourceListToDestinationList(
|
|
3346 CreateDataReaderListMapper(reader),
|
|
3347 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
|
|
3348 parameters);
|
|
3349
|
|
3350 return destDictionary;
|
|
3351 }
|
|
3352
|
|
3353 public IDictionary MapDataReaderToDictionary(
|
|
3354 IDataReader reader,
|
|
3355 MapIndex index,
|
|
3356 Type destObjectType,
|
|
3357 params object[] parameters)
|
|
3358 {
|
|
3359 IDictionary destDictionary = new Dictionary<object,object>();
|
|
3360
|
|
3361 MapSourceListToDestinationList(
|
|
3362 CreateDataReaderListMapper(reader),
|
|
3363 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
|
|
3364 parameters);
|
|
3365
|
|
3366 return destDictionary;
|
|
3367 }
|
|
3368
|
|
3369 public IDictionary<CompoundValue,T> MapDataReaderToDictionary<T>(
|
|
3370 IDataReader reader,
|
|
3371 IDictionary<CompoundValue,T> destDictionary,
|
|
3372 MapIndex index,
|
|
3373 Type destObjectType,
|
|
3374 params object[] parameters)
|
|
3375 {
|
|
3376 MapSourceListToDestinationList(
|
|
3377 CreateDataReaderListMapper(reader),
|
|
3378 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
|
|
3379 parameters);
|
|
3380
|
|
3381 return destDictionary;
|
|
3382 }
|
|
3383
|
|
3384 public IDictionary<CompoundValue,T> MapDataReaderToDictionary<T>(
|
|
3385 IDataReader reader,
|
|
3386 IDictionary<CompoundValue,T> destDictionary,
|
|
3387 MapIndex index,
|
|
3388 params object[] parameters)
|
|
3389 {
|
|
3390 MapSourceListToDestinationList(
|
|
3391 CreateDataReaderListMapper(reader),
|
|
3392 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(typeof(T))),
|
|
3393 parameters);
|
|
3394
|
|
3395 return destDictionary;
|
|
3396 }
|
|
3397
|
|
3398 public Dictionary<CompoundValue,T> MapDataReaderToDictionary<T>(
|
|
3399 IDataReader reader,
|
|
3400 MapIndex index,
|
|
3401 params object[] parameters)
|
|
3402 {
|
|
3403 Dictionary<CompoundValue,T> destDictionary = new Dictionary<CompoundValue,T>();
|
|
3404
|
|
3405 MapSourceListToDestinationList(
|
|
3406 CreateDataReaderListMapper (reader),
|
|
3407 CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
|
|
3408 parameters);
|
|
3409
|
|
3410 return destDictionary;
|
|
3411 }
|
|
3412
|
|
3413 #endregion
|
|
3414
|
|
3415 #endregion
|
|
3416
|
|
3417 #region Dictionary
|
|
3418
|
|
3419 #region MapDictionaryToList
|
|
3420
|
|
3421 public IList MapDictionaryToList(
|
|
3422 IDictionary sourceDictionary,
|
|
3423 IList destList,
|
|
3424 Type destObjectType,
|
|
3425 params object[] parameters)
|
|
3426 {
|
|
3427 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3428
|
|
3429 MapSourceListToDestinationList(
|
|
3430 CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
|
|
3431 CreateObjectListMapper(destList, GetObjectMapper(destObjectType)),
|
|
3432 parameters);
|
|
3433
|
|
3434 return destList;
|
|
3435 }
|
|
3436
|
|
3437 public IList MapDictionaryToList(
|
|
3438 IDictionary sourceDictionary,
|
|
3439 Type destObjectType,
|
|
3440 params object[] parameters)
|
|
3441 {
|
|
3442 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3443
|
|
3444 IList destList = new List<object>();
|
|
3445
|
|
3446 MapSourceListToDestinationList(
|
|
3447 CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
|
|
3448 CreateObjectListMapper(destList, GetObjectMapper(destObjectType)),
|
|
3449 parameters);
|
|
3450
|
|
3451 return destList;
|
|
3452 }
|
|
3453
|
|
3454 public List<T> MapDictionaryToList<T>(
|
|
3455 IDictionary sourceDictionary,
|
|
3456 List<T> destList,
|
|
3457 params object[] parameters)
|
|
3458 {
|
|
3459 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3460
|
|
3461 MapSourceListToDestinationList(
|
|
3462 CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
|
|
3463 CreateObjectListMapper(destList, GetObjectMapper(typeof(T))),
|
|
3464 parameters);
|
|
3465
|
|
3466 return destList;
|
|
3467 }
|
|
3468
|
|
3469 public List<T> MapDictionaryToList<T>(
|
|
3470 IDictionary sourceDictionary,
|
|
3471 params object[] parameters)
|
|
3472 {
|
|
3473 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3474
|
|
3475 List<T> destList = new List<T>();
|
|
3476
|
|
3477 MapSourceListToDestinationList(
|
|
3478 CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
|
|
3479 CreateObjectListMapper(destList, GetObjectMapper(typeof(T))),
|
|
3480 parameters);
|
|
3481
|
|
3482 return destList;
|
|
3483 }
|
|
3484
|
|
3485 #endregion
|
|
3486
|
|
3487 #region MapDictionaryToDataTable
|
|
3488
|
|
3489 #if !SILVERLIGHT
|
|
3490
|
|
3491 public DataTable MapDictionaryToDataTable(
|
|
3492 IDictionary sourceDictionary,
|
|
3493 DataTable destTable)
|
|
3494 {
|
|
3495 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3496
|
|
3497 MapSourceListToDestinationList(
|
|
3498 CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
|
|
3499 CreateDataTableMapper (destTable, DataRowVersion.Default),
|
|
3500 null);
|
|
3501
|
|
3502 return destTable;
|
|
3503 }
|
|
3504
|
|
3505 [SuppressMessage("Microsoft.Globalization", "CA1306:SetLocaleForDataTypes")]
|
|
3506 public DataTable MapDictionaryToDataTable(IDictionary sourceDictionary)
|
|
3507 {
|
|
3508 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3509
|
|
3510 DataTable destTable = new DataTable();
|
|
3511
|
|
3512 MapSourceListToDestinationList(
|
|
3513 CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
|
|
3514 CreateDataTableMapper (destTable, DataRowVersion.Default),
|
|
3515 null);
|
|
3516
|
|
3517 return destTable;
|
|
3518 }
|
|
3519
|
|
3520 #endif
|
|
3521
|
|
3522 #endregion
|
|
3523
|
|
3524 #region MapDictionaryToDictionary
|
|
3525
|
|
3526 public IDictionary MapDictionaryToDictionary(
|
|
3527 IDictionary sourceDictionary,
|
|
3528 IDictionary destDictionary,
|
|
3529 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3530 Type destObjectType,
|
|
3531 params object[] parameters)
|
|
3532 {
|
|
3533 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3534
|
|
3535 MapSourceListToDestinationList(
|
|
3536 CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
|
|
3537 CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
|
|
3538 parameters);
|
|
3539
|
|
3540 return destDictionary;
|
|
3541 }
|
|
3542
|
|
3543 public IDictionary MapDictionaryToDictionary(
|
|
3544 IDictionary sourceDictionary,
|
|
3545 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3546 Type destObjectType,
|
|
3547 params object[] parameters)
|
|
3548 {
|
|
3549 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3550
|
|
3551 IDictionary dest = new Dictionary<object,object>();
|
|
3552
|
|
3553 MapSourceListToDestinationList(
|
|
3554 CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
|
|
3555 CreateDictionaryListMapper(dest, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
|
|
3556 parameters);
|
|
3557
|
|
3558 return dest;
|
|
3559 }
|
|
3560
|
|
3561 public IDictionary<TK,T> MapDictionaryToDictionary<TK,T>(
|
|
3562 IDictionary sourceDictionary,
|
|
3563 IDictionary<TK,T> destDictionary,
|
|
3564 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3565 params object[] parameters)
|
|
3566 {
|
|
3567 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3568
|
|
3569 MapSourceListToDestinationList(
|
|
3570 CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
|
|
3571 CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
|
|
3572 parameters);
|
|
3573
|
|
3574 return destDictionary;
|
|
3575 }
|
|
3576
|
|
3577 public Dictionary<TK,T> MapDictionaryToDictionary<TK,T>(
|
|
3578 IDictionary sourceDictionary,
|
|
3579 NameOrIndexParameter keyFieldNameOrIndex,
|
|
3580 params object[] parameters)
|
|
3581 {
|
|
3582 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3583
|
|
3584 Dictionary<TK,T> dest = new Dictionary<TK,T>();
|
|
3585
|
|
3586 MapSourceListToDestinationList(
|
|
3587 CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
|
|
3588 CreateDictionaryListMapper<TK,T>(dest, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
|
|
3589 parameters);
|
|
3590
|
|
3591 return dest;
|
|
3592 }
|
|
3593
|
|
3594 #endregion
|
|
3595
|
|
3596 #region MapDictionaryToDictionary (Index)
|
|
3597
|
|
3598 public IDictionary MapDictionaryToDictionary(
|
|
3599 IDictionary sourceDictionary,
|
|
3600 IDictionary destDictionary,
|
|
3601 MapIndex index,
|
|
3602 Type destObjectType,
|
|
3603 params object[] parameters)
|
|
3604 {
|
|
3605 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3606
|
|
3607 MapSourceListToDestinationList(
|
|
3608 CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
|
|
3609 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
|
|
3610 parameters);
|
|
3611
|
|
3612 return destDictionary;
|
|
3613 }
|
|
3614
|
|
3615 public IDictionary MapDictionaryToDictionary(
|
|
3616 IDictionary sourceDictionary,
|
|
3617 MapIndex index,
|
|
3618 Type destObjectType,
|
|
3619 params object[] parameters)
|
|
3620 {
|
|
3621 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3622
|
|
3623 IDictionary destDictionary = new Dictionary<object,object>();
|
|
3624
|
|
3625 MapSourceListToDestinationList(
|
|
3626 CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
|
|
3627 CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
|
|
3628 parameters);
|
|
3629
|
|
3630 return destDictionary;
|
|
3631 }
|
|
3632
|
|
3633 public IDictionary<CompoundValue,T> MapDictionaryToDictionary<T>(
|
|
3634 IDictionary sourceDictionary,
|
|
3635 IDictionary<CompoundValue,T> destDictionary,
|
|
3636 MapIndex index,
|
|
3637 params object[] parameters)
|
|
3638 {
|
|
3639 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3640
|
|
3641 MapSourceListToDestinationList(
|
|
3642 CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
|
|
3643 CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
|
|
3644 parameters);
|
|
3645
|
|
3646 return destDictionary;
|
|
3647 }
|
|
3648
|
|
3649 public Dictionary<CompoundValue,T> MapDictionaryToDictionary<T>(
|
|
3650 IDictionary sourceDictionary,
|
|
3651 MapIndex index,
|
|
3652 params object[] parameters)
|
|
3653 {
|
|
3654 if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
|
|
3655
|
|
3656 Dictionary<CompoundValue,T> destDictionary = new Dictionary<CompoundValue,T>();
|
|
3657
|
|
3658 MapSourceListToDestinationList(
|
|
3659 CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
|
|
3660 CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
|
|
3661 parameters);
|
|
3662
|
|
3663 return destDictionary;
|
|
3664 }
|
|
3665
|
|
3666 #endregion
|
|
3667
|
|
3668 #endregion
|
|
3669
|
|
3670 #region MapToResultSet
|
|
3671
|
|
3672 public void MapResultSets(MapResultSet[] resultSets)
|
|
3673 {
|
|
3674 var initTable = new Dictionary<object,object>();
|
|
3675 var context = new InitContext();
|
|
3676
|
|
3677 object lastContainer = null;
|
|
3678
|
|
3679 context.MappingSchema = this;
|
|
3680
|
|
3681 try
|
|
3682 {
|
|
3683 PrepareRelarions(resultSets);
|
|
3684
|
|
3685 // Map relations.
|
|
3686 //
|
|
3687 foreach (MapResultSet rs in resultSets)
|
|
3688 {
|
|
3689 if (rs.Relations == null)
|
|
3690 continue;
|
|
3691
|
|
3692 ObjectMapper masterMapper = GetObjectMapper(rs.ObjectType);
|
|
3693
|
|
3694 foreach (MapRelation r in rs.Relations)
|
|
3695 {
|
|
3696 MemberAccessor ma = masterMapper.TypeAccessor[r.ContainerName];
|
|
3697
|
|
3698 if (ma == null)
|
|
3699 throw new MappingException(string.Format(Resources.MapIndex_BadField,
|
|
3700 masterMapper.TypeAccessor.OriginalType.Name, r.ContainerName));
|
|
3701
|
|
3702 // Map.
|
|
3703 //
|
|
3704 var slave = r.SlaveResultSet;
|
|
3705 var slaveMapper = GetObjectMapper(r.SlaveResultSet.ObjectType);
|
|
3706 var indexedLists = rs.GetIndex(this, r.MasterIndex);
|
|
3707
|
|
3708 foreach (object o in slave.List)
|
|
3709 {
|
|
3710 object key = r.SlaveIndex.GetValueOrIndex(slaveMapper, o);
|
|
3711
|
|
3712 if (IsNull(key))
|
|
3713 continue;
|
|
3714
|
|
3715 IList masterList;
|
|
3716
|
|
3717 if (!indexedLists.TryGetValue(key, out masterList))
|
|
3718 continue;
|
|
3719
|
|
3720 foreach (object master in masterList)
|
|
3721 {
|
|
3722 ISupportMapping msm = master as ISupportMapping;
|
|
3723
|
|
3724 if (msm != null)
|
|
3725 {
|
|
3726 if (initTable.ContainsKey(master) == false)
|
|
3727 {
|
|
3728 msm.BeginMapping(context);
|
|
3729 initTable.Add(master, msm);
|
|
3730 }
|
|
3731 }
|
|
3732
|
|
3733 object container = ma.GetValue(master);
|
|
3734
|
|
3735 if (container is IList)
|
|
3736 {
|
|
3737 if (lastContainer != container)
|
|
3738 {
|
|
3739 lastContainer = container;
|
|
3740
|
|
3741 ISupportMapping sm = container as ISupportMapping;
|
|
3742
|
|
3743 if (sm != null)
|
|
3744 {
|
|
3745 if (initTable.ContainsKey(container) == false)
|
|
3746 {
|
|
3747 sm.BeginMapping(context);
|
|
3748 initTable[container] = sm;
|
|
3749 }
|
|
3750 }
|
|
3751 }
|
|
3752
|
|
3753 ((IList)container).Add(o);
|
|
3754 }
|
|
3755 else
|
|
3756 {
|
|
3757 ma.SetValue(master, o);
|
|
3758 }
|
|
3759 }
|
|
3760 }
|
|
3761 }
|
|
3762 }
|
|
3763 }
|
|
3764 finally
|
|
3765 {
|
|
3766 foreach (ISupportMapping si in initTable.Values)
|
|
3767 si.EndMapping(context);
|
|
3768 }
|
|
3769 }
|
|
3770
|
|
3771 public void MapDataReaderToResultSet(
|
|
3772 IDataReader reader,
|
|
3773 MapResultSet[] resultSets)
|
|
3774 {
|
|
3775 if (reader == null) throw new ArgumentNullException("reader");
|
|
3776
|
|
3777 foreach (MapResultSet rs in resultSets)
|
|
3778 {
|
|
3779 MapDataReaderToList(reader, rs.List, rs.ObjectType, rs.Parameters);
|
|
3780
|
|
3781 if (reader.NextResult() == false)
|
|
3782 break;
|
|
3783 }
|
|
3784
|
|
3785 MapResultSets(resultSets);
|
|
3786 }
|
|
3787
|
|
3788 #if !SILVERLIGHT
|
|
3789
|
|
3790 public void MapDataSetToResultSet(
|
|
3791 DataSet dataSet,
|
|
3792 MapResultSet[] resultSets)
|
|
3793 {
|
|
3794 for (int i = 0; i < resultSets.Length && i < dataSet.Tables.Count; i++)
|
|
3795 {
|
|
3796 MapResultSet rs = resultSets[i];
|
|
3797
|
|
3798 MapDataTableToList(dataSet.Tables[i], rs.List, rs.ObjectType, rs.Parameters);
|
|
3799 }
|
|
3800
|
|
3801 MapResultSets(resultSets);
|
|
3802 }
|
|
3803
|
|
3804 #endif
|
|
3805
|
|
3806 public MapResultSet[] Clone(MapResultSet[] resultSets)
|
|
3807 {
|
|
3808 MapResultSet[] output = new MapResultSet[resultSets.Length];
|
|
3809
|
|
3810 for (int i = 0; i < resultSets.Length; i++)
|
|
3811 output[i] = new MapResultSet(resultSets[i]);
|
|
3812
|
|
3813 return output;
|
|
3814 }
|
|
3815
|
|
3816 private static int GetResultCount(MapNextResult[] nextResults)
|
|
3817 {
|
|
3818 int n = nextResults.Length;
|
|
3819
|
|
3820 foreach (MapNextResult nr in nextResults)
|
|
3821 n += GetResultCount(nr.NextResults);
|
|
3822
|
|
3823 return n;
|
|
3824 }
|
|
3825
|
|
3826 private static int GetResultSets(
|
|
3827 int current,
|
|
3828 MapResultSet[] output,
|
|
3829 MapResultSet master,
|
|
3830 MapNextResult[] nextResults)
|
|
3831 {
|
|
3832 foreach (MapNextResult nr in nextResults)
|
|
3833 {
|
|
3834 output[current] = new MapResultSet(nr.ObjectType);
|
|
3835
|
|
3836 master.AddRelation(output[current], nr.SlaveIndex, nr.MasterIndex, nr.ContainerName);
|
|
3837
|
|
3838 current += GetResultSets(current + 1, output, output[current], nr.NextResults);
|
|
3839 }
|
|
3840
|
|
3841 return current;
|
|
3842 }
|
|
3843
|
|
3844 public MapResultSet[] ConvertToResultSet(
|
|
3845 Type masterType,
|
|
3846 params MapNextResult[] nextResults)
|
|
3847 {
|
|
3848 MapResultSet[] output = new MapResultSet[1 + GetResultCount(nextResults)];
|
|
3849
|
|
3850 output[0] = new MapResultSet(masterType);
|
|
3851
|
|
3852 GetResultSets(1, output, output[0], nextResults);
|
|
3853
|
|
3854 return output;
|
|
3855 }
|
|
3856
|
|
3857 private void PrepareRelarions(params MapResultSet[] sets)
|
|
3858 {
|
|
3859 foreach (MapResultSet masterSet in sets)
|
|
3860 {
|
|
3861 if (masterSet.Relations != null)
|
|
3862 continue;
|
|
3863
|
|
3864 foreach (MapResultSet slaveSet in sets)
|
|
3865 {
|
|
3866 bool isSet;
|
|
3867
|
|
3868 List<MapRelationBase> relations
|
|
3869 = MetadataProvider.GetRelations(this, Extensions, masterSet.ObjectType, slaveSet.ObjectType, out isSet);
|
|
3870
|
|
3871 if (!isSet)
|
|
3872 continue;
|
|
3873
|
|
3874 foreach (MapRelationBase relation in relations)
|
|
3875 masterSet.AddRelation(slaveSet, relation);
|
|
3876 }
|
|
3877 }
|
|
3878 }
|
|
3879
|
|
3880 #endregion
|
|
3881
|
|
3882 #region GetObjectMapper
|
|
3883
|
|
3884 public Func<TSource,TDest> GetObjectMapper<TSource,TDest>()
|
|
3885 {
|
|
3886 return new ExpressionMapper<TSource,TDest>(this)
|
|
3887 {
|
|
3888 IncludeComplexMapping = Common.Configuration.ExpressionMapper.IncludeComplexMapping
|
|
3889 }.GetMapper();
|
|
3890 }
|
|
3891
|
|
3892 public Func<TSource,TDest> GetObjectMapper<TSource,TDest>(bool deepCopy)
|
|
3893 {
|
|
3894 return new ExpressionMapper<TSource,TDest>(this)
|
|
3895 {
|
|
3896 DeepCopy = deepCopy,
|
|
3897 IncludeComplexMapping = Common.Configuration.ExpressionMapper.IncludeComplexMapping
|
|
3898 }.GetMapper();
|
|
3899 }
|
|
3900
|
|
3901 public Func<TSource,TDest> GetObjectMapper<TSource,TDest>(bool deepCopy, bool includeComplexMapping)
|
|
3902 {
|
|
3903 return new ExpressionMapper<TSource,TDest>(this)
|
|
3904 {
|
|
3905 DeepCopy = deepCopy,
|
|
3906 IncludeComplexMapping = includeComplexMapping
|
|
3907 }.GetMapper();
|
|
3908 }
|
|
3909
|
|
3910 #endregion
|
|
3911
|
|
3912 #region ConvertParameterValue
|
|
3913
|
|
3914 public virtual object ConvertParameterValue(object value, Type systemType)
|
|
3915 {
|
|
3916 return value;
|
|
3917 }
|
|
3918
|
|
3919 #endregion
|
|
3920 }
|
|
3921 }
|