0
|
1 using System;
|
|
2 using System.Data.SqlTypes;
|
|
3
|
|
4 using KeyValue = System.Collections.Generic.KeyValuePair<System.Type, System.Type>;
|
|
5 using Table = System.Collections.Generic.Dictionary<System.Collections.Generic.KeyValuePair<System.Type, System.Type>, BLToolkit.Mapping.IValueMapper>;
|
|
6
|
|
7 namespace BLToolkit.Mapping
|
|
8 {
|
|
9 [CLSCompliant(false)]
|
|
10 public interface IMapDataSource
|
|
11 {
|
|
12 int Count { get; }
|
|
13
|
|
14 Type GetFieldType (int index);
|
|
15 string GetName (int index);
|
|
16 int GetOrdinal (string name);
|
|
17 object GetValue (object o, int index);
|
|
18 object GetValue (object o, string name);
|
|
19
|
|
20 bool IsNull (object o, int index);
|
|
21
|
|
22 bool SupportsTypedValues(int index);
|
|
23
|
|
24 // Simple type getters.
|
|
25 //
|
|
26 [CLSCompliant(false)]
|
|
27 SByte GetSByte (object o, int index);
|
|
28 Int16 GetInt16 (object o, int index);
|
|
29 Int32 GetInt32 (object o, int index);
|
|
30 Int64 GetInt64 (object o, int index);
|
|
31
|
|
32 Byte GetByte (object o, int index);
|
|
33 [CLSCompliant(false)]
|
|
34 UInt16 GetUInt16 (object o, int index);
|
|
35 [CLSCompliant(false)]
|
|
36 UInt32 GetUInt32 (object o, int index);
|
|
37 [CLSCompliant(false)]
|
|
38 UInt64 GetUInt64 (object o, int index);
|
|
39
|
|
40 Boolean GetBoolean (object o, int index);
|
|
41 Char GetChar (object o, int index);
|
|
42 Single GetSingle (object o, int index);
|
|
43 Double GetDouble (object o, int index);
|
|
44 Decimal GetDecimal (object o, int index);
|
|
45 DateTime GetDateTime (object o, int index);
|
|
46 Guid GetGuid (object o, int index);
|
|
47
|
|
48 #if FW2
|
|
49 // Simple type getters.
|
|
50 //
|
|
51 [CLSCompliant(false)]
|
|
52 SByte? GetNullableSByte (object o, int index);
|
|
53 Int16? GetNullableInt16 (object o, int index);
|
|
54 Int32? GetNullableInt32 (object o, int index);
|
|
55 Int64? GetNullableInt64 (object o, int index);
|
|
56
|
|
57 Byte? GetNullableByte (object o, int index);
|
|
58 [CLSCompliant(false)]
|
|
59 UInt16? GetNullableUInt16 (object o, int index);
|
|
60 [CLSCompliant(false)]
|
|
61 UInt32? GetNullableUInt32 (object o, int index);
|
|
62 [CLSCompliant(false)]
|
|
63 UInt64? GetNullableUInt64 (object o, int index);
|
|
64
|
|
65 Boolean? GetNullableBoolean (object o, int index);
|
|
66 Char? GetNullableChar (object o, int index);
|
|
67 Single? GetNullableSingle (object o, int index);
|
|
68 Double? GetNullableDouble (object o, int index);
|
|
69 Decimal? GetNullableDecimal (object o, int index);
|
|
70 DateTime? GetNullableDateTime(object o, int index);
|
|
71 Guid? GetNullableGuid (object o, int index);
|
|
72 #endif
|
|
73
|
|
74 // SQL type getters.
|
|
75 //
|
|
76 SqlByte GetSqlByte (object o, int index);
|
|
77 SqlInt16 GetSqlInt16 (object o, int index);
|
|
78 SqlInt32 GetSqlInt32 (object o, int index);
|
|
79 SqlInt64 GetSqlInt64 (object o, int index);
|
|
80 SqlSingle GetSqlSingle (object o, int index);
|
|
81 SqlBoolean GetSqlBoolean (object o, int index);
|
|
82 SqlDouble GetSqlDouble (object o, int index);
|
|
83 SqlDateTime GetSqlDateTime (object o, int index);
|
|
84 SqlDecimal GetSqlDecimal (object o, int index);
|
|
85 SqlMoney GetSqlMoney (object o, int index);
|
|
86 SqlGuid GetSqlGuid (object o, int index);
|
|
87 SqlString GetSqlString (object o, int index);
|
|
88 }
|
|
89
|
|
90 [CLSCompliant(false)]
|
|
91 public interface IMapDataDestination
|
|
92 {
|
|
93 Type GetFieldType (int index);
|
|
94 int GetOrdinal (string name);
|
|
95 void SetValue (object o, int index, object value);
|
|
96 void SetValue (object o, string name, object value);
|
|
97
|
|
98 void SetNull (object o, int index);
|
|
99
|
|
100 bool SupportsTypedValues(int index);
|
|
101
|
|
102 // Simple type setters.
|
|
103 //
|
|
104 [CLSCompliant(false)]
|
|
105 void SetSByte (object o, int index, SByte value);
|
|
106 void SetInt16 (object o, int index, Int16 value);
|
|
107 void SetInt32 (object o, int index, Int32 value);
|
|
108 void SetInt64 (object o, int index, Int64 value);
|
|
109
|
|
110 void SetByte (object o, int index, Byte value);
|
|
111 [CLSCompliant(false)]
|
|
112 void SetUInt16 (object o, int index, UInt16 value);
|
|
113 [CLSCompliant(false)]
|
|
114 void SetUInt32 (object o, int index, UInt32 value);
|
|
115 [CLSCompliant(false)]
|
|
116 void SetUInt64 (object o, int index, UInt64 value);
|
|
117
|
|
118 void SetBoolean (object o, int index, Boolean value);
|
|
119 void SetChar (object o, int index, Char value);
|
|
120 void SetSingle (object o, int index, Single value);
|
|
121 void SetDouble (object o, int index, Double value);
|
|
122 void SetDecimal (object o, int index, Decimal value);
|
|
123 void SetGuid (object o, int index, Guid value);
|
|
124 void SetDateTime (object o, int index, DateTime value);
|
|
125
|
|
126 #if FW2
|
|
127 // Simple type setters.
|
|
128 //
|
|
129 [CLSCompliant(false)]
|
|
130 void SetNullableSByte (object o, int index, SByte? value);
|
|
131 void SetNullableInt16 (object o, int index, Int16? value);
|
|
132 void SetNullableInt32 (object o, int index, Int32? value);
|
|
133 void SetNullableInt64 (object o, int index, Int64? value);
|
|
134
|
|
135 void SetNullableByte (object o, int index, Byte? value);
|
|
136 [CLSCompliant(false)]
|
|
137 void SetNullableUInt16 (object o, int index, UInt16? value);
|
|
138 [CLSCompliant(false)]
|
|
139 void SetNullableUInt32 (object o, int index, UInt32? value);
|
|
140 [CLSCompliant(false)]
|
|
141 void SetNullableUInt64 (object o, int index, UInt64? value);
|
|
142
|
|
143 void SetNullableBoolean (object o, int index, Boolean? value);
|
|
144 void SetNullableChar (object o, int index, Char? value);
|
|
145 void SetNullableSingle (object o, int index, Single? value);
|
|
146 void SetNullableDouble (object o, int index, Double? value);
|
|
147 void SetNullableDecimal (object o, int index, Decimal? value);
|
|
148 void SetNullableGuid (object o, int index, Guid? value);
|
|
149 void SetNullableDateTime (object o, int index, DateTime? value);
|
|
150 #endif
|
|
151
|
|
152 // SQL type setters.
|
|
153 //
|
|
154 void SetSqlByte (object o, int index, SqlByte value);
|
|
155 void SetSqlInt16 (object o, int index, SqlInt16 value);
|
|
156 void SetSqlInt32 (object o, int index, SqlInt32 value);
|
|
157 void SetSqlInt64 (object o, int index, SqlInt64 value);
|
|
158 void SetSqlSingle (object o, int index, SqlSingle value);
|
|
159 void SetSqlBoolean (object o, int index, SqlBoolean value);
|
|
160 void SetSqlDouble (object o, int index, SqlDouble value);
|
|
161 void SetSqlDateTime(object o, int index, SqlDateTime value);
|
|
162 void SetSqlDecimal (object o, int index, SqlDecimal value);
|
|
163 void SetSqlMoney (object o, int index, SqlMoney value);
|
|
164 void SetSqlGuid (object o, int index, SqlGuid value);
|
|
165 void SetSqlString (object o, int index, SqlString value);
|
|
166 }
|
|
167
|
|
168 [CLSCompliant(false)]
|
|
169 public interface IValueMapper
|
|
170 {
|
|
171 void Map(
|
|
172 IMapDataSource source, object sourceObject, int sourceIndex,
|
|
173 IMapDataDestination dest, object destObject, int destIndex);
|
|
174 }
|
|
175
|
|
176 public static partial class ValueMapping
|
|
177 {
|
|
178 #region Init
|
|
179
|
|
180 private static Table _mappers = new Table();
|
|
181
|
|
182 private static void AddSameType(Type type, IValueMapper mapper)
|
|
183 {
|
|
184 _mappers.Add(new KeyValue(type, type), mapper);
|
|
185 }
|
|
186
|
|
187 #endregion
|
|
188
|
|
189 #region Default Mapper
|
|
190
|
|
191 class DefaultValueMapper : IValueMapper
|
|
192 {
|
|
193 public void Map(
|
|
194 IMapDataSource source, object sourceObject, int sourceIndex,
|
|
195 IMapDataDestination dest, object destObject, int destIndex)
|
|
196 {
|
|
197 dest.SetValue(destObject, destIndex, source.GetValue(sourceObject, sourceIndex));
|
|
198
|
|
199 //object o = source.GetValue(sourceObject, sourceIndex);
|
|
200
|
|
201 //if (o == null) dest.SetNull (destObject, destIndex);
|
|
202 //else dest.SetValue(destObject, destIndex, o);
|
|
203 }
|
|
204 }
|
|
205
|
|
206 private static IValueMapper _defaultMapper = new DefaultValueMapper();
|
|
207 [CLSCompliant(false)]
|
|
208 public static IValueMapper DefaultMapper
|
|
209 {
|
|
210 get { return _defaultMapper; }
|
|
211 set { _defaultMapper = value; }
|
|
212 }
|
|
213
|
|
214 #endregion
|
|
215
|
|
216 #region GetMapper
|
|
217
|
|
218 private static object _sync = new object();
|
|
219
|
|
220 [CLSCompliant(false)]
|
|
221 public static IValueMapper GetMapper(Type t1, Type t2)
|
|
222 {
|
|
223 lock (_sync)
|
|
224 {
|
|
225 if (t1 == null) t1 = typeof(object);
|
|
226 if (t2 == null) t2 = typeof(object);
|
|
227
|
|
228 if (t1.IsEnum) t1 = Enum.GetUnderlyingType(t1);
|
|
229 if (t2.IsEnum) t2 = Enum.GetUnderlyingType(t2);
|
|
230
|
|
231 KeyValue key = new KeyValue(t1, t2);
|
|
232
|
|
233 IValueMapper t;
|
|
234
|
|
235 #if FW2
|
|
236 if (_mappers.TryGetValue(key, out t))
|
|
237 return t;
|
|
238
|
|
239 #else
|
|
240 t = (IValueMapper)_mappers[key];
|
|
241
|
|
242 if (t != null)
|
|
243 return t;
|
|
244
|
|
245 t = _defaultMapper;
|
|
246 #endif
|
|
247 _mappers.Add(key, t);
|
|
248
|
|
249 return t;
|
|
250 }
|
|
251 }
|
|
252
|
|
253 #endregion
|
|
254 }
|
|
255 }
|