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

/Source/Mapping/MappingSchema.cs

https://gitlab.com/Chermyanin/bltoolkitsourcelearn
C# | 3854 lines | 3007 code | 812 blank | 35 comment | 498 complexity | d901190d2cd114a8961639a97c686c11 MD5 | raw file
  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. return Enum.ToObject(conversionType, ConvertChangeType(value, Enum.GetUnderlyingType(conversionType), false));
  711. if (isNullable)
  712. {
  713. if (TypeHelper.IsNullable(conversionType))
  714. {
  715. // Return a null reference or boxed not null value.
  716. //
  717. return value == null || value is DBNull? null:
  718. ConvertChangeType(value, conversionType.GetGenericArguments()[0]);
  719. }
  720. Type type = conversionType.IsEnum? Enum.GetUnderlyingType(conversionType): conversionType;
  721. switch (Type.GetTypeCode(type))
  722. {
  723. case TypeCode.Boolean: return ConvertToNullableBoolean (value);
  724. case TypeCode.Byte: return ConvertToNullableByte (value);
  725. case TypeCode.Char: return ConvertToNullableChar (value);
  726. case TypeCode.DateTime: return ConvertToNullableDateTime(value);
  727. case TypeCode.Decimal: return ConvertToNullableDecimal (value);
  728. case TypeCode.Double: return ConvertToNullableDouble (value);
  729. case TypeCode.Int16: return ConvertToNullableInt16 (value);
  730. case TypeCode.Int32: return ConvertToNullableInt32 (value);
  731. case TypeCode.Int64: return ConvertToNullableInt64 (value);
  732. case TypeCode.SByte: return ConvertToNullableSByte (value);
  733. case TypeCode.Single: return ConvertToNullableSingle (value);
  734. case TypeCode.UInt16: return ConvertToNullableUInt16 (value);
  735. case TypeCode.UInt32: return ConvertToNullableUInt32 (value);
  736. case TypeCode.UInt64: return ConvertToNullableUInt64 (value);
  737. }
  738. if (typeof(Guid) == conversionType) return ConvertToNullableGuid(value);
  739. if (typeof(DateTimeOffset) == conversionType) return ConvertToNullableDateTimeOffset(value);
  740. if (typeof(TimeSpan) == conversionType) return ConvertToNullableTimeSpan(value);
  741. }
  742. switch (Type.GetTypeCode(conversionType))
  743. {
  744. case TypeCode.Boolean: return ConvertToBoolean (value);
  745. case TypeCode.Byte: return ConvertToByte (value);
  746. case TypeCode.Char: return ConvertToChar (value);
  747. case TypeCode.DateTime: return ConvertToDateTime(value);
  748. case TypeCode.Decimal: return ConvertToDecimal (value);
  749. case TypeCode.Double: return ConvertToDouble (value);
  750. case TypeCode.Int16: return ConvertToInt16 (value);
  751. case TypeCode.Int32: return ConvertToInt32 (value);
  752. case TypeCode.Int64: return ConvertToInt64 (value);
  753. case TypeCode.SByte: return ConvertToSByte (value);
  754. case TypeCode.Single: return ConvertToSingle (value);
  755. case TypeCode.String: return ConvertToString (value);
  756. case TypeCode.UInt16: return ConvertToUInt16 (value);
  757. case TypeCode.UInt32: return ConvertToUInt32 (value);
  758. case TypeCode.UInt64: return ConvertToUInt64 (value);
  759. }
  760. if (typeof(Guid) == conversionType) return ConvertToGuid (value);
  761. if (typeof(Stream) == conversionType) return ConvertToStream (value);
  762. #if !SILVERLIGHT
  763. if (typeof(XmlReader) == conversionType) return ConvertToXmlReader (value);
  764. if (typeof(XmlDocument) == conversionType) return ConvertToXmlDocument (value);
  765. if (typeof(XElement) == conversionType) return ConvertToXElement (value);
  766. #endif
  767. if (typeof(byte[]) == conversionType) return ConvertToByteArray (value);
  768. if (typeof(Binary) == conversionType) return ConvertToLinqBinary (value);
  769. if (typeof(DateTimeOffset) == conversionType) return ConvertToDateTimeOffset(value);
  770. if (typeof(char[]) == conversionType) return ConvertToCharArray (value);
  771. if (typeof(TimeSpan) == conversionType) return ConvertToTimeSpan (value);
  772. #if !SILVERLIGHT
  773. if (typeof(SqlInt32) == conversionType) return ConvertToSqlInt32 (value);
  774. if (typeof(SqlString) == conversionType) return ConvertToSqlString (value);
  775. if (typeof(SqlDecimal) == conversionType) return ConvertToSqlDecimal (value);
  776. if (typeof(SqlDateTime) == conversionType) return ConvertToSqlDateTime (value);
  777. if (typeof(SqlBoolean) == conversionType) return ConvertToSqlBoolean (value);
  778. if (typeof(SqlMoney) == conversionType) return ConvertToSqlMoney (value);
  779. if (typeof(SqlGuid) == conversionType) return ConvertToSqlGuid (value);
  780. if (typeof(SqlDouble) == conversionType) return ConvertToSqlDouble (value);
  781. if (typeof(SqlByte) == conversionType) return ConvertToSqlByte (value);
  782. if (typeof(SqlInt16) == conversionType) return ConvertToSqlInt16 (value);
  783. if (typeof(SqlInt64) == conversionType) return ConvertToSqlInt64 (value);
  784. if (typeof(SqlSingle) == conversionType) return ConvertToSqlSingle (value);
  785. if (typeof(SqlBinary) == conversionType) return ConvertToSqlBinary (value);
  786. if (typeof(SqlBytes) == conversionType) return ConvertToSqlBytes (value);
  787. if (typeof(SqlChars) == conversionType) return ConvertToSqlChars (value);
  788. if (typeof(SqlXml) == conversionType) return ConvertToSqlXml (value);
  789. #endif
  790. return System.Convert.ChangeType(value, conversionType, Thread.CurrentThread.CurrentCulture);
  791. }
  792. #endregion
  793. #endregion
  794. #region Factory Members
  795. public virtual DataReaderMapper CreateDataReaderMapper(IDataReader dataReader)
  796. {
  797. return new DataReaderMapper(this, dataReader);
  798. }
  799. public virtual DataReaderListMapper CreateDataReaderListMapper(IDataReader reader)
  800. {
  801. return new DataReaderListMapper(CreateDataReaderMapper(reader));
  802. }
  803. public virtual DataReaderMapper CreateDataReaderMapper(
  804. IDataReader dataReader,
  805. NameOrIndexParameter nameOrIndex)
  806. {
  807. return new ScalarDataReaderMapper(this, dataReader, nameOrIndex);
  808. }
  809. public virtual DataReaderListMapper CreateDataReaderListMapper(
  810. IDataReader reader,
  811. NameOrIndexParameter nameOrIndex)
  812. {
  813. return new DataReaderListMapper(CreateDataReaderMapper(reader, nameOrIndex));
  814. }
  815. #if !SILVERLIGHT
  816. public virtual DataRowMapper CreateDataRowMapper(
  817. DataRow row,
  818. DataRowVersion version)
  819. {
  820. return new DataRowMapper(row, version);
  821. }
  822. public virtual DataTableMapper CreateDataTableMapper(
  823. DataTable dataTable,
  824. DataRowVersion version)
  825. {
  826. return new DataTableMapper(dataTable, CreateDataRowMapper(null, version));
  827. }
  828. #endif
  829. public virtual DictionaryMapper CreateDictionaryMapper(IDictionary dictionary)
  830. {
  831. return new DictionaryMapper(dictionary);
  832. }
  833. public virtual DictionaryListMapper CreateDictionaryListMapper(
  834. IDictionary dic,
  835. NameOrIndexParameter keyFieldNameOrIndex,
  836. ObjectMapper objectMapper)
  837. {
  838. return new DictionaryListMapper(dic, keyFieldNameOrIndex, objectMapper);
  839. }
  840. public virtual DictionaryIndexListMapper CreateDictionaryListMapper(
  841. IDictionary dic,
  842. MapIndex index,
  843. ObjectMapper objectMapper)
  844. {
  845. return new DictionaryIndexListMapper(dic, index, objectMapper);
  846. }
  847. public virtual DictionaryListMapper<TK,T> CreateDictionaryListMapper<TK,T>(
  848. IDictionary<TK,T> dic,
  849. NameOrIndexParameter keyFieldNameOrIndex,
  850. ObjectMapper objectMapper)
  851. {
  852. return new DictionaryListMapper<TK,T>(dic, keyFieldNameOrIndex, objectMapper);
  853. }
  854. public virtual DictionaryIndexListMapper<T> CreateDictionaryListMapper<T>(
  855. IDictionary<CompoundValue,T> dic,
  856. MapIndex index,
  857. ObjectMapper objectMapper)
  858. {
  859. return new DictionaryIndexListMapper<T>(dic, index, objectMapper);
  860. }
  861. public virtual EnumeratorMapper CreateEnumeratorMapper(IEnumerator enumerator)
  862. {
  863. return new EnumeratorMapper(enumerator);
  864. }
  865. public virtual ObjectListMapper CreateObjectListMapper(IList list, ObjectMapper objectMapper)
  866. {
  867. return new ObjectListMapper(list, objectMapper);
  868. }
  869. public virtual ScalarListMapper CreateScalarListMapper(IList list, Type type)
  870. {
  871. return new ScalarListMapper(list, type);
  872. }
  873. public virtual SimpleDestinationListMapper CreateScalarDestinationListMapper(IList list, Type type)
  874. {
  875. return new SimpleDestinationListMapper(CreateScalarListMapper(list, type));
  876. }
  877. public virtual SimpleSourceListMapper CreateScalarSourceListMapper(IList list, Type type)
  878. {
  879. return new SimpleSourceListMapper(CreateScalarListMapper(list, type));
  880. }
  881. public virtual ScalarListMapper<T> CreateScalarListMapper<T>(IList<T> list)
  882. {
  883. return new ScalarListMapper<T>(this, list);
  884. }
  885. public virtual SimpleDestinationListMapper CreateScalarDestinationListMapper<T>(IList<T> list)
  886. {
  887. return new SimpleDestinationListMapper(CreateScalarListMapper<T>(list));
  888. }
  889. #endregion
  890. #region GetNullValue
  891. public virtual object GetNullValue(Type type)
  892. {
  893. return TypeAccessor.GetNullValue(type);
  894. }
  895. public virtual bool IsNull(object value)
  896. {
  897. return TypeAccessor.IsNull(value);
  898. }
  899. #endregion
  900. #region GetMapValues
  901. private readonly Dictionary<Type,MapValue[]> _mapValues = new Dictionary<Type,MapValue[]>();
  902. public virtual MapValue[] GetMapValues([JetBrains.Annotations.NotNull] Type type)
  903. {
  904. if (type == null) throw new ArgumentNullException("type");
  905. lock (_mapValues)
  906. {
  907. MapValue[] mapValues;
  908. if (_mapValues.TryGetValue(type, out mapValues))
  909. return mapValues;
  910. var typeExt = TypeExtension.GetTypeExtension(type, Extensions);
  911. bool isSet;
  912. mapValues = MetadataProvider.GetMapValues(typeExt, type, out isSet);
  913. _mapValues.Add(type, mapValues);
  914. return mapValues;
  915. }
  916. }
  917. private readonly Dictionary<MemberAccessor, MapValue[]> _memberMapValues = new Dictionary<MemberAccessor, MapValue[]>();
  918. private Type GetMapValueType(MapValue[] mapValues)
  919. {
  920. if (mapValues != null)
  921. {
  922. var value = mapValues.SelectMany(mv => mv.MapValues).FirstOrDefault();
  923. if (value != null)
  924. {
  925. return value.GetType();
  926. }
  927. }
  928. return null;
  929. }
  930. public virtual MapValue[] GetMapValues([JetBrains.Annotations.NotNull] MemberAccessor memberAccessor)
  931. {
  932. if (memberAccessor == null) throw new ArgumentNullException("memberAccessor");
  933. lock (_memberMapValues)
  934. {
  935. MapValue[] mapValues;
  936. if (_memberMapValues.TryGetValue(memberAccessor, out mapValues))
  937. return mapValues;
  938. var typeExt = TypeExtension.GetTypeExtension(memberAccessor.Type, Extensions);
  939. bool isSet;
  940. mapValues = MetadataProvider.GetMapValues(typeExt, memberAccessor, out isSet);
  941. _memberMapValues.Add(memberAccessor, mapValues);
  942. return mapValues;
  943. }
  944. }
  945. #endregion
  946. #region GetDefaultValue
  947. private readonly Dictionary<Type,object> _defaultValues = new Dictionary<Type,object>();
  948. public virtual object GetDefaultValue([JetBrains.Annotations.NotNull] Type type)
  949. {
  950. if (type == null) throw new ArgumentNullException("type");
  951. lock (_defaultValues)
  952. {
  953. object defaultValue;
  954. if (_defaultValues.TryGetValue(type, out defaultValue))
  955. return defaultValue;
  956. var typeExt = TypeExtension.GetTypeExtension(type, Extensions);
  957. bool isSet;
  958. defaultValue = MetadataProvider.GetDefaultValue(this, typeExt, type, out isSet);
  959. _defaultValues.Add(type, defaultValue = TypeExtension.ChangeType(defaultValue, type));
  960. return defaultValue;
  961. }
  962. }
  963. #endregion
  964. #region GetDataSource, GetDataDestination
  965. [CLSCompliant(false)]
  966. public virtual IMapDataSource GetDataSource(object obj)
  967. {
  968. if (obj == null) throw new ArgumentNullException("obj");
  969. if (obj is IMapDataSource)
  970. return (IMapDataSource)obj;
  971. if (obj is IDataReader)
  972. return CreateDataReaderMapper((IDataReader)obj);
  973. #if !SILVERLIGHT
  974. if (obj is DataRow)
  975. return CreateDataRowMapper((DataRow)obj, DataRowVersion.Default);
  976. if (obj is DataRowView)
  977. return CreateDataRowMapper(
  978. ((DataRowView)obj).Row,
  979. ((DataRowView)obj).RowVersion);
  980. if (obj is DataTable)
  981. return CreateDataRowMapper(((DataTable)(obj)).Rows[0], DataRowVersion.Default);
  982. #endif
  983. if (obj is IDictionary)
  984. return CreateDictionaryMapper((IDictionary)obj);
  985. return GetObjectMapper(obj.GetType());
  986. }
  987. [CLSCompliant(false)]
  988. public virtual IMapDataDestination GetDataDestination(object obj)
  989. {
  990. if (obj == null) throw new ArgumentNullException("obj");
  991. if (obj is IMapDataDestination)
  992. return (IMapDataDestination)obj;
  993. #if !SILVERLIGHT
  994. if (obj is DataRow)
  995. return CreateDataRowMapper((DataRow)obj, DataRowVersion.Default);
  996. if (obj is DataRowView)
  997. return CreateDataRowMapper(
  998. ((DataRowView)obj).Row,
  999. ((DataRowView)obj).RowVersion);
  1000. if (obj is DataTable)
  1001. {
  1002. DataTable dt = obj as DataTable;
  1003. DataRow dr = dt.NewRow();
  1004. dt.Rows.Add(dr);
  1005. return CreateDataRowMapper(dr, DataRowVersion.Default);
  1006. }
  1007. #endif
  1008. if (obj is IDictionary)
  1009. return CreateDictionaryMapper((IDictionary)obj);
  1010. return GetObjectMapper(obj.GetType());
  1011. }
  1012. [CLSCompliant(false)]
  1013. public virtual IMapDataSourceList GetDataSourceList(object obj)
  1014. {
  1015. if (obj == null) throw new ArgumentNullException("obj");
  1016. if (obj is IMapDataSourceList)
  1017. return (IMapDataSourceList)obj;
  1018. if (obj is IDataReader)
  1019. return CreateDataReaderListMapper((IDataReader)obj);
  1020. Type type = obj.GetType().GetElementType();
  1021. return TypeHelper.IsScalar(type)?
  1022. (IMapDataSourceList)CreateScalarSourceListMapper((IList)obj, type):
  1023. CreateObjectListMapper((IList)obj, CreateObjectMapper(type));
  1024. }
  1025. [CLSCompliant(false)]
  1026. public virtual IMapDataDestinationList GetDataDestinationList(object obj)
  1027. {
  1028. if (obj == null) throw new ArgumentNullException("obj");
  1029. if (obj is IMapDataDestinationList)
  1030. return (IMapDataDestinationList)obj;
  1031. Type type = obj.GetType().GetElementType();
  1032. return TypeHelper.IsScalar(type)?
  1033. (IMapDataDestinationList)CreateScalarDestinationListMapper((IList)obj, type):
  1034. CreateObjectListMapper((IList)obj, CreateObjectMapper(type));
  1035. }
  1036. #endregion
  1037. #region ValueMapper
  1038. [CLSCompliant(false)]
  1039. public virtual IValueMapper DefaultValueMapper
  1040. {
  1041. get { return ValueMapping.DefaultMapper; }
  1042. }
  1043. internal readonly Dictionary<Type,IValueMapper> SameTypeMappers = new Dictionary<Type,IValueMapper>();
  1044. internal readonly Dictionary<KeyValue,IValueMapper> DifferentTypeMappers = new Dictionary<KeyValue,IValueMapper>();
  1045. [CLSCompliant(false)]
  1046. public void SetValueMapper(
  1047. Type sourceType,
  1048. Type destType,
  1049. IValueMapper mapper)
  1050. {
  1051. if (sourceType == null) sourceType = typeof(object);
  1052. if (destType == null) destType = typeof(object);
  1053. if (sourceType == destType)
  1054. {
  1055. lock (SameTypeMappers)
  1056. {
  1057. if (mapper == null)
  1058. SameTypeMappers.Remove(sourceType);
  1059. else if (SameTypeMappers.ContainsKey(sourceType))
  1060. SameTypeMappers[sourceType] = mapper;
  1061. else
  1062. SameTypeMappers.Add(sourceType, mapper);
  1063. }
  1064. }
  1065. else
  1066. {
  1067. KeyValue key = new KeyValue(sourceType, destType);
  1068. lock (DifferentTypeMappers)
  1069. {
  1070. if (mapper == null)
  1071. DifferentTypeMappers.Remove(key);
  1072. else if (DifferentTypeMappers.ContainsKey(key))
  1073. DifferentTypeMappers[key] = mapper;
  1074. else
  1075. DifferentTypeMappers.Add(key, mapper);
  1076. }
  1077. }
  1078. }
  1079. [CLSCompliant(false)]
  1080. protected internal virtual IValueMapper GetValueMapper(
  1081. Type sourceType,
  1082. Type destType)
  1083. {
  1084. return ValueMapping.GetMapper(sourceType, destType);
  1085. }
  1086. [CLSCompliant(false)]
  1087. internal protected IValueMapper[] GetValueMappers(
  1088. IMapDataSource source,
  1089. IMapDataDestination dest,
  1090. int[] index)
  1091. {
  1092. IValueMapper[] mappers = new IValueMapper[index.Length];
  1093. for (int i = 0; i < index.Length; i++)
  1094. {
  1095. int n = index[i];
  1096. if (n < 0)
  1097. continue;
  1098. if (!source.SupportsTypedValues(i) || !dest.SupportsTypedValues(n))
  1099. {
  1100. mappers[i] = DefaultValueMapper;
  1101. continue;
  1102. }
  1103. Type sourceType = source.GetFieldType(i);
  1104. Type destType = dest. GetFieldType(n);
  1105. if (sourceType == null) sourceType = typeof(object);
  1106. if (destType == null) destType = typeof(object);
  1107. IValueMapper t;
  1108. if (sourceType == destType)
  1109. {
  1110. lock (SameTypeMappers)
  1111. if (!SameTypeMappers.TryGetValue(sourceType, out t))
  1112. SameTypeMappers.Add(sourceType, t = GetValueMapper(sourceType, destType));
  1113. }
  1114. else
  1115. {
  1116. var key = new KeyValue(sourceType, destType);
  1117. lock (DifferentTypeMappers)
  1118. if (!DifferentTypeMappers.TryGetValue(key, out t))
  1119. DifferentTypeMappers[key] = t = GetValueMapper(sourceType, destType);
  1120. }
  1121. mappers[i] = t;
  1122. }
  1123. return mappers;
  1124. }
  1125. #endregion
  1126. #region Base Mapping
  1127. [CLSCompliant(false)]
  1128. internal protected static int[] GetIndex(
  1129. IMapDataSource source,
  1130. IMapDataDestination dest)
  1131. {
  1132. int count = source.Count;
  1133. int[] index = new int[count];
  1134. for (int i = 0; i < count; i++)
  1135. index[i] = dest.GetOrdinal(source.GetName(i));
  1136. return index;
  1137. }
  1138. [CLSCompliant(false), Obsolete]
  1139. protected static void MapInternal(
  1140. IMapDataSource source, object sourceObject,
  1141. IMapDataDestination dest, object destObject,
  1142. int[] index)
  1143. {
  1144. for (int i = 0; i < index.Length; i++)
  1145. {
  1146. int n = index[i];
  1147. if (n >= 0)
  1148. dest.SetValue(destObject, n, source.GetValue(sourceObject, i));
  1149. }
  1150. }
  1151. [CLSCompliant(false)]
  1152. internal protected static void MapInternal(
  1153. IMapDataSource source, object sourceObject,
  1154. IMapDataDestination dest, object destObject,
  1155. int[] index,
  1156. IValueMapper[] mappers)
  1157. {
  1158. for (int i = 0; i < index.Length; i++)
  1159. {
  1160. int n = index[i];
  1161. if (n >= 0)
  1162. mappers[i].Map(source, sourceObject, i, dest, destObject, n);
  1163. }
  1164. }
  1165. [CLSCompliant(false)]
  1166. protected virtual void MapInternal(
  1167. InitContext initContext,
  1168. IMapDataSource source, object sourceObject,
  1169. IMapDataDestination dest, object destObject,
  1170. params object[] parameters)
  1171. {
  1172. ISupportMapping smSource = sourceObject as ISupportMapping;
  1173. ISupportMapping smDest = destObject as ISupportMapping;
  1174. if (smSource != null)
  1175. {
  1176. if (initContext == null)
  1177. {
  1178. initContext = new InitContext();
  1179. initContext.MappingSchema = this;
  1180. initContext.DataSource = source;
  1181. initContext.SourceObject = sourceObject;
  1182. initContext.ObjectMapper = dest as ObjectMapper;
  1183. initContext.Parameters = parameters;
  1184. }
  1185. initContext.IsSource = true;
  1186. smSource.BeginMapping(initContext);
  1187. initContext.IsSource = false;
  1188. if (initContext.StopMapping)
  1189. return;
  1190. }
  1191. if (smDest != null)
  1192. {
  1193. if (initContext == null)
  1194. {
  1195. initContext = new InitContext();
  1196. initContext.MappingSchema = this;
  1197. initContext.DataSource = source;
  1198. initContext.SourceObject = sourceObject;
  1199. initContext.ObjectMapper = dest as ObjectMapper;
  1200. initContext.Parameters = parameters;
  1201. }
  1202. smDest.BeginMapping(initContext);
  1203. if (initContext.StopMapping)
  1204. return;
  1205. if (dest != initContext.ObjectMapper && initContext.ObjectMapper != null)
  1206. dest = initContext.ObjectMapper;
  1207. }
  1208. int[] index = GetIndex (source, dest);
  1209. IValueMapper[] mappers = GetValueMappers(source, dest, index);
  1210. MapInternal(source, sourceObject, dest, destObject, index, mappers);
  1211. if (smDest != null)
  1212. smDest.EndMapping(initContext);
  1213. if (smSource != null)
  1214. {
  1215. initContext.IsSource = true;
  1216. smSource.EndMapping(initContext);
  1217. initContext.IsSource = false;
  1218. }
  1219. }
  1220. protected virtual object MapInternal(InitContext initContext)
  1221. {
  1222. object dest = initContext.ObjectMapper.CreateInstance(initContext);
  1223. if (initContext.StopMapping == false)
  1224. {
  1225. MapInternal(initContext,
  1226. initContext.DataSource, initContext.SourceObject,
  1227. initContext.ObjectMapper, dest,
  1228. initContext.Parameters);
  1229. }
  1230. return dest;
  1231. }
  1232. [CLSCompliant(false)]
  1233. public void MapSourceToDestination(
  1234. IMapDataSource source, object sourceObject,
  1235. IMapDataDestination dest, object destObject,
  1236. params object[] parameters)
  1237. {
  1238. MapInternal(null, source, sourceObject, dest, destObject, parameters);
  1239. }
  1240. public void MapSourceToDestination(
  1241. object sourceObject,
  1242. object destObject,
  1243. params object[] parameters)
  1244. {
  1245. IMapDataSource source = GetDataSource (sourceObject);
  1246. IMapDataDestination dest = GetDataDestination(destObject);
  1247. MapInternal(null, source, sourceObject, dest, destObject, parameters);
  1248. }
  1249. private static readonly ObjectMapper _nullMapper = new ObjectMapper();
  1250. private class MapInfo
  1251. {
  1252. public int[] Index;
  1253. public IValueMapper[] Mappers;
  1254. }
  1255. [CLSCompliant(false)]
  1256. public virtual void MapSourceListToDestinationList(
  1257. IMapDataSourceList dataSourceList,
  1258. IMapDataDestinationList dataDestinationList,
  1259. params object[] parameters)
  1260. {
  1261. if (dataSourceList == null) throw new ArgumentNullException("dataSourceList");
  1262. if (dataDestinationList == null) throw new ArgumentNullException("dataDestinationList");
  1263. Dictionary<ObjectMapper,MapInfo> infos = new Dictionary<ObjectMapper,MapInfo>();
  1264. InitContext ctx = new InitContext();
  1265. ctx.MappingSchema = this;
  1266. ctx.Parameters = parameters;
  1267. dataSourceList. InitMapping(ctx); if (ctx.StopMapping) return;
  1268. dataDestinationList.InitMapping(ctx); if (ctx.StopMapping) return;
  1269. int[] index = null;
  1270. IValueMapper[] mappers = null;
  1271. ObjectMapper current = _nullMapper;
  1272. IMapDataDestination dest = dataDestinationList.GetDataDestination(ctx);
  1273. ObjectMapper om = dest as ObjectMapper;
  1274. while (dataSourceList.SetNextDataSource(ctx))
  1275. {
  1276. ctx.ObjectMapper = om;
  1277. ctx.StopMapping = false;
  1278. object destObject = dataDestinationList.GetNextObject(ctx);
  1279. if (ctx.StopMapping) continue;
  1280. ISupportMapping smSource = ctx.SourceObject as ISupportMapping;
  1281. ISupportMapping smDest = destObject as ISupportMapping;
  1282. if (smSource != null)
  1283. {
  1284. ctx.IsSource = true;
  1285. smSource.BeginMapping(ctx);
  1286. ctx.IsSource = false;
  1287. if (ctx.StopMapping)
  1288. continue;
  1289. }
  1290. if (smDest != null)
  1291. {
  1292. smDest.BeginMapping(ctx);
  1293. if (ctx.StopMapping)
  1294. continue;
  1295. }
  1296. IMapDataDestination currentDest = current ?? dest;
  1297. if (current != ctx.ObjectMapper)
  1298. {
  1299. current = ctx.ObjectMapper;
  1300. currentDest = current ?? dest;
  1301. if (current != null)
  1302. {
  1303. MapInfo info;
  1304. if (!infos.TryGetValue(current, out info))
  1305. {
  1306. info = new MapInfo();
  1307. info.Index = GetIndex(ctx.DataSource, currentDest);
  1308. info.Mappers = GetValueMappers(ctx.DataSource, currentDest, info.Index);
  1309. infos.Add(current, info);
  1310. }
  1311. index = info.Index;
  1312. mappers = info.Mappers;
  1313. }
  1314. else
  1315. {
  1316. index = GetIndex(ctx.DataSource, currentDest);
  1317. mappers = GetValueMappers(ctx.DataSource, currentDest, index);
  1318. }
  1319. }
  1320. MapInternal(
  1321. ctx.DataSource,
  1322. ctx.SourceObject,
  1323. currentDest,
  1324. destObject,
  1325. index,
  1326. mappers);
  1327. if (smDest != null)
  1328. smDest.EndMapping(ctx);
  1329. if (smSource != null)
  1330. {
  1331. ctx.IsSource = true;
  1332. smSource.EndMapping(ctx);
  1333. ctx.IsSource = false;
  1334. }
  1335. }
  1336. dataDestinationList.EndMapping(ctx);
  1337. dataSourceList. EndMapping(ctx);
  1338. }
  1339. #endregion
  1340. #region ValueToEnum, EnumToValue
  1341. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  1342. public virtual object MapValueToEnum(object value, Type type)
  1343. {
  1344. if (value == null)
  1345. return GetNullValue(type);
  1346. MapValue[] mapValues = GetMapValues(type);
  1347. var mapValueType = GetMapValueType(mapValues);
  1348. if (mapValueType != null && value.GetType() != mapValueType)
  1349. {
  1350. value = ConvertChangeType(value, mapValueType);
  1351. }
  1352. if (mapValues != null)
  1353. {
  1354. var comp = (IComparable)value;
  1355. foreach (MapValue mv in mapValues)
  1356. foreach (object mapValue in mv.MapValues)
  1357. {
  1358. try
  1359. {
  1360. if (comp.CompareTo(mapValue) == 0)
  1361. return mv.OrigValue;
  1362. }
  1363. catch (ArgumentException ex)
  1364. {
  1365. Debug.WriteLine(ex.Message, MethodBase.GetCurrentMethod().Name);
  1366. }
  1367. }
  1368. }
  1369. InvalidCastException exInvalidCast = null;
  1370. var enumType = TypeHelper.UnwrapNullableType(type);
  1371. try
  1372. {
  1373. value = ConvertChangeType(value, Enum.GetUnderlyingType(enumType));
  1374. if (Enum.IsDefined(enumType, value))
  1375. {
  1376. // Regular (known) enum field w/o explicit mapping defined.
  1377. //
  1378. return Enum.ToObject(enumType, value);
  1379. }
  1380. }
  1381. catch (InvalidCastException ex)
  1382. {
  1383. exInvalidCast = ex;
  1384. }
  1385. // Default value.
  1386. //
  1387. object defaultValue = GetDefaultValue(type);
  1388. if (defaultValue != null)
  1389. return defaultValue;
  1390. if (exInvalidCast != null)
  1391. {
  1392. // Rethrow an InvalidCastException when no default value specified.
  1393. //
  1394. throw exInvalidCast;
  1395. }
  1396. // At this point we have an undefined enum value.
  1397. //
  1398. return Enum.ToObject(enumType, value);
  1399. }
  1400. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  1401. public virtual object MapValueToEnum(object value, MemberAccessor ma)
  1402. {
  1403. if (value == null || value is DBNull)
  1404. return GetNullValue(ma.Type);
  1405. MapValue[] mapValues = GetMapValues(ma);
  1406. var mapValueType = GetMapValueType(mapValues);
  1407. if (mapValueType != null && value.GetType() != mapValueType)
  1408. {
  1409. value = ConvertChangeType(value, mapValueType);
  1410. }
  1411. if (mapValues != null)
  1412. {
  1413. var comp = (IComparable)value;
  1414. foreach (MapValue mv in mapValues)
  1415. foreach (object mapValue in mv.MapValues)
  1416. {
  1417. try
  1418. {
  1419. if (comp.CompareTo(mapValue) == 0)
  1420. return mv.OrigValue;
  1421. }
  1422. catch (ArgumentException ex)
  1423. {
  1424. Debug.WriteLine(ex.Message, MethodBase.GetCurrentMethod().Name);
  1425. }
  1426. }
  1427. }
  1428. InvalidCastException exInvalidCast = null;
  1429. var enumType = TypeHelper.UnwrapNullableType(ma.Type);
  1430. try
  1431. {
  1432. value = ConvertChangeType(value, Enum.GetUnderlyingType(enumType));
  1433. if (Enum.IsDefined(enumType, value))
  1434. {
  1435. // Regular (known) enum field w/o explicit mapping defined.
  1436. //
  1437. return Enum.ToObject(enumType, value);
  1438. }
  1439. }
  1440. catch (InvalidCastException ex)
  1441. {
  1442. exInvalidCast = ex;
  1443. }
  1444. // Default value.
  1445. //
  1446. object defaultValue = GetDefaultValue(ma.Type);
  1447. if (defaultValue != null)
  1448. return defaultValue;
  1449. if (exInvalidCast != null)
  1450. {
  1451. // Rethrow an InvalidCastException when no default value specified.
  1452. //
  1453. throw exInvalidCast;
  1454. }
  1455. // At this point we have an undefined enum value.
  1456. //
  1457. return Enum.ToObject(enumType, value);
  1458. }
  1459. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  1460. public virtual object MapEnumToValue(object value, [JetBrains.Annotations.NotNull] Type type, bool convertToUnderlyingType)
  1461. {
  1462. if (value == null)
  1463. return null;
  1464. if (type == null) throw new ArgumentNullException("type");
  1465. type = value.GetType();
  1466. object nullValue = GetNullValue(type);
  1467. if (nullValue != null)
  1468. {
  1469. IComparable comp = (IComparable)value;
  1470. try
  1471. {
  1472. if (comp.CompareTo(nullValue) == 0)
  1473. return null;
  1474. }
  1475. catch
  1476. {
  1477. }
  1478. }
  1479. MapValue[] mapValues = GetMapValues(type);
  1480. if (mapValues != null)
  1481. {
  1482. IComparable comp = (IComparable)value;
  1483. foreach (MapValue mv in mapValues)
  1484. {
  1485. try
  1486. {
  1487. if (comp.CompareTo(mv.OrigValue) == 0)
  1488. return mv.MapValues[0];
  1489. }
  1490. catch
  1491. {
  1492. }
  1493. }
  1494. }
  1495. return convertToUnderlyingType ?
  1496. System.Convert.ChangeType(value, Enum.GetUnderlyingType(type), Thread.CurrentThread.CurrentCulture) :
  1497. value;
  1498. }
  1499. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  1500. public virtual object MapEnumToValue(object value, [JetBrains.Annotations.NotNull] MemberAccessor memberAccessor, bool convertToUnderlyingType)
  1501. {
  1502. if (value == null)
  1503. return null;
  1504. if (memberAccessor == null) throw new ArgumentNullException("memberAccessor");
  1505. object nullValue = GetNullValue(memberAccessor.Type);
  1506. if (nullValue != null)
  1507. {
  1508. IComparable comp = (IComparable)value;
  1509. try
  1510. {
  1511. if (comp.CompareTo(nullValue) == 0)
  1512. return null;
  1513. }
  1514. catch
  1515. {
  1516. }
  1517. }
  1518. MapValue[] mapValues = GetMapValues(memberAccessor);
  1519. if (mapValues != null)
  1520. {
  1521. IComparable comp = (IComparable)value;
  1522. foreach (MapValue mv in mapValues)
  1523. {
  1524. try
  1525. {
  1526. if (comp.CompareTo(mv.OrigValue) == 0)
  1527. return mv.MapValues[0];
  1528. }
  1529. catch
  1530. {
  1531. }
  1532. }
  1533. }
  1534. var memberAccessorType = TypeHelper.UnwrapNullableType(memberAccessor.Type);
  1535. return convertToUnderlyingType ?
  1536. System.Convert.ChangeType(value, Enum.GetUnderlyingType(memberAccessorType), Thread.CurrentThread.CurrentCulture) :
  1537. value;
  1538. }
  1539. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  1540. public virtual object MapEnumToValue(object value, bool convertToUnderlyingType)
  1541. {
  1542. if (value == null)
  1543. return null;
  1544. return MapEnumToValue(value, value.GetType(), convertToUnderlyingType);
  1545. }
  1546. public object MapEnumToValue(object value)
  1547. {
  1548. return MapEnumToValue(value, false);
  1549. }
  1550. public virtual object MapEnumToValue(object value, Type type)
  1551. {
  1552. return MapEnumToValue(value, type, false);
  1553. }
  1554. public T MapValueToEnum<T>(object value)
  1555. {
  1556. return (T)MapValueToEnum(value, typeof(T));
  1557. }
  1558. #endregion
  1559. #region Object
  1560. #region MapObjectToObject
  1561. public object MapObjectToObject(
  1562. object sourceObject,
  1563. object destObject,
  1564. params object[] parameters)
  1565. {
  1566. if (sourceObject == null) throw new ArgumentNullException("sourceObject");
  1567. if (destObject == null) throw new ArgumentNullException("destObject");
  1568. MapInternal(
  1569. null,
  1570. GetObjectMapper(sourceObject.GetType()), sourceObject,
  1571. GetObjectMapper(destObject. GetType()), destObject,
  1572. parameters);
  1573. return destObject;
  1574. }
  1575. public object MapObjectToObject(
  1576. object sourceObject,
  1577. Type destObjectType,
  1578. params object[] parameters)
  1579. {
  1580. if (sourceObject == null) throw new ArgumentNullException("sourceObject");
  1581. InitContext ctx = new InitContext();
  1582. ctx.MappingSchema = this;
  1583. ctx.DataSource = GetObjectMapper(sourceObject.GetType());
  1584. ctx.SourceObject = sourceObject;
  1585. ctx.ObjectMapper = GetObjectMapper(destObjectType);
  1586. ctx.Parameters = parameters;
  1587. return MapInternal(ctx);
  1588. }
  1589. public T MapObjectToObject<T>(
  1590. object sourceObject,
  1591. params object[] parameters)
  1592. {
  1593. return (T)MapObjectToObject(sourceObject, typeof(T), parameters);
  1594. }
  1595. #endregion
  1596. #region MapObjectToDataRow
  1597. #if !SILVERLIGHT
  1598. public DataRow MapObjectToDataRow(
  1599. object sourceObject,
  1600. DataRow destRow)
  1601. {
  1602. if (sourceObject == null) throw new ArgumentNullException("sourceObject");
  1603. MapInternal(
  1604. null,
  1605. GetObjectMapper (sourceObject.GetType()), sourceObject,
  1606. CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
  1607. null);
  1608. return destRow;
  1609. }
  1610. public DataRow MapObjectToDataRow(
  1611. object sourceObject,
  1612. DataTable destTable)
  1613. {
  1614. if (destTable == null) throw new ArgumentNullException("destTable");
  1615. if (sourceObject == null) throw new ArgumentNullException("sourceObject");
  1616. DataRow destRow = destTable.NewRow();
  1617. destTable.Rows.Add(destRow);
  1618. MapInternal(
  1619. null,
  1620. GetObjectMapper (sourceObject.GetType()), sourceObject,
  1621. CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
  1622. null);
  1623. return destRow;
  1624. }
  1625. #endif
  1626. #endregion
  1627. #region MapObjectToDictionary
  1628. public IDictionary MapObjectToDictionary(
  1629. object sourceObject,
  1630. IDictionary destDictionary)
  1631. {
  1632. if (sourceObject == null) throw new ArgumentNullException("sourceObject");
  1633. MapInternal(
  1634. null,
  1635. GetObjectMapper (sourceObject.GetType()), sourceObject,
  1636. CreateDictionaryMapper(destDictionary), destDictionary,
  1637. null);
  1638. return destDictionary;
  1639. }
  1640. public IDictionary MapObjectToDictionary(object sourceObject)
  1641. {
  1642. if (sourceObject == null) throw new ArgumentNullException("sourceObject");
  1643. ObjectMapper om = GetObjectMapper(sourceObject.GetType());
  1644. var destDictionary = new Dictionary<object,object>(om.Count);
  1645. MapInternal(
  1646. null,
  1647. om, sourceObject,
  1648. CreateDictionaryMapper(destDictionary), destDictionary,
  1649. null);
  1650. return destDictionary;
  1651. }
  1652. #endregion
  1653. #endregion
  1654. #region DataRow
  1655. #if !SILVERLIGHT
  1656. #region MapDataRowToObject
  1657. public object MapDataRowToObject(
  1658. DataRow dataRow,
  1659. object destObject,
  1660. params object[] parameters)
  1661. {
  1662. if (destObject == null) throw new ArgumentNullException("destObject");
  1663. MapInternal(
  1664. null,
  1665. CreateDataRowMapper(dataRow, DataRowVersion.Default), dataRow,
  1666. GetObjectMapper(destObject. GetType()), destObject,
  1667. parameters);
  1668. return destObject;
  1669. }
  1670. public object MapDataRowToObject(
  1671. DataRow dataRow,
  1672. DataRowVersion version,
  1673. object destObject,
  1674. params object[] parameters)
  1675. {
  1676. if (destObject == null) throw new ArgumentNullException("destObject");
  1677. MapInternal(
  1678. null,
  1679. CreateDataRowMapper(dataRow, version), dataRow,
  1680. GetObjectMapper(destObject. GetType()), destObject,
  1681. parameters);
  1682. return destObject;
  1683. }
  1684. public object MapDataRowToObject(
  1685. DataRow dataRow,
  1686. Type destObjectType,
  1687. params object[] parameters)
  1688. {
  1689. InitContext ctx = new InitContext();
  1690. ctx.MappingSchema = this;
  1691. ctx.DataSource = CreateDataRowMapper(dataRow, DataRowVersion.Default);
  1692. ctx.SourceObject = dataRow;
  1693. ctx.ObjectMapper = GetObjectMapper(destObjectType);
  1694. ctx.Parameters = parameters;
  1695. return MapInternal(ctx);
  1696. }
  1697. public object MapDataRowToObject(
  1698. DataRow dataRow,
  1699. DataRowVersion version,
  1700. Type destObjectType,
  1701. params object[] parameters)
  1702. {
  1703. InitContext ctx = new InitContext();
  1704. ctx.MappingSchema = this;
  1705. ctx.DataSource = CreateDataRowMapper(dataRow, version);
  1706. ctx.SourceObject = dataRow;
  1707. ctx.ObjectMapper = GetObjectMapper(destObjectType);
  1708. ctx.Parameters = parameters;
  1709. return MapInternal(ctx);
  1710. }
  1711. public T MapDataRowToObject<T>(
  1712. DataRow dataRow,
  1713. params object[] parameters)
  1714. {
  1715. return (T)MapDataRowToObject(dataRow, typeof(T), parameters);
  1716. }
  1717. public T MapDataRowToObject<T>(
  1718. DataRow dataRow,
  1719. DataRowVersion version,
  1720. params object[] parameters)
  1721. {
  1722. return (T)MapDataRowToObject(dataRow, version, typeof(T), parameters);
  1723. }
  1724. #endregion
  1725. #region MapDataRowToDataRow
  1726. public DataRow MapDataRowToDataRow(
  1727. DataRow sourceRow,
  1728. DataRow destRow)
  1729. {
  1730. MapInternal(
  1731. null,
  1732. CreateDataRowMapper(sourceRow, DataRowVersion.Default), sourceRow,
  1733. CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
  1734. null);
  1735. return destRow;
  1736. }
  1737. public DataRow MapDataRowToDataRow(
  1738. DataRow sourceRow,
  1739. DataRowVersion version,
  1740. DataRow destRow)
  1741. {
  1742. MapInternal(
  1743. null,
  1744. CreateDataRowMapper(sourceRow, version), sourceRow,
  1745. CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
  1746. null);
  1747. return destRow;
  1748. }
  1749. public DataRow MapDataRowToDataRow(
  1750. DataRow sourceRow,
  1751. DataTable destTable)
  1752. {
  1753. if (destTable == null) throw new ArgumentNullException("destTable");
  1754. DataRow destRow = destTable.NewRow();
  1755. destTable.Rows.Add(destRow);
  1756. MapInternal(
  1757. null,
  1758. CreateDataRowMapper(sourceRow, DataRowVersion.Default), sourceRow,
  1759. CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
  1760. null);
  1761. return destRow;
  1762. }
  1763. public DataRow MapDataRowToDataRow(
  1764. DataRow sourceRow,
  1765. DataRowVersion version,
  1766. DataTable destTable)
  1767. {
  1768. if (destTable == null) throw new ArgumentNullException("destTable");
  1769. DataRow destRow = destTable.NewRow();
  1770. destTable.Rows.Add(destRow);
  1771. MapInternal(
  1772. null,
  1773. CreateDataRowMapper(sourceRow, version), sourceRow,
  1774. CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
  1775. null);
  1776. return destRow;
  1777. }
  1778. #endregion
  1779. #region MapDataRowToDictionary
  1780. public IDictionary MapDataRowToDictionary(
  1781. DataRow sourceRow,
  1782. IDictionary destDictionary)
  1783. {
  1784. MapInternal(
  1785. null,
  1786. CreateDataRowMapper (sourceRow, DataRowVersion.Default), sourceRow,
  1787. CreateDictionaryMapper(destDictionary), destDictionary,
  1788. null);
  1789. return destDictionary;
  1790. }
  1791. public Hashtable MapDataRowToDictionary(DataRow sourceRow)
  1792. {
  1793. if (sourceRow == null) throw new ArgumentNullException("sourceRow");
  1794. Hashtable destDictionary = new Hashtable(sourceRow.Table.Columns.Count);
  1795. MapInternal(
  1796. null,
  1797. CreateDataRowMapper (sourceRow, DataRowVersion.Default), sourceRow,
  1798. CreateDictionaryMapper(destDictionary), destDictionary,
  1799. null);
  1800. return destDictionary;
  1801. }
  1802. public IDictionary MapDataRowToDictionary(
  1803. DataRow sourceRow,
  1804. DataRowVersion version,
  1805. IDictionary destDictionary)
  1806. {
  1807. MapInternal(
  1808. null,
  1809. CreateDataRowMapper (sourceRow, version), sourceRow,
  1810. CreateDictionaryMapper(destDictionary), destDictionary,
  1811. null);
  1812. return destDictionary;
  1813. }
  1814. public Hashtable MapDataRowToDictionary(
  1815. DataRow sourceRow,
  1816. DataRowVersion version)
  1817. {
  1818. if (sourceRow == null) throw new ArgumentNullException("sourceRow");
  1819. Hashtable destDictionary = new Hashtable(sourceRow.Table.Columns.Count);
  1820. MapInternal(
  1821. null,
  1822. CreateDataRowMapper (sourceRow, version), sourceRow,
  1823. CreateDictionaryMapper(destDictionary), destDictionary,
  1824. null);
  1825. return destDictionary;
  1826. }
  1827. #endregion
  1828. #endif
  1829. #endregion
  1830. #region DataReader
  1831. #region MapDataReaderToObject
  1832. public object MapDataReaderToObject(
  1833. IDataReader dataReader,
  1834. object destObject,
  1835. params object[] parameters)
  1836. {
  1837. if (destObject == null) throw new ArgumentNullException("destObject");
  1838. MapInternal(
  1839. null,
  1840. CreateDataReaderMapper(dataReader), dataReader,
  1841. GetObjectMapper(destObject. GetType()), destObject,
  1842. parameters);
  1843. return destObject;
  1844. }
  1845. //NOTE changed to virtual
  1846. public virtual object MapDataReaderToObject(
  1847. IDataReader dataReader,
  1848. Type destObjectType,
  1849. params object[] parameters)
  1850. {
  1851. InitContext ctx = new InitContext();
  1852. ctx.MappingSchema = this;
  1853. ctx.DataSource = CreateDataReaderMapper(dataReader);
  1854. ctx.SourceObject = dataReader;
  1855. ctx.ObjectMapper = GetObjectMapper(destObjectType);
  1856. ctx.Parameters = parameters;
  1857. return MapInternal(ctx);
  1858. }
  1859. public T MapDataReaderToObject<T>(
  1860. IDataReader dataReader,
  1861. params object[] parameters)
  1862. {
  1863. return (T)MapDataReaderToObject(dataReader, typeof(T), parameters);
  1864. }
  1865. #endregion
  1866. #region MapDataReaderToDataRow
  1867. #if !SILVERLIGHT
  1868. public DataRow MapDataReaderToDataRow(IDataReader dataReader, DataRow destRow)
  1869. {
  1870. MapInternal(
  1871. null,
  1872. CreateDataReaderMapper(dataReader), dataReader,
  1873. CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
  1874. null);
  1875. return destRow;
  1876. }
  1877. public DataRow MapDataReaderToDataRow(
  1878. IDataReader dataReader,
  1879. DataTable destTable)
  1880. {
  1881. if (destTable == null) throw new ArgumentNullException("destTable");
  1882. DataRow destRow = destTable.NewRow();
  1883. destTable.Rows.Add(destRow);
  1884. MapInternal(
  1885. null,
  1886. CreateDataReaderMapper(dataReader), dataReader,
  1887. CreateDataRowMapper(destRow, DataRowVersion.Default), destRow,
  1888. null);
  1889. return destRow;
  1890. }
  1891. #endif
  1892. #endregion
  1893. #region MapDataReaderToDictionary
  1894. public IDictionary MapDataReaderToDictionary(
  1895. IDataReader dataReader,
  1896. IDictionary destDictionary)
  1897. {
  1898. MapInternal(
  1899. null,
  1900. CreateDataReaderMapper(dataReader), dataReader,
  1901. CreateDictionaryMapper(destDictionary), destDictionary,
  1902. null);
  1903. return destDictionary;
  1904. }
  1905. public IDictionary MapDataReaderToDictionary(IDataReader dataReader)
  1906. {
  1907. if (dataReader == null) throw new ArgumentNullException("dataReader");
  1908. var destDictionary = new Dictionary<object,object>(dataReader.FieldCount);
  1909. MapInternal(
  1910. null,
  1911. CreateDataReaderMapper(dataReader), dataReader,
  1912. CreateDictionaryMapper(destDictionary), destDictionary,
  1913. null);
  1914. return destDictionary;
  1915. }
  1916. #endregion
  1917. #endregion
  1918. #region Dictionary
  1919. #region MapDictionaryToObject
  1920. public object MapDictionaryToObject(
  1921. IDictionary sourceDictionary,
  1922. object destObject,
  1923. params object[] parameters)
  1924. {
  1925. if (destObject == null) throw new ArgumentNullException("destObject");
  1926. MapInternal(
  1927. null,
  1928. CreateDictionaryMapper(sourceDictionary), sourceDictionary,
  1929. GetObjectMapper (destObject. GetType()), destObject,
  1930. parameters);
  1931. return destObject;
  1932. }
  1933. public object MapDictionaryToObject(
  1934. IDictionary sourceDictionary,
  1935. Type destObjectType,
  1936. params object[] parameters)
  1937. {
  1938. InitContext ctx = new InitContext();
  1939. ctx.MappingSchema = this;
  1940. ctx.DataSource = CreateDictionaryMapper(sourceDictionary);
  1941. ctx.SourceObject = sourceDictionary;
  1942. ctx.ObjectMapper = GetObjectMapper(destObjectType);
  1943. ctx.Parameters = parameters;
  1944. return MapInternal(ctx);
  1945. }
  1946. public T MapDictionaryToObject<T>(IDictionary sourceDictionary, params object[] parameters)
  1947. {
  1948. return (T)MapDictionaryToObject(sourceDictionary, typeof(T), parameters);
  1949. }
  1950. #endregion
  1951. #region MapDictionaryToDataRow
  1952. #if !SILVERLIGHT
  1953. public DataRow MapDictionaryToDataRow(
  1954. IDictionary sourceDictionary,
  1955. DataRow destRow)
  1956. {
  1957. MapInternal(
  1958. null,
  1959. CreateDictionaryMapper(sourceDictionary), sourceDictionary,
  1960. CreateDataRowMapper (destRow, DataRowVersion.Default), destRow,
  1961. null);
  1962. return destRow;
  1963. }
  1964. public DataRow MapDictionaryToDataRow(
  1965. IDictionary sourceDictionary,
  1966. DataTable destTable)
  1967. {
  1968. if (destTable == null) throw new ArgumentNullException("destTable");
  1969. DataRow destRow = destTable.NewRow();
  1970. destTable.Rows.Add(destRow);
  1971. MapInternal(
  1972. null,
  1973. CreateDictionaryMapper(sourceDictionary), sourceDictionary,
  1974. CreateDataRowMapper (destRow, DataRowVersion.Default), destRow,
  1975. null);
  1976. return destRow;
  1977. }
  1978. #endif
  1979. #endregion
  1980. #endregion
  1981. #region List
  1982. #region MapListToList
  1983. public IList MapListToList(
  1984. ICollection sourceList,
  1985. IList destList,
  1986. Type destObjectType,
  1987. params object[] parameters)
  1988. {
  1989. if (sourceList == null) throw new ArgumentNullException("sourceList");
  1990. MapSourceListToDestinationList(
  1991. CreateEnumeratorMapper(sourceList.GetEnumerator()),
  1992. CreateObjectListMapper(destList, GetObjectMapper(destObjectType)),
  1993. parameters);
  1994. return destList;
  1995. }
  1996. public IList MapListToList(
  1997. ICollection sourceList,
  1998. Type destObjectType,
  1999. params object[] parameters)
  2000. {
  2001. if (sourceList == null) throw new ArgumentNullException("sourceList");
  2002. var destList = new List<object>();
  2003. MapSourceListToDestinationList(
  2004. CreateEnumeratorMapper(sourceList.GetEnumerator()),
  2005. CreateObjectListMapper(destList, GetObjectMapper(destObjectType)),
  2006. parameters);
  2007. return destList;
  2008. }
  2009. public List<T> MapListToList<T>(
  2010. ICollection sourceList,
  2011. List<T> destList,
  2012. params object[] parameters)
  2013. {
  2014. MapSourceListToDestinationList(
  2015. CreateEnumeratorMapper(sourceList.GetEnumerator()),
  2016. CreateObjectListMapper(destList, GetObjectMapper(typeof(T))),
  2017. parameters);
  2018. return destList;
  2019. }
  2020. public List<T> MapListToList<T>(
  2021. ICollection sourceList,
  2022. params object[] parameters)
  2023. {
  2024. List<T> destList = new List<T>();
  2025. MapSourceListToDestinationList(
  2026. CreateEnumeratorMapper(sourceList.GetEnumerator()),
  2027. CreateObjectListMapper(destList, GetObjectMapper(typeof(T))),
  2028. parameters);
  2029. return destList;
  2030. }
  2031. #endregion
  2032. #region MapListToDataTable
  2033. #if !SILVERLIGHT
  2034. public DataTable MapListToDataTable(
  2035. ICollection sourceList,
  2036. DataTable destTable)
  2037. {
  2038. if (sourceList == null) throw new ArgumentNullException("sourceList");
  2039. MapSourceListToDestinationList(
  2040. CreateEnumeratorMapper(sourceList.GetEnumerator()),
  2041. CreateDataTableMapper (destTable, DataRowVersion.Default),
  2042. null);
  2043. return destTable;
  2044. }
  2045. [SuppressMessage("Microsoft.Globalization", "CA1306:SetLocaleForDataTypes")]
  2046. public DataTable MapListToDataTable(ICollection sourceList)
  2047. {
  2048. if (sourceList == null) throw new ArgumentNullException("sourceList");
  2049. DataTable destTable = new DataTable();
  2050. MapSourceListToDestinationList(
  2051. CreateEnumeratorMapper(sourceList.GetEnumerator()),
  2052. CreateDataTableMapper (destTable, DataRowVersion.Default),
  2053. null);
  2054. return destTable;
  2055. }
  2056. #endif
  2057. #endregion
  2058. #region MapListToDictionary
  2059. public IDictionary MapListToDictionary(
  2060. ICollection sourceList,
  2061. IDictionary destDictionary,
  2062. NameOrIndexParameter keyFieldNameOrIndex,
  2063. Type destObjectType,
  2064. params object[] parameters)
  2065. {
  2066. if (sourceList == null) throw new ArgumentNullException("sourceList");
  2067. MapSourceListToDestinationList(
  2068. CreateEnumeratorMapper (sourceList.GetEnumerator()),
  2069. CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
  2070. parameters);
  2071. return destDictionary;
  2072. }
  2073. public IDictionary MapListToDictionary(
  2074. ICollection sourceList,
  2075. NameOrIndexParameter keyFieldNameOrIndex,
  2076. Type destObjectType,
  2077. params object[] parameters)
  2078. {
  2079. if (sourceList == null) throw new ArgumentNullException("sourceList");
  2080. IDictionary destDictionary = new Dictionary<object,object>();
  2081. MapSourceListToDestinationList(
  2082. CreateEnumeratorMapper (sourceList.GetEnumerator()),
  2083. CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
  2084. parameters);
  2085. return destDictionary;
  2086. }
  2087. public IDictionary<TK,T> MapListToDictionary<TK,T>(
  2088. ICollection sourceList,
  2089. IDictionary<TK,T> destDictionary,
  2090. NameOrIndexParameter keyFieldNameOrIndex,
  2091. params object[] parameters)
  2092. {
  2093. MapSourceListToDestinationList(
  2094. CreateEnumeratorMapper (sourceList.GetEnumerator()),
  2095. CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
  2096. parameters);
  2097. return destDictionary;
  2098. }
  2099. public Dictionary<TK,T> MapListToDictionary<TK,T>(
  2100. ICollection sourceList,
  2101. NameOrIndexParameter keyFieldNameOrIndex,
  2102. params object[] parameters)
  2103. {
  2104. Dictionary<TK,T> destDictionary = new Dictionary<TK,T>();
  2105. MapSourceListToDestinationList(
  2106. CreateEnumeratorMapper (sourceList.GetEnumerator()),
  2107. CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
  2108. parameters);
  2109. return destDictionary;
  2110. }
  2111. #endregion
  2112. #region MapListToDictionaryIndex
  2113. public IDictionary MapListToDictionary(
  2114. ICollection sourceList,
  2115. IDictionary destDictionary,
  2116. MapIndex index,
  2117. Type destObjectType,
  2118. params object[] parameters)
  2119. {
  2120. if (sourceList == null) throw new ArgumentNullException("sourceList");
  2121. MapSourceListToDestinationList(
  2122. CreateEnumeratorMapper (sourceList.GetEnumerator()),
  2123. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
  2124. parameters);
  2125. return destDictionary;
  2126. }
  2127. public IDictionary MapListToDictionary(
  2128. ICollection sourceList,
  2129. MapIndex index,
  2130. Type destObjectType,
  2131. params object[] parameters)
  2132. {
  2133. if (sourceList == null) throw new ArgumentNullException("sourceList");
  2134. IDictionary destDictionary = new Dictionary<object,object>();
  2135. MapSourceListToDestinationList(
  2136. CreateEnumeratorMapper (sourceList.GetEnumerator()),
  2137. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
  2138. parameters);
  2139. return destDictionary;
  2140. }
  2141. public IDictionary<CompoundValue,T> MapListToDictionary<T>(
  2142. ICollection sourceList,
  2143. IDictionary<CompoundValue,T> destDictionary,
  2144. MapIndex index,
  2145. params object[] parameters)
  2146. {
  2147. MapSourceListToDestinationList(
  2148. CreateEnumeratorMapper (sourceList.GetEnumerator()),
  2149. CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
  2150. parameters);
  2151. return destDictionary;
  2152. }
  2153. public Dictionary<CompoundValue,T> MapListToDictionary<T>(
  2154. ICollection sourceList,
  2155. MapIndex index,
  2156. params object[] parameters)
  2157. {
  2158. Dictionary<CompoundValue, T> destDictionary = new Dictionary<CompoundValue,T>();
  2159. MapSourceListToDestinationList(
  2160. CreateEnumeratorMapper (sourceList.GetEnumerator()),
  2161. CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
  2162. parameters);
  2163. return destDictionary;
  2164. }
  2165. #endregion
  2166. #endregion
  2167. #region Table
  2168. #if !SILVERLIGHT
  2169. #region MapDataTableToDataTable
  2170. public DataTable MapDataTableToDataTable(
  2171. DataTable sourceTable,
  2172. DataTable destTable)
  2173. {
  2174. MapSourceListToDestinationList(
  2175. CreateDataTableMapper(sourceTable, DataRowVersion.Default),
  2176. CreateDataTableMapper(destTable, DataRowVersion.Default),
  2177. null);
  2178. return destTable;
  2179. }
  2180. public DataTable MapDataTableToDataTable(
  2181. DataTable sourceTable,
  2182. DataRowVersion version,
  2183. DataTable destTable)
  2184. {
  2185. MapSourceListToDestinationList(
  2186. CreateDataTableMapper(sourceTable, version),
  2187. CreateDataTableMapper(destTable, DataRowVersion.Default),
  2188. null);
  2189. return destTable;
  2190. }
  2191. public DataTable MapDataTableToDataTable(DataTable sourceTable)
  2192. {
  2193. if (sourceTable == null) throw new ArgumentNullException("sourceTable");
  2194. DataTable destTable = sourceTable.Clone();
  2195. MapSourceListToDestinationList(
  2196. CreateDataTableMapper(sourceTable, DataRowVersion.Default),
  2197. CreateDataTableMapper(destTable, DataRowVersion.Default),
  2198. null);
  2199. return destTable;
  2200. }
  2201. public DataTable MapDataTableToDataTable(
  2202. DataTable sourceTable,
  2203. DataRowVersion version)
  2204. {
  2205. if (sourceTable == null) throw new ArgumentNullException("sourceTable");
  2206. DataTable destTable = sourceTable.Clone();
  2207. MapSourceListToDestinationList(
  2208. CreateDataTableMapper(sourceTable, version),
  2209. CreateDataTableMapper(destTable, DataRowVersion.Default),
  2210. null);
  2211. return destTable;
  2212. }
  2213. #endregion
  2214. #region MapDataTableToList
  2215. public IList MapDataTableToList(
  2216. DataTable sourceTable,
  2217. IList list,
  2218. Type destObjectType,
  2219. params object[] parameters)
  2220. {
  2221. MapSourceListToDestinationList(
  2222. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2223. CreateObjectListMapper(list, GetObjectMapper(destObjectType)),
  2224. parameters);
  2225. return list;
  2226. }
  2227. public IList MapDataTableToList(
  2228. DataTable sourceTable,
  2229. DataRowVersion version,
  2230. IList list,
  2231. Type destObjectType,
  2232. params object[] parameters)
  2233. {
  2234. MapSourceListToDestinationList(
  2235. CreateDataTableMapper (sourceTable, version),
  2236. CreateObjectListMapper(list, GetObjectMapper(destObjectType)),
  2237. parameters);
  2238. return list;
  2239. }
  2240. public ArrayList MapDataTableToList(
  2241. DataTable sourceTable,
  2242. Type destObjectType,
  2243. params object[] parameters)
  2244. {
  2245. ArrayList list = new ArrayList();
  2246. MapSourceListToDestinationList(
  2247. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2248. CreateObjectListMapper(list, GetObjectMapper(destObjectType)),
  2249. parameters);
  2250. return list;
  2251. }
  2252. public ArrayList MapDataTableToList(
  2253. DataTable sourceTable,
  2254. DataRowVersion version,
  2255. Type destObjectType,
  2256. params object[] parameters)
  2257. {
  2258. ArrayList list = new ArrayList();
  2259. MapSourceListToDestinationList(
  2260. CreateDataTableMapper (sourceTable, version),
  2261. CreateObjectListMapper(list, GetObjectMapper(destObjectType)),
  2262. parameters);
  2263. return list;
  2264. }
  2265. public List<T> MapDataTableToList<T>(
  2266. DataTable sourceTable,
  2267. List<T> list,
  2268. params object[] parameters)
  2269. {
  2270. MapSourceListToDestinationList(
  2271. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2272. CreateObjectListMapper(list, GetObjectMapper(typeof(T))),
  2273. parameters);
  2274. return list;
  2275. }
  2276. public List<T> MapDataTableToList<T>(
  2277. DataTable sourceTable,
  2278. DataRowVersion version,
  2279. List<T> list,
  2280. params object[] parameters)
  2281. {
  2282. MapSourceListToDestinationList(
  2283. CreateDataTableMapper (sourceTable, version),
  2284. CreateObjectListMapper(list, GetObjectMapper(typeof(T))),
  2285. parameters);
  2286. return list;
  2287. }
  2288. public List<T> MapDataTableToList<T>(
  2289. DataTable sourceTable,
  2290. params object[] parameters)
  2291. {
  2292. List<T> list = new List<T>();
  2293. MapSourceListToDestinationList(
  2294. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2295. CreateObjectListMapper(list, GetObjectMapper(typeof(T))),
  2296. parameters);
  2297. return list;
  2298. }
  2299. public List<T> MapDataTableToList<T>(
  2300. DataTable sourceTable,
  2301. DataRowVersion version,
  2302. params object[] parameters)
  2303. {
  2304. List<T> list = new List<T>();
  2305. MapSourceListToDestinationList(
  2306. CreateDataTableMapper (sourceTable, version),
  2307. CreateObjectListMapper(list, GetObjectMapper(typeof(T))),
  2308. parameters);
  2309. return list;
  2310. }
  2311. #endregion
  2312. #region MapDataTableToDictionary
  2313. public IDictionary MapDataTableToDictionary(
  2314. DataTable sourceTable,
  2315. IDictionary destDictionary,
  2316. NameOrIndexParameter keyFieldNameOrIndex,
  2317. Type destObjectType,
  2318. params object[] parameters)
  2319. {
  2320. MapSourceListToDestinationList(
  2321. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2322. CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
  2323. parameters);
  2324. return destDictionary;
  2325. }
  2326. public Hashtable MapDataTableToDictionary(
  2327. DataTable sourceTable,
  2328. NameOrIndexParameter keyFieldNameOrIndex,
  2329. Type destObjectType,
  2330. params object[] parameters)
  2331. {
  2332. Hashtable destDictionary = new Hashtable();
  2333. MapSourceListToDestinationList(
  2334. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2335. CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
  2336. parameters);
  2337. return destDictionary;
  2338. }
  2339. public IDictionary<TK,T> MapDataTableToDictionary<TK,T>(
  2340. DataTable sourceTable,
  2341. IDictionary<TK,T> destDictionary,
  2342. NameOrIndexParameter keyFieldNameOrIndex,
  2343. params object[] parameters)
  2344. {
  2345. MapSourceListToDestinationList(
  2346. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2347. CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
  2348. parameters);
  2349. return destDictionary;
  2350. }
  2351. public Dictionary<TK,T> MapDataTableToDictionary<TK,T>(
  2352. DataTable sourceTable,
  2353. NameOrIndexParameter keyFieldNameOrIndex,
  2354. params object[] parameters)
  2355. {
  2356. Dictionary<TK,T> destDictionary = new Dictionary<TK,T>();
  2357. MapSourceListToDestinationList(
  2358. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2359. CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
  2360. parameters);
  2361. return destDictionary;
  2362. }
  2363. #endregion
  2364. #region MapDataTableToDictionary (Index)
  2365. public IDictionary MapDataTableToDictionary(
  2366. DataTable sourceTable,
  2367. IDictionary destDictionary,
  2368. MapIndex index,
  2369. Type destObjectType,
  2370. params object[] parameters)
  2371. {
  2372. MapSourceListToDestinationList(
  2373. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2374. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
  2375. parameters);
  2376. return destDictionary;
  2377. }
  2378. public Hashtable MapDataTableToDictionary(
  2379. DataTable sourceTable,
  2380. MapIndex index,
  2381. Type destObjectType,
  2382. params object[] parameters)
  2383. {
  2384. Hashtable destDictionary = new Hashtable();
  2385. MapSourceListToDestinationList(
  2386. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2387. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
  2388. parameters);
  2389. return destDictionary;
  2390. }
  2391. public IDictionary<CompoundValue,T> MapDataTableToDictionary<T>(
  2392. DataTable sourceTable,
  2393. IDictionary<CompoundValue,T> destDictionary,
  2394. MapIndex index,
  2395. params object[] parameters)
  2396. {
  2397. MapSourceListToDestinationList(
  2398. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2399. CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
  2400. parameters);
  2401. return destDictionary;
  2402. }
  2403. public Dictionary<CompoundValue,T> MapDataTableToDictionary<T>(
  2404. DataTable sourceTable,
  2405. MapIndex index,
  2406. params object[] parameters)
  2407. {
  2408. Dictionary<CompoundValue,T> destDictionary = new Dictionary<CompoundValue,T>();
  2409. MapSourceListToDestinationList(
  2410. CreateDataTableMapper (sourceTable, DataRowVersion.Default),
  2411. CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
  2412. parameters);
  2413. return destDictionary;
  2414. }
  2415. #endregion
  2416. #endif
  2417. #endregion
  2418. #region DataReader
  2419. #region MapDataReaderToList
  2420. public virtual IList MapDataReaderToList(
  2421. IDataReader reader,
  2422. IList list,
  2423. Type destObjectType,
  2424. params object[] parameters)
  2425. {
  2426. MapSourceListToDestinationList(
  2427. CreateDataReaderListMapper(reader),
  2428. CreateObjectListMapper (list, GetObjectMapper(destObjectType)),
  2429. parameters);
  2430. return list;
  2431. }
  2432. public IList MapDataReaderToList(
  2433. IDataReader reader,
  2434. Type destObjectType,
  2435. params object[] parameters)
  2436. {
  2437. IList list = new List<object>();
  2438. MapSourceListToDestinationList(
  2439. CreateDataReaderListMapper(reader),
  2440. CreateObjectListMapper (list, GetObjectMapper(destObjectType)),
  2441. parameters);
  2442. return list;
  2443. }
  2444. //NOTE changed to virtual
  2445. public virtual IList<T> MapDataReaderToList<T>(
  2446. IDataReader reader,
  2447. IList<T> list,
  2448. params object[] parameters)
  2449. {
  2450. MapSourceListToDestinationList(
  2451. CreateDataReaderListMapper(reader),
  2452. CreateObjectListMapper ((IList)list, GetObjectMapper(typeof(T))),
  2453. parameters);
  2454. return list;
  2455. }
  2456. public List<T> MapDataReaderToList<T>(
  2457. IDataReader reader,
  2458. params object[] parameters)
  2459. {
  2460. List<T> list = new List<T>();
  2461. MapSourceListToDestinationList(
  2462. CreateDataReaderListMapper(reader),
  2463. CreateObjectListMapper (list, GetObjectMapper(typeof(T))),
  2464. parameters);
  2465. return list;
  2466. }
  2467. #endregion
  2468. #region MapDataReaderToScalarList
  2469. public IList MapDataReaderToScalarList(
  2470. IDataReader reader,
  2471. NameOrIndexParameter nameOrIndex,
  2472. IList list,
  2473. Type type)
  2474. {
  2475. MapSourceListToDestinationList(
  2476. CreateDataReaderListMapper(reader, nameOrIndex),
  2477. CreateScalarDestinationListMapper(list, type),
  2478. null);
  2479. return list;
  2480. }
  2481. public IList MapDataReaderToScalarList(
  2482. IDataReader reader,
  2483. NameOrIndexParameter nameOrIndex,
  2484. Type type)
  2485. {
  2486. IList list = new List<object>();
  2487. MapSourceListToDestinationList(
  2488. CreateDataReaderListMapper(reader, nameOrIndex),
  2489. CreateScalarDestinationListMapper(list, type),
  2490. null);
  2491. return list;
  2492. }
  2493. public IList<T> MapDataReaderToScalarList<T>(
  2494. IDataReader reader,
  2495. NameOrIndexParameter nameOrIndex,
  2496. IList<T> list)
  2497. {
  2498. MapSourceListToDestinationList(
  2499. CreateDataReaderListMapper(reader, nameOrIndex),
  2500. CreateScalarDestinationListMapper(list),
  2501. null);
  2502. return list;
  2503. }
  2504. public List<T> MapDataReaderToScalarList<T>(
  2505. IDataReader reader,
  2506. NameOrIndexParameter nameOrIndex)
  2507. {
  2508. List<T> list = new List<T>();
  2509. MapSourceListToDestinationList(
  2510. CreateDataReaderListMapper(reader, nameOrIndex),
  2511. CreateScalarDestinationListMapper(list),
  2512. null);
  2513. return list;
  2514. }
  2515. #endregion
  2516. #region MapDataReaderToDataTable
  2517. #if !SILVERLIGHT
  2518. public DataTable MapDataReaderToDataTable(
  2519. IDataReader reader,
  2520. DataTable destTable)
  2521. {
  2522. MapSourceListToDestinationList(
  2523. CreateDataReaderListMapper(reader),
  2524. CreateDataTableMapper (destTable, DataRowVersion.Default),
  2525. null);
  2526. return destTable;
  2527. }
  2528. [SuppressMessage("Microsoft.Globalization", "CA1306:SetLocaleForDataTypes")]
  2529. public DataTable MapDataReaderToDataTable(IDataReader reader)
  2530. {
  2531. DataTable destTable = new DataTable();
  2532. MapSourceListToDestinationList(
  2533. CreateDataReaderListMapper(reader),
  2534. CreateDataTableMapper (destTable, DataRowVersion.Default),
  2535. null);
  2536. return destTable;
  2537. }
  2538. #endif
  2539. #endregion
  2540. #region MapDataReaderToDictionary
  2541. public IDictionary MapDataReaderToDictionary(
  2542. IDataReader reader,
  2543. IDictionary destDictionary,
  2544. NameOrIndexParameter keyFieldNameOrIndex,
  2545. Type destObjectType,
  2546. params object[] parameters)
  2547. {
  2548. MapSourceListToDestinationList(
  2549. CreateDataReaderListMapper(reader),
  2550. CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
  2551. parameters);
  2552. return destDictionary;
  2553. }
  2554. public IDictionary MapDataReaderToDictionary(
  2555. IDataReader reader,
  2556. NameOrIndexParameter keyFieldNameOrIndex,
  2557. Type destObjectType,
  2558. params object[] parameters)
  2559. {
  2560. IDictionary dest = new Dictionary<object,object>();
  2561. MapSourceListToDestinationList(
  2562. CreateDataReaderListMapper(reader),
  2563. CreateDictionaryListMapper(dest, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
  2564. parameters);
  2565. return dest;
  2566. }
  2567. public IDictionary<TK,T> MapDataReaderToDictionary<TK,T>(
  2568. IDataReader reader,
  2569. IDictionary<TK,T> destDictionary,
  2570. NameOrIndexParameter keyFieldNameOrIndex,
  2571. Type destObjectType,
  2572. params object[] parameters)
  2573. {
  2574. MapSourceListToDestinationList(
  2575. CreateDataReaderListMapper (reader),
  2576. CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
  2577. parameters);
  2578. return destDictionary;
  2579. }
  2580. public IDictionary<TK,T> MapDataReaderToDictionary<TK,T>(
  2581. IDataReader reader,
  2582. IDictionary<TK,T> destDictionary,
  2583. NameOrIndexParameter keyFieldNameOrIndex,
  2584. params object[] parameters)
  2585. {
  2586. MapSourceListToDestinationList(
  2587. CreateDataReaderListMapper (reader),
  2588. CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
  2589. parameters);
  2590. return destDictionary;
  2591. }
  2592. public Dictionary<TK,T> MapDataReaderToDictionary<TK,T>(
  2593. IDataReader reader,
  2594. NameOrIndexParameter keyFieldNameOrIndex,
  2595. params object[] parameters)
  2596. {
  2597. Dictionary<TK,T> dest = new Dictionary<TK,T>();
  2598. MapSourceListToDestinationList(
  2599. CreateDataReaderListMapper (reader),
  2600. CreateDictionaryListMapper<TK,T>(dest, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
  2601. parameters);
  2602. return dest;
  2603. }
  2604. #endregion
  2605. #region MapDataReaderToDictionary (Index)
  2606. public IDictionary MapDataReaderToDictionary(
  2607. IDataReader reader,
  2608. IDictionary destDictionary,
  2609. MapIndex index,
  2610. Type destObjectType,
  2611. params object[] parameters)
  2612. {
  2613. MapSourceListToDestinationList(
  2614. CreateDataReaderListMapper(reader),
  2615. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
  2616. parameters);
  2617. return destDictionary;
  2618. }
  2619. public IDictionary MapDataReaderToDictionary(
  2620. IDataReader reader,
  2621. MapIndex index,
  2622. Type destObjectType,
  2623. params object[] parameters)
  2624. {
  2625. IDictionary destDictionary = new Dictionary<object,object>();
  2626. MapSourceListToDestinationList(
  2627. CreateDataReaderListMapper(reader),
  2628. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
  2629. parameters);
  2630. return destDictionary;
  2631. }
  2632. public IDictionary<CompoundValue,T> MapDataReaderToDictionary<T>(
  2633. IDataReader reader,
  2634. IDictionary<CompoundValue,T> destDictionary,
  2635. MapIndex index,
  2636. Type destObjectType,
  2637. params object[] parameters)
  2638. {
  2639. MapSourceListToDestinationList(
  2640. CreateDataReaderListMapper(reader),
  2641. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
  2642. parameters);
  2643. return destDictionary;
  2644. }
  2645. public IDictionary<CompoundValue,T> MapDataReaderToDictionary<T>(
  2646. IDataReader reader,
  2647. IDictionary<CompoundValue,T> destDictionary,
  2648. MapIndex index,
  2649. params object[] parameters)
  2650. {
  2651. MapSourceListToDestinationList(
  2652. CreateDataReaderListMapper(reader),
  2653. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(typeof(T))),
  2654. parameters);
  2655. return destDictionary;
  2656. }
  2657. public Dictionary<CompoundValue,T> MapDataReaderToDictionary<T>(
  2658. IDataReader reader,
  2659. MapIndex index,
  2660. params object[] parameters)
  2661. {
  2662. Dictionary<CompoundValue,T> destDictionary = new Dictionary<CompoundValue,T>();
  2663. MapSourceListToDestinationList(
  2664. CreateDataReaderListMapper (reader),
  2665. CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
  2666. parameters);
  2667. return destDictionary;
  2668. }
  2669. #endregion
  2670. #endregion
  2671. #region Dictionary
  2672. #region MapDictionaryToList
  2673. public IList MapDictionaryToList(
  2674. IDictionary sourceDictionary,
  2675. IList destList,
  2676. Type destObjectType,
  2677. params object[] parameters)
  2678. {
  2679. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2680. MapSourceListToDestinationList(
  2681. CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
  2682. CreateObjectListMapper(destList, GetObjectMapper(destObjectType)),
  2683. parameters);
  2684. return destList;
  2685. }
  2686. public IList MapDictionaryToList(
  2687. IDictionary sourceDictionary,
  2688. Type destObjectType,
  2689. params object[] parameters)
  2690. {
  2691. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2692. IList destList = new List<object>();
  2693. MapSourceListToDestinationList(
  2694. CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
  2695. CreateObjectListMapper(destList, GetObjectMapper(destObjectType)),
  2696. parameters);
  2697. return destList;
  2698. }
  2699. public List<T> MapDictionaryToList<T>(
  2700. IDictionary sourceDictionary,
  2701. List<T> destList,
  2702. params object[] parameters)
  2703. {
  2704. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2705. MapSourceListToDestinationList(
  2706. CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
  2707. CreateObjectListMapper(destList, GetObjectMapper(typeof(T))),
  2708. parameters);
  2709. return destList;
  2710. }
  2711. public List<T> MapDictionaryToList<T>(
  2712. IDictionary sourceDictionary,
  2713. params object[] parameters)
  2714. {
  2715. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2716. List<T> destList = new List<T>();
  2717. MapSourceListToDestinationList(
  2718. CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
  2719. CreateObjectListMapper(destList, GetObjectMapper(typeof(T))),
  2720. parameters);
  2721. return destList;
  2722. }
  2723. #endregion
  2724. #region MapDictionaryToDataTable
  2725. #if !SILVERLIGHT
  2726. public DataTable MapDictionaryToDataTable(
  2727. IDictionary sourceDictionary,
  2728. DataTable destTable)
  2729. {
  2730. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2731. MapSourceListToDestinationList(
  2732. CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
  2733. CreateDataTableMapper (destTable, DataRowVersion.Default),
  2734. null);
  2735. return destTable;
  2736. }
  2737. [SuppressMessage("Microsoft.Globalization", "CA1306:SetLocaleForDataTypes")]
  2738. public DataTable MapDictionaryToDataTable(IDictionary sourceDictionary)
  2739. {
  2740. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2741. DataTable destTable = new DataTable();
  2742. MapSourceListToDestinationList(
  2743. CreateEnumeratorMapper(sourceDictionary.Values.GetEnumerator()),
  2744. CreateDataTableMapper (destTable, DataRowVersion.Default),
  2745. null);
  2746. return destTable;
  2747. }
  2748. #endif
  2749. #endregion
  2750. #region MapDictionaryToDictionary
  2751. public IDictionary MapDictionaryToDictionary(
  2752. IDictionary sourceDictionary,
  2753. IDictionary destDictionary,
  2754. NameOrIndexParameter keyFieldNameOrIndex,
  2755. Type destObjectType,
  2756. params object[] parameters)
  2757. {
  2758. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2759. MapSourceListToDestinationList(
  2760. CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
  2761. CreateDictionaryListMapper(destDictionary, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
  2762. parameters);
  2763. return destDictionary;
  2764. }
  2765. public IDictionary MapDictionaryToDictionary(
  2766. IDictionary sourceDictionary,
  2767. NameOrIndexParameter keyFieldNameOrIndex,
  2768. Type destObjectType,
  2769. params object[] parameters)
  2770. {
  2771. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2772. IDictionary dest = new Dictionary<object,object>();
  2773. MapSourceListToDestinationList(
  2774. CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
  2775. CreateDictionaryListMapper(dest, keyFieldNameOrIndex, GetObjectMapper(destObjectType)),
  2776. parameters);
  2777. return dest;
  2778. }
  2779. public IDictionary<TK,T> MapDictionaryToDictionary<TK,T>(
  2780. IDictionary sourceDictionary,
  2781. IDictionary<TK,T> destDictionary,
  2782. NameOrIndexParameter keyFieldNameOrIndex,
  2783. params object[] parameters)
  2784. {
  2785. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2786. MapSourceListToDestinationList(
  2787. CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
  2788. CreateDictionaryListMapper<TK,T>(destDictionary, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
  2789. parameters);
  2790. return destDictionary;
  2791. }
  2792. public Dictionary<TK,T> MapDictionaryToDictionary<TK,T>(
  2793. IDictionary sourceDictionary,
  2794. NameOrIndexParameter keyFieldNameOrIndex,
  2795. params object[] parameters)
  2796. {
  2797. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2798. Dictionary<TK,T> dest = new Dictionary<TK,T>();
  2799. MapSourceListToDestinationList(
  2800. CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
  2801. CreateDictionaryListMapper<TK,T>(dest, keyFieldNameOrIndex, GetObjectMapper(typeof(T))),
  2802. parameters);
  2803. return dest;
  2804. }
  2805. #endregion
  2806. #region MapDictionaryToDictionary (Index)
  2807. public IDictionary MapDictionaryToDictionary(
  2808. IDictionary sourceDictionary,
  2809. IDictionary destDictionary,
  2810. MapIndex index,
  2811. Type destObjectType,
  2812. params object[] parameters)
  2813. {
  2814. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2815. MapSourceListToDestinationList(
  2816. CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
  2817. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
  2818. parameters);
  2819. return destDictionary;
  2820. }
  2821. public IDictionary MapDictionaryToDictionary(
  2822. IDictionary sourceDictionary,
  2823. MapIndex index,
  2824. Type destObjectType,
  2825. params object[] parameters)
  2826. {
  2827. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2828. IDictionary destDictionary = new Dictionary<object,object>();
  2829. MapSourceListToDestinationList(
  2830. CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
  2831. CreateDictionaryListMapper(destDictionary, index, GetObjectMapper(destObjectType)),
  2832. parameters);
  2833. return destDictionary;
  2834. }
  2835. public IDictionary<CompoundValue,T> MapDictionaryToDictionary<T>(
  2836. IDictionary sourceDictionary,
  2837. IDictionary<CompoundValue,T> destDictionary,
  2838. MapIndex index,
  2839. params object[] parameters)
  2840. {
  2841. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2842. MapSourceListToDestinationList(
  2843. CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
  2844. CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
  2845. parameters);
  2846. return destDictionary;
  2847. }
  2848. public Dictionary<CompoundValue,T> MapDictionaryToDictionary<T>(
  2849. IDictionary sourceDictionary,
  2850. MapIndex index,
  2851. params object[] parameters)
  2852. {
  2853. if (sourceDictionary == null) throw new ArgumentNullException("sourceDictionary");
  2854. Dictionary<CompoundValue,T> destDictionary = new Dictionary<CompoundValue,T>();
  2855. MapSourceListToDestinationList(
  2856. CreateEnumeratorMapper (sourceDictionary.Values.GetEnumerator()),
  2857. CreateDictionaryListMapper<T>(destDictionary, index, GetObjectMapper(typeof(T))),
  2858. parameters);
  2859. return destDictionary;
  2860. }
  2861. #endregion
  2862. #endregion
  2863. #region MapToResultSet
  2864. public void MapResultSets(MapResultSet[] resultSets)
  2865. {
  2866. var initTable = new Dictionary<object,object>();
  2867. var context = new InitContext();
  2868. object lastContainer = null;
  2869. context.MappingSchema = this;
  2870. try
  2871. {
  2872. PrepareRelarions(resultSets);
  2873. // Map relations.
  2874. //
  2875. foreach (MapResultSet rs in resultSets)
  2876. {
  2877. if (rs.Relations == null)
  2878. continue;
  2879. ObjectMapper masterMapper = GetObjectMapper(rs.ObjectType);
  2880. foreach (MapRelation r in rs.Relations)
  2881. {
  2882. MemberAccessor ma = masterMapper.TypeAccessor[r.ContainerName];
  2883. if (ma == null)
  2884. throw new MappingException(string.Format(Resources.MapIndex_BadField,
  2885. masterMapper.TypeAccessor.OriginalType.Name, r.ContainerName));
  2886. // Map.
  2887. //
  2888. var slave = r.SlaveResultSet;
  2889. var slaveMapper = GetObjectMapper(r.SlaveResultSet.ObjectType);
  2890. var indexedLists = rs.GetIndex(this, r.MasterIndex);
  2891. foreach (object o in slave.List)
  2892. {
  2893. object key = r.SlaveIndex.GetValueOrIndex(slaveMapper, o);
  2894. if (IsNull(key))
  2895. continue;
  2896. IList masterList;
  2897. if (!indexedLists.TryGetValue(key, out masterList))
  2898. continue;
  2899. foreach (object master in masterList)
  2900. {
  2901. ISupportMapping msm = master as ISupportMapping;
  2902. if (msm != null)
  2903. {
  2904. if (initTable.ContainsKey(master) == false)
  2905. {
  2906. msm.BeginMapping(context);
  2907. initTable.Add(master, msm);
  2908. }
  2909. }
  2910. object container = ma.GetValue(master);
  2911. if (container is IList)
  2912. {
  2913. if (lastContainer != container)
  2914. {
  2915. lastContainer = container;
  2916. ISupportMapping sm = container as ISupportMapping;
  2917. if (sm != null)
  2918. {
  2919. if (initTable.ContainsKey(container) == false)
  2920. {
  2921. sm.BeginMapping(context);
  2922. initTable[container] = sm;
  2923. }
  2924. }
  2925. }
  2926. ((IList)container).Add(o);
  2927. }
  2928. else
  2929. {
  2930. ma.SetValue(master, o);
  2931. }
  2932. }
  2933. }
  2934. }
  2935. }
  2936. }
  2937. finally
  2938. {
  2939. foreach (ISupportMapping si in initTable.Values)
  2940. si.EndMapping(context);
  2941. }
  2942. }
  2943. public void MapDataReaderToResultSet(
  2944. IDataReader reader,
  2945. MapResultSet[] resultSets)
  2946. {
  2947. if (reader == null) throw new ArgumentNullException("reader");
  2948. foreach (MapResultSet rs in resultSets)
  2949. {
  2950. MapDataReaderToList(reader, rs.List, rs.ObjectType, rs.Parameters);
  2951. if (reader.NextResult() == false)
  2952. break;
  2953. }
  2954. MapResultSets(resultSets);
  2955. }
  2956. #if !SILVERLIGHT
  2957. public void MapDataSetToResultSet(
  2958. DataSet dataSet,
  2959. MapResultSet[] resultSets)
  2960. {
  2961. for (int i = 0; i < resultSets.Length && i < dataSet.Tables.Count; i++)
  2962. {
  2963. MapResultSet rs = resultSets[i];
  2964. MapDataTableToList(dataSet.Tables[i], rs.List, rs.ObjectType, rs.Parameters);
  2965. }
  2966. MapResultSets(resultSets);
  2967. }
  2968. #endif
  2969. public MapResultSet[] Clone(MapResultSet[] resultSets)
  2970. {
  2971. MapResultSet[] output = new MapResultSet[resultSets.Length];
  2972. for (int i = 0; i < resultSets.Length; i++)
  2973. output[i] = new MapResultSet(resultSets[i]);
  2974. return output;
  2975. }
  2976. private static int GetResultCount(MapNextResult[] nextResults)
  2977. {
  2978. int n = nextResults.Length;
  2979. foreach (MapNextResult nr in nextResults)
  2980. n += GetResultCount(nr.NextResults);
  2981. return n;
  2982. }
  2983. private static int GetResultSets(
  2984. int current,
  2985. MapResultSet[] output,
  2986. MapResultSet master,
  2987. MapNextResult[] nextResults)
  2988. {
  2989. foreach (MapNextResult nr in nextResults)
  2990. {
  2991. output[current] = new MapResultSet(nr.ObjectType);
  2992. master.AddRelation(output[current], nr.SlaveIndex, nr.MasterIndex, nr.ContainerName);
  2993. current += GetResultSets(current + 1, output, output[current], nr.NextResults);
  2994. }
  2995. return current;
  2996. }
  2997. public MapResultSet[] ConvertToResultSet(
  2998. Type masterType,
  2999. params MapNextResult[] nextResults)
  3000. {
  3001. MapResultSet[] output = new MapResultSet[1 + GetResultCount(nextResults)];
  3002. output[0] = new MapResultSet(masterType);
  3003. GetResultSets(1, output, output[0], nextResults);
  3004. return output;
  3005. }
  3006. private void PrepareRelarions(params MapResultSet[] sets)
  3007. {
  3008. foreach (MapResultSet masterSet in sets)
  3009. {
  3010. if (masterSet.Relations != null)
  3011. continue;
  3012. foreach (MapResultSet slaveSet in sets)
  3013. {
  3014. bool isSet;
  3015. List<MapRelationBase> relations
  3016. = MetadataProvider.GetRelations(this, Extensions, masterSet.ObjectType, slaveSet.ObjectType, out isSet);
  3017. if (!isSet)
  3018. continue;
  3019. foreach (MapRelationBase relation in relations)
  3020. masterSet.AddRelation(slaveSet, relation);
  3021. }
  3022. }
  3023. }
  3024. #endregion
  3025. #region GetObjectMapper
  3026. public Func<TSource,TDest> GetObjectMapper<TSource,TDest>()
  3027. {
  3028. return new ExpressionMapper<TSource,TDest>(this).GetMapper();
  3029. }
  3030. public Func<TSource,TDest> GetObjectMapper<TSource,TDest>(bool deepCopy)
  3031. {
  3032. return new ExpressionMapper<TSource,TDest>(this) { DeepCopy = deepCopy }.GetMapper();
  3033. }
  3034. #endregion
  3035. #region ConvertParameterValue
  3036. public virtual object ConvertParameterValue(object value, Type systemType)
  3037. {
  3038. return value;
  3039. }
  3040. #endregion
  3041. }
  3042. }