0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Data.SqlTypes;
|
|
4
|
|
5 using BLToolkit.Common;
|
|
6
|
|
7 namespace BLToolkit.Mapping
|
|
8 {
|
|
9 public class ScalarListMapper<T> : MapDataDestinationBase
|
|
10 {
|
|
11 public ScalarListMapper(MappingSchema mappingSchema, IList<T> list)
|
|
12 {
|
|
13 _list = list;
|
|
14 _mappingSchema = mappingSchema;
|
|
15 _nullValue = (T)mappingSchema.GetNullValue(_type);
|
|
16 _isNullable = _type.IsGenericType && _type.GetGenericTypeDefinition() == typeof(Nullable<>);
|
|
17 _underlyingType = _isNullable? Nullable.GetUnderlyingType(_type): _type;
|
|
18 }
|
|
19
|
|
20 private readonly IList<T> _list;
|
|
21 private readonly MappingSchema _mappingSchema;
|
|
22 private readonly T _nullValue;
|
|
23 private readonly bool _isNullable;
|
|
24 private readonly Type _type = typeof(T);
|
|
25 private readonly Type _underlyingType;
|
|
26
|
|
27 #region IMapDataDestination Members
|
|
28
|
|
29 public override Type GetFieldType(int index) { return _type; }
|
|
30 public override int GetOrdinal (string name) { return 0; }
|
|
31 public override void SetValue (object o, int index, object value) { _list.Add((T)_mappingSchema.ConvertChangeType(value, _underlyingType, _isNullable)); }
|
|
32 public override void SetValue (object o, string name, object value) { _list.Add((T)_mappingSchema.ConvertChangeType(value, _underlyingType, _isNullable)); }
|
|
33
|
|
34 public override void SetNull (object o, int index) { _list.Add(_nullValue); }
|
|
35
|
|
36 public override bool SupportsTypedValues(int index) { return true; }
|
|
37
|
|
38 // Simple types setters.
|
|
39 //
|
|
40 [CLSCompliant(false)]
|
|
41 public override void SetSByte (object o, int index, SByte value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
42 public override void SetInt16 (object o, int index, Int16 value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
43 public override void SetInt32 (object o, int index, Int32 value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
44 public override void SetInt64 (object o, int index, Int64 value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
45
|
|
46 public override void SetByte (object o, int index, Byte value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
47 [CLSCompliant(false)]
|
|
48 public override void SetUInt16 (object o, int index, UInt16 value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
49 [CLSCompliant(false)]
|
|
50 public override void SetUInt32 (object o, int index, UInt32 value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
51 [CLSCompliant(false)]
|
|
52 public override void SetUInt64 (object o, int index, UInt64 value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
53
|
|
54 public override void SetBoolean (object o, int index, Boolean value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
55 public override void SetChar (object o, int index, Char value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
56 public override void SetSingle (object o, int index, Single value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
57 public override void SetDouble (object o, int index, Double value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
58 public override void SetDecimal (object o, int index, Decimal value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
59 public override void SetGuid (object o, int index, Guid value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
60 public override void SetDateTime (object o, int index, DateTime value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
61 public override void SetDateTimeOffset(object o, int index, DateTimeOffset value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
62
|
|
63 // Nullable types setters.
|
|
64 //
|
|
65 [CLSCompliant(false)]
|
|
66 public override void SetNullableSByte (object o, int index, SByte? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
67 public override void SetNullableInt16 (object o, int index, Int16? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
68 public override void SetNullableInt32 (object o, int index, Int32? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
69 public override void SetNullableInt64 (object o, int index, Int64? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
70
|
|
71 public override void SetNullableByte (object o, int index, Byte? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
72 [CLSCompliant(false)]
|
|
73 public override void SetNullableUInt16 (object o, int index, UInt16? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
74 [CLSCompliant(false)]
|
|
75 public override void SetNullableUInt32 (object o, int index, UInt32? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
76 [CLSCompliant(false)]
|
|
77 public override void SetNullableUInt64 (object o, int index, UInt64? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
78
|
|
79 public override void SetNullableBoolean (object o, int index, Boolean? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
80 public override void SetNullableChar (object o, int index, Char? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
81 public override void SetNullableSingle (object o, int index, Single? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
82 public override void SetNullableDouble (object o, int index, Double? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
83 public override void SetNullableDecimal (object o, int index, Decimal? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
84 public override void SetNullableGuid (object o, int index, Guid? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
85 public override void SetNullableDateTime(object o, int index, DateTime? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
86 public override void SetNullableDateTimeOffset(object o, int index, DateTimeOffset? value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
87
|
|
88 #if !SILVERLIGHT
|
|
89
|
|
90 // SQL type setters.
|
|
91 //
|
|
92 public override void SetSqlByte (object o, int index, SqlByte value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
93 public override void SetSqlInt16 (object o, int index, SqlInt16 value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
94 public override void SetSqlInt32 (object o, int index, SqlInt32 value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
95 public override void SetSqlInt64 (object o, int index, SqlInt64 value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
96 public override void SetSqlSingle (object o, int index, SqlSingle value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
97 public override void SetSqlBoolean (object o, int index, SqlBoolean value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
98 public override void SetSqlDouble (object o, int index, SqlDouble value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
99 public override void SetSqlDateTime(object o, int index, SqlDateTime value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
100 public override void SetSqlDecimal (object o, int index, SqlDecimal value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
101 public override void SetSqlMoney (object o, int index, SqlMoney value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
102 public override void SetSqlGuid (object o, int index, SqlGuid value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
103 public override void SetSqlString (object o, int index, SqlString value) { _list.Add(ConvertTo<T>.From(value)); }
|
|
104
|
|
105 #endif
|
|
106
|
|
107 #endregion
|
|
108 }
|
|
109 }
|