comparison Source/Mapping/MemberMapper.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Data;
3 using System.Data.SqlTypes;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Xml;
7
8 using BLToolkit.Data.Sql;
9 using BLToolkit.Reflection;
10 using BLToolkit.TypeBuilder;
11
12 using Convert = BLToolkit.Common.Convert;
13 #if !SILVERLIGHT
14 using System.Xml.Linq;
15 #endif
16
17 namespace BLToolkit.Mapping
18 {
19 public partial class MemberMapper
20 {
21 #region Init
22
23 public virtual void Init(MapMemberInfo mapMemberInfo)
24 {
25 if (mapMemberInfo == null) throw new ArgumentNullException("mapMemberInfo");
26
27 MapMemberInfo = mapMemberInfo;
28 Name = mapMemberInfo.Name;
29 MemberName = mapMemberInfo.MemberName;
30 Storage = mapMemberInfo.Storage;
31 DbType = mapMemberInfo.DbType;
32 _type = mapMemberInfo.Type;
33 MemberAccessor = mapMemberInfo.MemberAccessor;
34 _complexMemberAccessor = mapMemberInfo.ComplexMemberAccessor;
35 MappingSchema = mapMemberInfo.MappingSchema;
36
37 if (Storage != null)
38 MemberAccessor = ExprMemberAccessor.GetMemberAccessor(MemberAccessor.TypeAccessor, Storage);
39 }
40
41 internal static MemberMapper CreateMemberMapper(MapMemberInfo mi)
42 {
43 var type = mi.Type;
44 var mm = null as MemberMapper;
45
46 if (type.IsPrimitive || type.IsEnum)
47 mm = GetPrimitiveMemberMapper(mi);
48
49 if (mm == null)
50 {
51 mm = GetNullableMemberMapper(mi);
52
53 //if (mm != null)
54 // mi.IsNullable = true;
55 }
56
57 if (mm == null) mm = GetSimpleMemberMapper(mi);
58 #if !SILVERLIGHT
59 if (mm == null) mm = GetSqlTypeMemberMapper(mi);
60 #endif
61 return mm ?? new DefaultMemberMapper();
62 }
63
64 #endregion
65
66 #region Public Properties
67
68 public MappingSchema MappingSchema { get; private set; }
69 public string Name { get; private set; }
70 public string MemberName { get; private set; }
71 public string Storage { get; private set; }
72 public DbType DbType { get; private set; }
73 public MapMemberInfo MapMemberInfo { get; private set; }
74 public int Ordinal { get; private set; }
75 public MemberAccessor MemberAccessor { get; private set; }
76 public bool IsExplicit { get; set; }
77
78 internal void SetOrdinal(int ordinal)
79 {
80 Ordinal = ordinal;
81 }
82
83 private MemberAccessor _complexMemberAccessor;
84 public MemberAccessor ComplexMemberAccessor
85 {
86 [DebuggerStepThrough]
87 get { return _complexMemberAccessor ?? MemberAccessor; }
88 }
89
90 Type _type;
91 public virtual Type Type
92 {
93 get { return _type; }
94 }
95
96 public DbType GetDbType()
97 {
98 if (MapMemberInfo.IsDbTypeSet)
99 return DbType;
100
101 if (DbType != DbType.Object)
102 return DbType;
103
104 var dataType = SqlDataType.GetDataType(_type);
105
106 switch (dataType.SqlDbType)
107 {
108 case SqlDbType.BigInt : return DbType.Int64;
109 case SqlDbType.Binary : return DbType.Binary;
110 case SqlDbType.Bit : return DbType.Boolean;
111 case SqlDbType.Char : return DbType.AnsiStringFixedLength;
112 case SqlDbType.DateTime : return DbType.DateTime;
113 case SqlDbType.Decimal : return DbType.Decimal;
114 case SqlDbType.Float : return DbType.Double;
115 case SqlDbType.Image : return DbType.Binary;
116 case SqlDbType.Int : return DbType.Int32;
117 case SqlDbType.Money : return DbType.Currency;
118 case SqlDbType.NChar : return DbType.StringFixedLength;
119 case SqlDbType.NText : return DbType.String;
120 case SqlDbType.NVarChar : return DbType.String;
121 case SqlDbType.Real : return DbType.Single;
122 case SqlDbType.UniqueIdentifier : return DbType.Guid;
123 case SqlDbType.SmallDateTime : return DbType.DateTime;
124 case SqlDbType.SmallInt : return DbType.Int16;
125 case SqlDbType.SmallMoney : return DbType.Currency;
126 case SqlDbType.Text : return DbType.AnsiString;
127 case SqlDbType.Timestamp : return DbType.Binary;
128 case SqlDbType.TinyInt : return DbType.Byte;
129 case SqlDbType.VarBinary : return DbType.Binary;
130 case SqlDbType.VarChar : return DbType.AnsiString;
131 case SqlDbType.Variant : return DbType.Object;
132 case SqlDbType.Xml : return DbType.Xml;
133 case SqlDbType.Udt : return DbType.Binary;
134 case SqlDbType.Date : return DbType.Date;
135 case SqlDbType.Time : return DbType.Time;
136 #if !MONO
137 case SqlDbType.Structured : return DbType.Binary;
138 case SqlDbType.DateTime2 : return DbType.DateTime2;
139 case SqlDbType.DateTimeOffset : return DbType.DateTimeOffset;
140 #endif
141 }
142
143 return DbType.Object;
144 }
145
146 public int GetDbSize(object value)
147 {
148 if (MapMemberInfo.IsDbSizeSet)
149 return MapMemberInfo.DbSize;
150
151 if (value == null)
152 return 0;
153
154 if (value is string)
155 return ((string)value).Length;
156
157 if (value is byte[])
158 return ((byte[])value).Length;
159
160
161 var dataType = SqlDataType.GetDataType(_type);
162
163 switch (dataType.SqlDbType)
164 {
165 case SqlDbType.BigInt : return 0;
166 case SqlDbType.Binary : return 0;
167 case SqlDbType.Bit : return 0;
168 case SqlDbType.Char : return 0;
169 case SqlDbType.DateTime : return 0;
170 case SqlDbType.Decimal : return 0;
171 case SqlDbType.Float : return 0;
172 case SqlDbType.Image : return 0;
173 case SqlDbType.Int : return 0;
174 case SqlDbType.Money : return 0;
175 case SqlDbType.NChar : return 0;
176 case SqlDbType.NText : return 0;
177 case SqlDbType.NVarChar : return 0;
178 case SqlDbType.Real : return 0;
179 case SqlDbType.UniqueIdentifier : return 0;
180 case SqlDbType.SmallDateTime : return 0;
181 case SqlDbType.SmallInt : return 0;
182 case SqlDbType.SmallMoney : return 0;
183 case SqlDbType.Text : return 0;
184 case SqlDbType.Timestamp : return 0;
185 case SqlDbType.TinyInt : return 0;
186 case SqlDbType.VarBinary : return 0;
187 case SqlDbType.VarChar : return 0;
188 case SqlDbType.Variant : return 0;
189 case SqlDbType.Xml : return 0;
190 case SqlDbType.Udt : return 0;
191 case SqlDbType.Date : return 0;
192 case SqlDbType.Time : return 0;
193 #if !MONO
194 case SqlDbType.Structured : return 0;
195 case SqlDbType.DateTime2 : return 0;
196 case SqlDbType.DateTimeOffset : return 0;
197 #endif
198 }
199
200 return 0;
201 }
202
203 #endregion
204
205 #region Default Members (GetValue, SetValue)
206
207 public virtual bool SupportsValue { get { return !IsExplicit; } }
208
209 public virtual object GetValue(object o)
210 {
211 return MemberAccessor.GetValue(o);
212 }
213
214 public virtual bool IsNull (object o) { return GetValue(o) == null; }
215
216 // Simple type getters.
217 //
218 [CLSCompliant(false)]
219 public virtual SByte GetSByte (object o) { return MemberAccessor.GetSByte (o); }
220 public virtual Int16 GetInt16 (object o) { return MemberAccessor.GetInt16 (o); }
221 public virtual Int32 GetInt32 (object o) { return MemberAccessor.GetInt32 (o); }
222 public virtual Int64 GetInt64 (object o) { return MemberAccessor.GetInt64 (o); }
223
224 public virtual Byte GetByte (object o) { return MemberAccessor.GetByte (o); }
225 [CLSCompliant(false)]
226 public virtual UInt16 GetUInt16 (object o) { return MemberAccessor.GetUInt16 (o); }
227 [CLSCompliant(false)]
228 public virtual UInt32 GetUInt32 (object o) { return MemberAccessor.GetUInt32 (o); }
229 [CLSCompliant(false)]
230 public virtual UInt64 GetUInt64 (object o) { return MemberAccessor.GetUInt64 (o); }
231
232 public virtual Boolean GetBoolean (object o) { return MemberAccessor.GetBoolean (o); }
233 public virtual Char GetChar (object o) { return MemberAccessor.GetChar (o); }
234 public virtual Single GetSingle (object o) { return MemberAccessor.GetSingle (o); }
235 public virtual Double GetDouble (object o) { return MemberAccessor.GetDouble (o); }
236 public virtual Decimal GetDecimal (object o) { return MemberAccessor.GetDecimal (o); }
237 public virtual Guid GetGuid (object o) { return MemberAccessor.GetGuid (o); }
238 public virtual DateTime GetDateTime(object o) { return MemberAccessor.GetDateTime(o); }
239 public virtual DateTimeOffset GetDateTimeOffset(object o) { return MemberAccessor.GetDateTimeOffset(o); }
240
241 // Nullable type getters.
242 //
243 [CLSCompliant(false)]
244 public virtual SByte? GetNullableSByte (object o) { return MemberAccessor.GetNullableSByte (o); }
245 public virtual Int16? GetNullableInt16 (object o) { return MemberAccessor.GetNullableInt16 (o); }
246 public virtual Int32? GetNullableInt32 (object o) { return MemberAccessor.GetNullableInt32 (o); }
247 public virtual Int64? GetNullableInt64 (object o) { return MemberAccessor.GetNullableInt64 (o); }
248
249 public virtual Byte? GetNullableByte (object o) { return MemberAccessor.GetNullableByte (o); }
250 [CLSCompliant(false)]
251 public virtual UInt16? GetNullableUInt16 (object o) { return MemberAccessor.GetNullableUInt16 (o); }
252 [CLSCompliant(false)]
253 public virtual UInt32? GetNullableUInt32 (object o) { return MemberAccessor.GetNullableUInt32 (o); }
254 [CLSCompliant(false)]
255 public virtual UInt64? GetNullableUInt64 (object o) { return MemberAccessor.GetNullableUInt64 (o); }
256
257 public virtual Boolean? GetNullableBoolean (object o) { return MemberAccessor.GetNullableBoolean (o); }
258 public virtual Char? GetNullableChar (object o) { return MemberAccessor.GetNullableChar (o); }
259 public virtual Single? GetNullableSingle (object o) { return MemberAccessor.GetNullableSingle (o); }
260 public virtual Double? GetNullableDouble (object o) { return MemberAccessor.GetNullableDouble (o); }
261 public virtual Decimal? GetNullableDecimal (object o) { return MemberAccessor.GetNullableDecimal (o); }
262 public virtual Guid? GetNullableGuid (object o) { return MemberAccessor.GetNullableGuid (o); }
263 public virtual DateTime? GetNullableDateTime(object o) { return MemberAccessor.GetNullableDateTime(o); }
264 public virtual DateTimeOffset? GetNullableDateTimeOffset(object o) { return MemberAccessor.GetNullableDateTimeOffset(o); }
265
266 #if !SILVERLIGHT
267
268 // SQL type getters.
269 //
270 public virtual SqlByte GetSqlByte (object o) { return MemberAccessor.GetSqlByte (o); }
271 public virtual SqlInt16 GetSqlInt16 (object o) { return MemberAccessor.GetSqlInt16 (o); }
272 public virtual SqlInt32 GetSqlInt32 (object o) { return MemberAccessor.GetSqlInt32 (o); }
273 public virtual SqlInt64 GetSqlInt64 (object o) { return MemberAccessor.GetSqlInt64 (o); }
274 public virtual SqlSingle GetSqlSingle (object o) { return MemberAccessor.GetSqlSingle (o); }
275 public virtual SqlBoolean GetSqlBoolean (object o) { return MemberAccessor.GetSqlBoolean (o); }
276 public virtual SqlDouble GetSqlDouble (object o) { return MemberAccessor.GetSqlDouble (o); }
277 public virtual SqlDateTime GetSqlDateTime(object o) { return MemberAccessor.GetSqlDateTime(o); }
278 public virtual SqlDecimal GetSqlDecimal (object o) { return MemberAccessor.GetSqlDecimal (o); }
279 public virtual SqlMoney GetSqlMoney (object o) { return MemberAccessor.GetSqlMoney (o); }
280 public virtual SqlGuid GetSqlGuid (object o) { return MemberAccessor.GetSqlGuid (o); }
281 public virtual SqlString GetSqlString (object o) { return MemberAccessor.GetSqlString (o); }
282
283 #endif
284
285 public virtual void SetValue(object o, object value)
286 {
287 MemberAccessor.SetValue(o, value);
288 }
289
290 public virtual void SetNull (object o) { SetValue(o, null); }
291
292 // Simple type setters.
293 //
294 [CLSCompliant(false)]
295 public virtual void SetSByte (object o, SByte value) { MemberAccessor.SetSByte (o, value); }
296 public virtual void SetInt16 (object o, Int16 value) { MemberAccessor.SetInt16 (o, value); }
297 public virtual void SetInt32 (object o, Int32 value) { MemberAccessor.SetInt32 (o, value); }
298 public virtual void SetInt64 (object o, Int64 value) { MemberAccessor.SetInt64 (o, value); }
299
300 public virtual void SetByte (object o, Byte value) { MemberAccessor.SetByte (o, value); }
301 [CLSCompliant(false)]
302 public virtual void SetUInt16 (object o, UInt16 value) { MemberAccessor.SetUInt16 (o, value); }
303 [CLSCompliant(false)]
304 public virtual void SetUInt32 (object o, UInt32 value) { MemberAccessor.SetUInt32 (o, value); }
305 [CLSCompliant(false)]
306 public virtual void SetUInt64 (object o, UInt64 value) { MemberAccessor.SetUInt64 (o, value); }
307
308 public virtual void SetBoolean (object o, Boolean value) { MemberAccessor.SetBoolean (o, value); }
309 public virtual void SetChar (object o, Char value) { MemberAccessor.SetChar (o, value); }
310 public virtual void SetSingle (object o, Single value) { MemberAccessor.SetSingle (o, value); }
311 public virtual void SetDouble (object o, Double value) { MemberAccessor.SetDouble (o, value); }
312 public virtual void SetDecimal (object o, Decimal value) { MemberAccessor.SetDecimal (o, value); }
313 public virtual void SetGuid (object o, Guid value) { MemberAccessor.SetGuid (o, value); }
314 public virtual void SetDateTime(object o, DateTime value) { MemberAccessor.SetDateTime(o, value); }
315 public virtual void SetDateTimeOffset(object o, DateTimeOffset value) { MemberAccessor.SetDateTimeOffset(o, value); }
316
317 // Nullable type setters.
318 //
319 [CLSCompliant(false)]
320 public virtual void SetNullableSByte (object o, SByte? value) { MemberAccessor.SetNullableSByte (o, value); }
321 public virtual void SetNullableInt16 (object o, Int16? value) { MemberAccessor.SetNullableInt16 (o, value); }
322 public virtual void SetNullableInt32 (object o, Int32? value) { MemberAccessor.SetNullableInt32 (o, value); }
323 public virtual void SetNullableInt64 (object o, Int64? value) { MemberAccessor.SetNullableInt64 (o, value); }
324
325 public virtual void SetNullableByte (object o, Byte? value) { MemberAccessor.SetNullableByte (o, value); }
326 [CLSCompliant(false)]
327 public virtual void SetNullableUInt16 (object o, UInt16? value) { MemberAccessor.SetNullableUInt16 (o, value); }
328 [CLSCompliant(false)]
329 public virtual void SetNullableUInt32 (object o, UInt32? value) { MemberAccessor.SetNullableUInt32 (o, value); }
330 [CLSCompliant(false)]
331 public virtual void SetNullableUInt64 (object o, UInt64? value) { MemberAccessor.SetNullableUInt64 (o, value); }
332
333 public virtual void SetNullableBoolean (object o, Boolean? value) { MemberAccessor.SetNullableBoolean (o, value); }
334 public virtual void SetNullableChar (object o, Char? value) { MemberAccessor.SetNullableChar (o, value); }
335 public virtual void SetNullableSingle (object o, Single? value) { MemberAccessor.SetNullableSingle (o, value); }
336 public virtual void SetNullableDouble (object o, Double? value) { MemberAccessor.SetNullableDouble (o, value); }
337 public virtual void SetNullableDecimal (object o, Decimal? value) { MemberAccessor.SetNullableDecimal (o, value); }
338 public virtual void SetNullableGuid (object o, Guid? value) { MemberAccessor.SetNullableGuid (o, value); }
339 public virtual void SetNullableDateTime(object o, DateTime? value) { MemberAccessor.SetNullableDateTime(o, value); }
340 public virtual void SetNullableDateTimeOffset(object o, DateTimeOffset? value) { MemberAccessor.SetNullableDateTimeOffset(o, value); }
341
342 #if !SILVERLIGHT
343
344 // SQL type setters.
345 //
346 public virtual void SetSqlByte (object o, SqlByte value) { MemberAccessor.SetSqlByte (o, value); }
347 public virtual void SetSqlInt16 (object o, SqlInt16 value) { MemberAccessor.SetSqlInt16 (o, value); }
348 public virtual void SetSqlInt32 (object o, SqlInt32 value) { MemberAccessor.SetSqlInt32 (o, value); }
349 public virtual void SetSqlInt64 (object o, SqlInt64 value) { MemberAccessor.SetSqlInt64 (o, value); }
350 public virtual void SetSqlSingle (object o, SqlSingle value) { MemberAccessor.SetSqlSingle (o, value); }
351 public virtual void SetSqlBoolean (object o, SqlBoolean value) { MemberAccessor.SetSqlBoolean (o, value); }
352 public virtual void SetSqlDouble (object o, SqlDouble value) { MemberAccessor.SetSqlDouble (o, value); }
353 public virtual void SetSqlDateTime(object o, SqlDateTime value) { MemberAccessor.SetSqlDateTime(o, value); }
354 public virtual void SetSqlDecimal (object o, SqlDecimal value) { MemberAccessor.SetSqlDecimal (o, value); }
355 public virtual void SetSqlMoney (object o, SqlMoney value) { MemberAccessor.SetSqlMoney (o, value); }
356 public virtual void SetSqlGuid (object o, SqlGuid value) { MemberAccessor.SetSqlGuid (o, value); }
357 public virtual void SetSqlString (object o, SqlString value) { MemberAccessor.SetSqlString (o, value); }
358
359 #endif
360
361 public virtual void CloneValue (object source, object dest) { MemberAccessor.CloneValue(source, dest); }
362
363 #endregion
364
365 #region Intermal Mappers
366
367 #region Complex Mapper
368
369 internal sealed class ComplexMapper : MemberMapper
370 {
371 public ComplexMapper(MemberMapper memberMapper)
372 {
373 _mapper = memberMapper;
374 }
375
376 private readonly MemberMapper _mapper;
377
378 public override void Init(MapMemberInfo mapMemberInfo)
379 {
380 base.Init(mapMemberInfo);
381
382 var attr = MemberAccessor.GetAttribute<NoInstanceAttribute>();
383
384 if (attr != null)
385 {
386 _createInstance = true;
387 }
388 }
389
390 bool _createInstance;
391 TypeAccessor _typeAccessor;
392
393 object GetObject(object o)
394 {
395 var obj = MemberAccessor.GetValue(o);
396
397 if (_createInstance && obj == null)
398 {
399 if (_typeAccessor == null)
400 _typeAccessor = TypeAccessor.GetAccessor(MemberAccessor.Type);
401
402 obj = _typeAccessor.CreateInstanceEx();
403
404 MemberAccessor.SetValue(o, obj);
405 }
406
407 return obj;
408 }
409
410 #region GetValue
411
412 public override object GetValue(object o)
413 {
414 var obj = MemberAccessor.GetValue(o);
415 return obj == null? null: _mapper.GetValue(obj);
416 }
417
418 // Simple type getters.
419 //
420 public override SByte GetSByte (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultSByteNullValue: _mapper.GetSByte (obj); }
421 public override Int16 GetInt16 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultInt16NullValue: _mapper.GetInt16 (obj); }
422 public override Int32 GetInt32 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultInt32NullValue: _mapper.GetInt32 (obj); }
423 public override Int64 GetInt64 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultInt64NullValue: _mapper.GetInt64 (obj); }
424
425 public override Byte GetByte (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultByteNullValue: _mapper.GetByte (obj); }
426 public override UInt16 GetUInt16 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultUInt16NullValue: _mapper.GetUInt16 (obj); }
427 public override UInt32 GetUInt32 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultUInt32NullValue: _mapper.GetUInt32 (obj); }
428 public override UInt64 GetUInt64 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultUInt64NullValue: _mapper.GetUInt64 (obj); }
429
430 public override Boolean GetBoolean (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultBooleanNullValue: _mapper.GetBoolean (obj); }
431 public override Char GetChar (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultCharNullValue: _mapper.GetChar (obj); }
432 public override Single GetSingle (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultSingleNullValue: _mapper.GetSingle (obj); }
433 public override Double GetDouble (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultDoubleNullValue: _mapper.GetDouble (obj); }
434 public override Decimal GetDecimal (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultDecimalNullValue: _mapper.GetDecimal (obj); }
435 public override Guid GetGuid (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultGuidNullValue: _mapper.GetGuid (obj); }
436 public override DateTime GetDateTime(object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultDateTimeNullValue: _mapper.GetDateTime(obj); }
437 public override DateTimeOffset GetDateTimeOffset(object o) { var obj = MemberAccessor.GetValue(o); return obj == null? MappingSchema.DefaultDateTimeOffsetNullValue: _mapper.GetDateTimeOffset(obj); }
438
439 // Nullable type getters.
440 //
441 public override SByte? GetNullableSByte (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableSByte (obj); }
442 public override Int16? GetNullableInt16 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableInt16 (obj); }
443 public override Int32? GetNullableInt32 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableInt32 (obj); }
444 public override Int64? GetNullableInt64 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableInt64 (obj); }
445
446 public override Byte? GetNullableByte (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableByte (obj); }
447 public override UInt16? GetNullableUInt16 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableUInt16 (obj); }
448 public override UInt32? GetNullableUInt32 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableUInt32 (obj); }
449 public override UInt64? GetNullableUInt64 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableUInt64 (obj); }
450
451 public override Boolean? GetNullableBoolean (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableBoolean (obj); }
452 public override Char? GetNullableChar (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableChar (obj); }
453 public override Single? GetNullableSingle (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableSingle (obj); }
454 public override Double? GetNullableDouble (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableDouble (obj); }
455 public override Decimal? GetNullableDecimal (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableDecimal (obj); }
456 public override Guid? GetNullableGuid (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableGuid (obj); }
457 public override DateTime? GetNullableDateTime(object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableDateTime(obj); }
458 public override DateTimeOffset? GetNullableDateTimeOffset(object o) { var obj = MemberAccessor.GetValue(o); return obj == null? null: _mapper.GetNullableDateTimeOffset(obj); }
459
460 #if !SILVERLIGHT
461
462 // SQL type getters.
463 //
464 public override SqlByte GetSqlByte (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlByte. Null: _mapper.GetSqlByte (obj); }
465 public override SqlInt16 GetSqlInt16 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlInt16. Null: _mapper.GetSqlInt16 (obj); }
466 public override SqlInt32 GetSqlInt32 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlInt32. Null: _mapper.GetSqlInt32 (obj); }
467 public override SqlInt64 GetSqlInt64 (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlInt64. Null: _mapper.GetSqlInt64 (obj); }
468 public override SqlSingle GetSqlSingle (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlSingle. Null: _mapper.GetSqlSingle (obj); }
469 public override SqlBoolean GetSqlBoolean (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlBoolean. Null: _mapper.GetSqlBoolean (obj); }
470 public override SqlDouble GetSqlDouble (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlDouble. Null: _mapper.GetSqlDouble (obj); }
471 public override SqlDateTime GetSqlDateTime(object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlDateTime.Null: _mapper.GetSqlDateTime(obj); }
472 public override SqlDecimal GetSqlDecimal (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlDecimal. Null: _mapper.GetSqlDecimal (obj); }
473 public override SqlMoney GetSqlMoney (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlMoney. Null: _mapper.GetSqlMoney (obj); }
474 public override SqlGuid GetSqlGuid (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlGuid. Null: _mapper.GetSqlGuid (obj); }
475 public override SqlString GetSqlString (object o) { var obj = MemberAccessor.GetValue(o); return obj == null? SqlString. Null: _mapper.GetSqlString (obj); }
476
477 #endif
478
479 #endregion
480
481 #region SetValue
482
483 public override void SetValue(object o, object value)
484 {
485 var obj = MemberAccessor.GetValue(o);
486
487 if (obj != null)
488 _mapper.SetValue(obj, value);
489 }
490
491 public override void SetSByte (object o, SByte value) { var obj = GetObject(o); if (obj != null) _mapper.SetSByte (obj, value); }
492 public override void SetInt16 (object o, Int16 value) { var obj = GetObject(o); if (obj != null) _mapper.SetInt16 (obj, value); }
493 public override void SetInt32 (object o, Int32 value) { var obj = GetObject(o); if (obj != null) _mapper.SetInt32 (obj, value); }
494 public override void SetInt64 (object o, Int64 value) { var obj = GetObject(o); if (obj != null) _mapper.SetInt64 (obj, value); }
495
496 public override void SetByte (object o, Byte value) { var obj = GetObject(o); if (obj != null) _mapper.SetByte (obj, value); }
497 public override void SetUInt16 (object o, UInt16 value) { var obj = GetObject(o); if (obj != null) _mapper.SetUInt16 (obj, value); }
498 public override void SetUInt32 (object o, UInt32 value) { var obj = GetObject(o); if (obj != null) _mapper.SetUInt32 (obj, value); }
499 public override void SetUInt64 (object o, UInt64 value) { var obj = GetObject(o); if (obj != null) _mapper.SetUInt64 (obj, value); }
500
501 public override void SetBoolean (object o, Boolean value) { var obj = GetObject(o); if (obj != null) _mapper.SetBoolean (obj, value); }
502 public override void SetChar (object o, Char value) { var obj = GetObject(o); if (obj != null) _mapper.SetChar (obj, value); }
503 public override void SetSingle (object o, Single value) { var obj = GetObject(o); if (obj != null) _mapper.SetSingle (obj, value); }
504 public override void SetDouble (object o, Double value) { var obj = GetObject(o); if (obj != null) _mapper.SetDouble (obj, value); }
505 public override void SetDecimal (object o, Decimal value) { var obj = GetObject(o); if (obj != null) _mapper.SetDecimal (obj, value); }
506 public override void SetGuid (object o, Guid value) { var obj = GetObject(o); if (obj != null) _mapper.SetGuid (obj, value); }
507 public override void SetDateTime(object o, DateTime value) { var obj = GetObject(o); if (obj != null) _mapper.SetDateTime(obj, value); }
508 public override void SetDateTimeOffset(object o, DateTimeOffset value) { var obj = GetObject(o); if (obj != null) _mapper.SetDateTimeOffset(obj, value); }
509
510 // Nullable type setters.
511 //
512 public override void SetNullableSByte (object o, SByte? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableSByte (obj, value); }
513 public override void SetNullableInt16 (object o, Int16? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableInt16 (obj, value); }
514 public override void SetNullableInt32 (object o, Int32? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableInt32 (obj, value); }
515 public override void SetNullableInt64 (object o, Int64? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableInt64 (obj, value); }
516
517 public override void SetNullableByte (object o, Byte? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableByte (obj, value); }
518 public override void SetNullableUInt16 (object o, UInt16? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableUInt16 (obj, value); }
519 public override void SetNullableUInt32 (object o, UInt32? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableUInt32 (obj, value); }
520 public override void SetNullableUInt64 (object o, UInt64? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableUInt64 (obj, value); }
521
522 public override void SetNullableBoolean (object o, Boolean? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableBoolean (obj, value); }
523 public override void SetNullableChar (object o, Char? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableChar (obj, value); }
524 public override void SetNullableSingle (object o, Single? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableSingle (obj, value); }
525 public override void SetNullableDouble (object o, Double? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableDouble (obj, value); }
526 public override void SetNullableDecimal (object o, Decimal? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableDecimal (obj, value); }
527 public override void SetNullableGuid (object o, Guid? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableGuid (obj, value); }
528 public override void SetNullableDateTime(object o, DateTime? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableDateTime(obj, value); }
529 public override void SetNullableDateTimeOffset(object o, DateTimeOffset? value) { var obj = GetObject(o); if (obj != null) _mapper.SetNullableDateTimeOffset(obj, value); }
530
531 #if !SILVERLIGHT
532
533 // SQL type setters.
534 //
535 public override void SetSqlByte (object o, SqlByte value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlByte (obj, value); }
536 public override void SetSqlInt16 (object o, SqlInt16 value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlInt16 (obj, value); }
537 public override void SetSqlInt32 (object o, SqlInt32 value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlInt32 (obj, value); }
538 public override void SetSqlInt64 (object o, SqlInt64 value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlInt64 (obj, value); }
539 public override void SetSqlSingle (object o, SqlSingle value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlSingle (obj, value); }
540 public override void SetSqlBoolean (object o, SqlBoolean value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlBoolean (obj, value); }
541 public override void SetSqlDouble (object o, SqlDouble value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlDouble (obj, value); }
542 public override void SetSqlDateTime(object o, SqlDateTime value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlDateTime(obj, value); }
543 public override void SetSqlDecimal (object o, SqlDecimal value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlDecimal (obj, value); }
544 public override void SetSqlMoney (object o, SqlMoney value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlMoney (obj, value); }
545 public override void SetSqlGuid (object o, SqlGuid value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlGuid (obj, value); }
546 public override void SetSqlString (object o, SqlString value) { var obj = GetObject(o); if (obj != null) _mapper.SetSqlString (obj, value); }
547
548 #endif
549
550 #endregion
551 }
552
553 #endregion
554
555 #region Primitive Mappers
556
557 private static MemberMapper GetPrimitiveMemberMapper(MapMemberInfo mi)
558 {
559 if (mi.MapValues != null)
560 return null;
561
562 var n = mi.Nullable;
563 var type = mi.MemberAccessor.UnderlyingType;
564
565 if (type == typeof(SByte)) return n? new SByteMapper. Nullable(): new SByteMapper();
566 if (type == typeof(Int16)) return n? new Int16Mapper. Nullable(): new Int16Mapper();
567 if (type == typeof(Int32)) return n? new Int32Mapper. Nullable(): new Int32Mapper();
568 if (type == typeof(Int64)) return n? new Int64Mapper. Nullable(): new Int64Mapper();
569 if (type == typeof(Byte)) return n? new ByteMapper. Nullable(): new ByteMapper();
570 if (type == typeof(UInt16)) return n? new UInt16Mapper. Nullable(): new UInt16Mapper();
571 if (type == typeof(UInt32)) return n? new UInt32Mapper. Nullable(): new UInt32Mapper();
572 if (type == typeof(UInt64)) return n? new UInt64Mapper. Nullable(): new UInt64Mapper();
573 if (type == typeof(Single)) return n? new SingleMapper. Nullable(): new SingleMapper();
574 if (type == typeof(Double)) return n? new DoubleMapper. Nullable(): new DoubleMapper();
575 if (type == typeof(Char)) return n? new CharMapper. Nullable(): new CharMapper();
576 if (type == typeof(Boolean)) return n? new BooleanMapper.Nullable(): new BooleanMapper();
577
578 throw new InvalidOperationException();
579 }
580
581 #endregion
582
583 #region Simple Mappers
584
585 private static MemberMapper GetSimpleMemberMapper(MapMemberInfo mi)
586 {
587 if (mi.MapValues != null)
588 return null;
589
590 var n = mi.Nullable;
591 var type = mi.Type;
592
593 if (type == typeof(String))
594 if (mi.Trimmable) return n? new StringMapper.Trimmable.NullableT(): new StringMapper.Trimmable();
595 else return n? new StringMapper.Nullable() : new StringMapper();
596
597 if (type == typeof(DateTime)) return n? new DateTimeMapper.Nullable() : new DateTimeMapper();
598 if (type == typeof(DateTimeOffset)) return n? new DateTimeOffsetMapper.Nullable() : new DateTimeOffsetMapper();
599 if (type == typeof(Decimal)) return n? new DecimalMapper.Nullable() : new DecimalMapper();
600 if (type == typeof(Guid)) return n? new GuidMapper.Nullable() : new GuidMapper();
601 if (type == typeof(Stream)) return n? new StreamMapper.Nullable() : new StreamMapper();
602 #if !SILVERLIGHT
603 if (type == typeof(XmlReader)) return n? new XmlReaderMapper.Nullable() : new XmlReaderMapper();
604 if (type == typeof(XmlDocument)) return n? new XmlDocumentMapper.Nullable() : new XmlDocumentMapper();
605 if (type == typeof(XElement)) return n? new XElementMapper.Nullable() : new XElementMapper();
606 #endif
607 return null;
608 }
609
610 class StringMapper : MemberMapper
611 {
612 string _nullValue;
613
614 public override void SetValue(object o, object value)
615 {
616 MemberAccessor.SetValue(
617 o,
618 value is string? value:
619 value == null? _nullValue:
620 MappingSchema.ConvertToString(value));
621 }
622
623 public override void Init(MapMemberInfo mapMemberInfo)
624 {
625 if (mapMemberInfo == null) throw new ArgumentNullException("mapMemberInfo");
626
627 if (mapMemberInfo.NullValue != null)
628 _nullValue = Convert.ToString(mapMemberInfo.NullValue);
629
630 base.Init(mapMemberInfo);
631 }
632
633 public class Nullable : StringMapper
634 {
635 public override object GetValue(object o)
636 {
637 var value = MemberAccessor.GetValue(o);
638 return (string)value == _nullValue? null: value;
639 }
640 }
641
642 public class Trimmable : StringMapper
643 {
644 public override void SetValue(object o, object value)
645 {
646 MemberAccessor.SetValue(
647 o, value == null? _nullValue: MappingSchema.ConvertToString(value).TrimEnd(_trim));
648 }
649
650 public class NullableT : Trimmable
651 {
652 public override object GetValue(object o)
653 {
654 var value = MemberAccessor.GetValue(o);
655 return (string)value == _nullValue? null: value;
656 }
657 }
658 }
659 }
660
661 #endregion
662
663 #region Nullable Mappers
664
665 private static MemberMapper GetNullableMemberMapper(MapMemberInfo mi)
666 {
667 var type = mi.Type;
668
669 if (type.IsGenericType == false || mi.MapValues != null)
670 return null;
671
672 var underlyingType = Nullable.GetUnderlyingType(type);
673
674 if (underlyingType == null)
675 return null;
676
677 if (underlyingType.IsEnum)
678 {
679 underlyingType = Enum.GetUnderlyingType(underlyingType);
680
681 if (underlyingType == typeof(SByte)) return new NullableSByteMapper. Enum();
682 if (underlyingType == typeof(Int16)) return new NullableInt16Mapper. Enum();
683 if (underlyingType == typeof(Int32)) return new NullableInt32Mapper. Enum();
684 if (underlyingType == typeof(Int64)) return new NullableInt64Mapper. Enum();
685 if (underlyingType == typeof(Byte)) return new NullableByteMapper. Enum();
686 if (underlyingType == typeof(UInt16)) return new NullableUInt16Mapper.Enum();
687 if (underlyingType == typeof(UInt32)) return new NullableUInt32Mapper.Enum();
688 if (underlyingType == typeof(UInt64)) return new NullableUInt64Mapper.Enum();
689 }
690 else
691 {
692 if (underlyingType == typeof(SByte)) return new NullableSByteMapper();
693 if (underlyingType == typeof(Int16)) return new NullableInt16Mapper();
694 if (underlyingType == typeof(Int32)) return new NullableInt32Mapper();
695 if (underlyingType == typeof(Int64)) return new NullableInt64Mapper();
696 if (underlyingType == typeof(Byte)) return new NullableByteMapper();
697 if (underlyingType == typeof(UInt16)) return new NullableUInt16Mapper();
698 if (underlyingType == typeof(UInt32)) return new NullableUInt32Mapper();
699 if (underlyingType == typeof(UInt64)) return new NullableUInt64Mapper();
700 if (underlyingType == typeof(Char)) return new NullableCharMapper();
701 if (underlyingType == typeof(Single)) return new NullableSingleMapper();
702 if (underlyingType == typeof(Boolean)) return new NullableBooleanMapper();
703 if (underlyingType == typeof(Double)) return new NullableDoubleMapper();
704 if (underlyingType == typeof(DateTime)) return new NullableDateTimeMapper();
705 if (underlyingType == typeof(Decimal)) return new NullableDecimalMapper();
706 if (underlyingType == typeof(Guid)) return new NullableGuidMapper();
707 }
708
709 return null;
710 }
711
712 abstract class NullableEnumMapper : MemberMapper
713 {
714 protected Type MemberType;
715 protected Type UnderlyingType;
716
717 public override void Init(MapMemberInfo mapMemberInfo)
718 {
719 if (mapMemberInfo == null) throw new ArgumentNullException("mapMemberInfo");
720
721 MemberType = Nullable.GetUnderlyingType(mapMemberInfo.Type);
722 UnderlyingType = mapMemberInfo.MemberAccessor.UnderlyingType;
723
724 base.Init(mapMemberInfo);
725 }
726 }
727
728 #endregion
729
730 #region SqlTypes
731
732 #if !SILVERLIGHT
733
734 private static MemberMapper GetSqlTypeMemberMapper(MapMemberInfo mi)
735 {
736 var type = mi.Type;
737
738 if (TypeHelper.IsSameOrParent(typeof(INullable), type) == false)
739 return null;
740
741 var d = mi.MapValues != null;
742
743 if (type == typeof(SqlByte)) return d? new SqlByteMapper. Default(): new SqlByteMapper();
744 if (type == typeof(SqlInt16)) return d? new SqlInt16Mapper. Default(): new SqlInt16Mapper();
745 if (type == typeof(SqlInt32)) return d? new SqlInt32Mapper. Default(): new SqlInt32Mapper();
746 if (type == typeof(SqlInt64)) return d? new SqlInt64Mapper. Default(): new SqlInt64Mapper();
747 if (type == typeof(SqlSingle)) return d? new SqlSingleMapper. Default(): new SqlSingleMapper();
748 if (type == typeof(SqlBoolean)) return d? new SqlBooleanMapper. Default(): new SqlBooleanMapper();
749 if (type == typeof(SqlDouble)) return d? new SqlDoubleMapper. Default(): new SqlDoubleMapper();
750 if (type == typeof(SqlDateTime)) return d? new SqlDateTimeMapper.Default(): new SqlDateTimeMapper();
751 if (type == typeof(SqlDecimal)) return d? new SqlDecimalMapper. Default(): new SqlDecimalMapper();
752 if (type == typeof(SqlMoney)) return d? new SqlMoneyMapper. Default(): new SqlMoneyMapper();
753 if (type == typeof(SqlGuid)) return d? new SqlGuidMapper. Default(): new SqlGuidMapper();
754 if (type == typeof(SqlString)) return d? new SqlStringMapper. Default(): new SqlStringMapper();
755
756 return null;
757 }
758
759 #endif
760
761 #endregion
762
763 #endregion
764
765 #region MapFrom, MapTo
766
767 protected object MapFrom(object value)
768 {
769 return MapFrom(value, MapMemberInfo);
770 }
771
772 static readonly char[] _trim = { ' ' };
773
774 protected object MapFrom(object value, MapMemberInfo mapInfo)
775 {
776 if (mapInfo == null) throw new ArgumentNullException("mapInfo");
777
778 if (value == null)
779 return mapInfo.NullValue;
780
781 if (mapInfo.Trimmable && value is string)
782 value = value.ToString().TrimEnd(_trim);
783
784 if (mapInfo.MapValues != null)
785 {
786 object origValue;
787 if (mapInfo.TryGetOrigValue(value, out origValue))
788 return origValue;
789
790 // 2012-09-18 ili: this is too slow when we have for ex. enum with 50+ values
791 //
792 //var comp = (IComparable)value;
793
794 //foreach (var mv in mapInfo.MapValues)
795 //foreach (var mapValue in mv.MapValues)
796 //{
797 // try
798 // {
799 // if (comp.CompareTo(mapValue) == 0)
800 // return mv.OrigValue;
801 // }
802 // catch
803 // {
804 // }
805 //}
806
807 // Default value.
808 //
809 if (mapInfo.DefaultValue != null)
810 return mapInfo.DefaultValue;
811 }
812
813 var valueType = value.GetType();
814 var memberType = mapInfo.Type;
815
816 if (!TypeHelper.IsSameOrParent(memberType, valueType))
817 {
818 if (memberType.IsGenericType)
819 {
820 var underlyingType = Nullable.GetUnderlyingType(memberType);
821
822 if (valueType == underlyingType)
823 return value;
824
825 memberType = underlyingType;
826 }
827
828 if (memberType.IsEnum)
829 {
830 var underlyingType = mapInfo.MemberAccessor.UnderlyingType;
831
832 if (valueType != underlyingType)
833 //value = _mappingSchema.ConvertChangeType(value, underlyingType);
834 return MapFrom(MappingSchema.ConvertChangeType(value, underlyingType), mapInfo);
835
836 //value = Enum.Parse(type, Enum.GetName(type, value));
837 value = Enum.ToObject(memberType, value);
838 }
839 else
840 {
841 value = MappingSchema.ConvertChangeType(value, memberType);
842 }
843 }
844
845 return value;
846 }
847
848 protected object MapTo(object value)
849 {
850 return MapTo(value, MapMemberInfo);
851 }
852
853 protected static object MapTo(object value, MapMemberInfo mapInfo)
854 {
855 if (mapInfo == null) throw new ArgumentNullException("mapInfo");
856
857 if (value == null)
858 return null;
859
860 if (mapInfo.Nullable && mapInfo.NullValue != null)
861 {
862 try
863 {
864 var comp = (IComparable)value;
865
866 if (comp.CompareTo(mapInfo.NullValue) == 0)
867 return null;
868 }
869 catch
870 {
871 }
872 }
873
874 if (mapInfo.MapValues != null)
875 {
876 object mapValue;
877 if (mapInfo.TryGetMapValue(value, out mapValue))
878 return mapValue;
879
880 //2011-11-16 ili: this is too slow when we have for ex. enum with 50+ values
881 //
882 //var comp = (IComparable)value;
883
884 //foreach (var mv in mapInfo.MapValues)
885 //{
886 // try
887 // {
888 // if (comp.CompareTo(mv.OrigValue) == 0)
889 // return mv.MapValues[0];
890 // }
891 // catch
892 // {
893 // }
894 //}
895 }
896
897 return value;
898 }
899
900 #endregion
901 }
902 }