PageRenderTime 66ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/Source/Mapping/MappingSchema.cs

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