comparison Source/Mapping/MapSetDataT.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.SqlTypes;
3
4 using BLToolkit.Reflection;
5
6 namespace BLToolkit.Mapping
7 {
8 [CLSCompliant(false)]
9 public static class MapSetData<T>
10 {
11 public abstract class MB<V>
12 {
13 public abstract void To(IMapDataDestination d, object o, int i, V v);
14 }
15
16 public static void To(IMapDataDestination d, object o, int i, T v)
17 {
18 I.To(d, o, i, v);
19 }
20
21 public static MB<T> I = GetSetter();
22 private static MB<T> GetSetter()
23 {
24 Type t = typeof(T);
25
26 // Scalar Types.
27 //
28 if (t == typeof(SByte)) return (MB<T>)(object)(new I8());
29 if (t == typeof(Int16)) return (MB<T>)(object)(new I16());
30 if (t == typeof(Int32)) return (MB<T>)(object)(new I32());
31 if (t == typeof(Int64)) return (MB<T>)(object)(new I64());
32
33 if (t == typeof(Byte)) return (MB<T>)(object)(new U8());
34 if (t == typeof(UInt16)) return (MB<T>)(object)(new U16());
35 if (t == typeof(UInt32)) return (MB<T>)(object)(new U32());
36 if (t == typeof(UInt64)) return (MB<T>)(object)(new U64());
37
38 if (t == typeof(Single)) return (MB<T>)(object)(new R4());
39 if (t == typeof(Double)) return (MB<T>)(object)(new R8());
40
41 if (t == typeof(Boolean)) return (MB<T>)(object)(new B());
42 if (t == typeof(Decimal)) return (MB<T>)(object)(new D());
43
44 if (t == typeof(Char)) return (MB<T>)(object)(new C());
45 if (t == typeof(Guid)) return (MB<T>)(object)(new G());
46 if (t == typeof(DateTime)) return (MB<T>)(object)(new DT());
47 if (t == typeof(DateTimeOffset)) return (MB<T>)(object)(new DTO());
48
49 // Enums.
50 //
51 if (t.IsEnum)
52 {
53 t = Enum.GetUnderlyingType(t);
54
55 if (t == typeof(SByte)) return new EI8<T>();
56 if (t == typeof(Int16)) return new EI16<T>();
57 if (t == typeof(Int32)) return new EI32<T>();
58 if (t == typeof(Int64)) return new EI64<T>();
59
60 if (t == typeof(Byte)) return new EU8<T>();
61 if (t == typeof(UInt16)) return new EU16<T>();
62 if (t == typeof(UInt32)) return new EU32<T>();
63 if (t == typeof(UInt64)) return new EU64<T>();
64 }
65
66 // Nullable Types.
67 //
68 if (t == typeof(SByte?)) return (MB<T>)(object)(new NI8());
69 if (t == typeof(Int16?)) return (MB<T>)(object)(new NI16());
70 if (t == typeof(Int32?)) return (MB<T>)(object)(new NI32());
71 if (t == typeof(Int64?)) return (MB<T>)(object)(new NI64());
72
73 if (t == typeof(Byte?)) return (MB<T>)(object)(new NU8());
74 if (t == typeof(UInt16?)) return (MB<T>)(object)(new NU16());
75 if (t == typeof(UInt32?)) return (MB<T>)(object)(new NU32());
76 if (t == typeof(UInt64?)) return (MB<T>)(object)(new NU64());
77
78 if (t == typeof(Single?)) return (MB<T>)(object)(new NR4());
79 if (t == typeof(Double?)) return (MB<T>)(object)(new NR8());
80
81 if (t == typeof(Boolean?)) return (MB<T>)(object)(new NB());
82 if (t == typeof(Decimal?)) return (MB<T>)(object)(new ND());
83
84 if (t == typeof(Char?)) return (MB<T>)(object)(new NC());
85 if (t == typeof(Guid?)) return (MB<T>)(object)(new NG());
86 if (t == typeof(DateTime?)) return (MB<T>)(object)(new NDT());
87 if (t == typeof(DateTimeOffset?)) return (MB<T>)(object)(new NDTO());
88
89 // Nullable Enums.
90 //
91 if (TypeHelper.IsNullable(t) && Nullable.GetUnderlyingType(t).IsEnum)
92 {
93 Type enumType = Nullable.GetUnderlyingType(t);
94 t = Enum.GetUnderlyingType(enumType);
95
96 if (t == typeof(SByte)) return (MB<T>)Activator.CreateInstance(typeof(NEI8<>).MakeGenericType(typeof(T), enumType));
97 if (t == typeof(Int16)) return (MB<T>)Activator.CreateInstance(typeof(NEI16<>).MakeGenericType(typeof(T), enumType));
98 if (t == typeof(Int32)) return (MB<T>)Activator.CreateInstance(typeof(NEI32<>).MakeGenericType(typeof(T), enumType));
99 if (t == typeof(Int64)) return (MB<T>)Activator.CreateInstance(typeof(NEI64<>).MakeGenericType(typeof(T), enumType));
100
101 if (t == typeof(Byte)) return (MB<T>)Activator.CreateInstance(typeof(NEU8<>).MakeGenericType(typeof(T), enumType));
102 if (t == typeof(UInt16)) return (MB<T>)Activator.CreateInstance(typeof(NEU16<>).MakeGenericType(typeof(T), enumType));
103 if (t == typeof(UInt32)) return (MB<T>)Activator.CreateInstance(typeof(NEU32<>).MakeGenericType(typeof(T), enumType));
104 if (t == typeof(UInt64)) return (MB<T>)Activator.CreateInstance(typeof(NEU64<>).MakeGenericType(typeof(T), enumType));
105 }
106
107 #if !SILVERLIGHT
108
109 // SqlTypes.
110 //
111 if (t == typeof(SqlString)) return (MB<T>)(object)(new dbS());
112
113 if (t == typeof(SqlByte)) return (MB<T>)(object)(new dbU8());
114 if (t == typeof(SqlInt16)) return (MB<T>)(object)(new dbI16());
115 if (t == typeof(SqlInt32)) return (MB<T>)(object)(new dbI32());
116 if (t == typeof(SqlInt64)) return (MB<T>)(object)(new dbI64());
117
118 if (t == typeof(SqlSingle)) return (MB<T>)(object)(new dbR4());
119 if (t == typeof(SqlDouble)) return (MB<T>)(object)(new dbR8());
120 if (t == typeof(SqlDecimal)) return (MB<T>)(object)(new dbD());
121 if (t == typeof(SqlMoney)) return (MB<T>)(object)(new dbM());
122
123 if (t == typeof(SqlBoolean)) return (MB<T>)(object)(new dbB());
124 if (t == typeof(SqlGuid)) return (MB<T>)(object)(new dbG());
125 if (t == typeof(SqlDateTime)) return (MB<T>)(object)(new dbDT());
126
127 #endif
128
129 return new Default<T>();
130 }
131
132 // Default setter.
133 //
134 public sealed class Default<V> : MB<V> { public override void To(IMapDataDestination d, object o, int i, V v) { d.SetValue (o, i, v); } }
135
136 // Scalar Types.
137 //
138 sealed class I8 : MB<SByte> { public override void To(IMapDataDestination d, object o, int i, SByte v) { d.SetSByte (o, i, v); } }
139 sealed class I16 : MB<Int16> { public override void To(IMapDataDestination d, object o, int i, Int16 v) { d.SetInt16 (o, i, v); } }
140 sealed class I32 : MB<Int32> { public override void To(IMapDataDestination d, object o, int i, Int32 v) { d.SetInt32 (o, i, v); } }
141 sealed class I64 : MB<Int64> { public override void To(IMapDataDestination d, object o, int i, Int64 v) { d.SetInt64 (o, i, v); } }
142
143 sealed class U8 : MB<Byte> { public override void To(IMapDataDestination d, object o, int i, Byte v) { d.SetByte (o, i, v); } }
144 sealed class U16 : MB<UInt16> { public override void To(IMapDataDestination d, object o, int i, UInt16 v) { d.SetUInt16 (o, i, v); } }
145 sealed class U32 : MB<UInt32> { public override void To(IMapDataDestination d, object o, int i, UInt32 v) { d.SetUInt32 (o, i, v); } }
146 sealed class U64 : MB<UInt64> { public override void To(IMapDataDestination d, object o, int i, UInt64 v) { d.SetUInt64 (o, i, v); } }
147
148 sealed class R4 : MB<Single> { public override void To(IMapDataDestination d, object o, int i, Single v) { d.SetSingle (o, i, v); } }
149 sealed class R8 : MB<Double> { public override void To(IMapDataDestination d, object o, int i, Double v) { d.SetDouble (o, i, v); } }
150
151 sealed class B : MB<Boolean> { public override void To(IMapDataDestination d, object o, int i, Boolean v) { d.SetBoolean (o, i, v); } }
152 sealed class D : MB<Decimal> { public override void To(IMapDataDestination d, object o, int i, Decimal v) { d.SetDecimal (o, i, v); } }
153
154 sealed class C : MB<Char> { public override void To(IMapDataDestination d, object o, int i, Char v) { d.SetChar (o, i, v); } }
155 sealed class G : MB<Guid> { public override void To(IMapDataDestination d, object o, int i, Guid v) { d.SetGuid (o, i, v); } }
156 sealed class DT : MB<DateTime> { public override void To(IMapDataDestination d, object o, int i, DateTime v) { d.SetDateTime (o, i, v); } }
157 sealed class DTO : MB<DateTimeOffset>{ public override void To(IMapDataDestination d, object o, int i, DateTimeOffset v) { d.SetDateTimeOffset (o, i, v); } }
158
159 // Enums.
160 //
161 sealed class EI8<E> : MB<E> { public override void To(IMapDataDestination d, object o, int i, E v) { d.SetSByte (o, i, (SByte)(object)v); } }
162 sealed class EI16<E> : MB<E> { public override void To(IMapDataDestination d, object o, int i, E v) { d.SetInt16 (o, i, (Int16)(object)v); } }
163 sealed class EI32<E> : MB<E> { public override void To(IMapDataDestination d, object o, int i, E v) { d.SetInt32 (o, i, (Int32)(object)v); } }
164 sealed class EI64<E> : MB<E> { public override void To(IMapDataDestination d, object o, int i, E v) { d.SetInt64 (o, i, (Int64)(object)v); } }
165
166 sealed class EU8<E> : MB<E> { public override void To(IMapDataDestination d, object o, int i, E v) { d.SetByte (o, i, (Byte)(object)v); } }
167 sealed class EU16<E> : MB<E> { public override void To(IMapDataDestination d, object o, int i, E v) { d.SetUInt16 (o, i, (UInt16)(object)v); } }
168 sealed class EU32<E> : MB<E> { public override void To(IMapDataDestination d, object o, int i, E v) { d.SetUInt32 (o, i, (UInt32)(object)v); } }
169 sealed class EU64<E> : MB<E> { public override void To(IMapDataDestination d, object o, int i, E v) { d.SetUInt64 (o, i, (UInt64)(object)v); } }
170
171 // Nullable Types.
172 //
173 sealed class NI8 : MB<SByte?> { public override void To(IMapDataDestination d, object o, int i, SByte? v) { d.SetNullableSByte (o, i, v); } }
174 sealed class NI16 : MB<Int16?> { public override void To(IMapDataDestination d, object o, int i, Int16? v) { d.SetNullableInt16 (o, i, v); } }
175 sealed class NI32 : MB<Int32?> { public override void To(IMapDataDestination d, object o, int i, Int32? v) { d.SetNullableInt32 (o, i, v); } }
176 sealed class NI64 : MB<Int64?> { public override void To(IMapDataDestination d, object o, int i, Int64? v) { d.SetNullableInt64 (o, i, v); } }
177
178 sealed class NU8 : MB<Byte?> { public override void To(IMapDataDestination d, object o, int i, Byte? v) { d.SetNullableByte (o, i, v); } }
179 sealed class NU16 : MB<UInt16?> { public override void To(IMapDataDestination d, object o, int i, UInt16? v) { d.SetNullableUInt16 (o, i, v); } }
180 sealed class NU32 : MB<UInt32?> { public override void To(IMapDataDestination d, object o, int i, UInt32? v) { d.SetNullableUInt32 (o, i, v); } }
181 sealed class NU64 : MB<UInt64?> { public override void To(IMapDataDestination d, object o, int i, UInt64? v) { d.SetNullableUInt64 (o, i, v); } }
182
183 sealed class NR4 : MB<Single?> { public override void To(IMapDataDestination d, object o, int i, Single? v) { d.SetNullableSingle (o, i, v); } }
184 sealed class NR8 : MB<Double?> { public override void To(IMapDataDestination d, object o, int i, Double? v) { d.SetNullableDouble (o, i, v); } }
185
186 sealed class NB : MB<Boolean?> { public override void To(IMapDataDestination d, object o, int i, Boolean? v) { d.SetNullableBoolean (o, i, v); } }
187 sealed class ND : MB<Decimal?> { public override void To(IMapDataDestination d, object o, int i, Decimal? v) { d.SetNullableDecimal (o, i, v); } }
188
189 sealed class NC : MB<Char?> { public override void To(IMapDataDestination d, object o, int i, Char? v) { d.SetNullableChar (o, i, v); } }
190 sealed class NG : MB<Guid?> { public override void To(IMapDataDestination d, object o, int i, Guid? v) { d.SetNullableGuid (o, i, v); } }
191 sealed class NDT : MB<DateTime?> { public override void To(IMapDataDestination d, object o, int i, DateTime? v) { d.SetNullableDateTime (o, i, v); } }
192 sealed class NDTO : MB<DateTimeOffset?>{ public override void To(IMapDataDestination d, object o, int i, DateTimeOffset? v) { d.SetNullableDateTimeOffset (o, i, v); } }
193
194 // Nullable Enums.
195 //
196 sealed class NEI8<E> : MB<E?> where E : struct { public override void To(IMapDataDestination d, object o, int i, E? v) { /*if (null == v) d.SetNull(o, i); else*/ d.SetSByte (o, i, (SByte)(object)v.Value); } }
197 sealed class NEI16<E> : MB<E?> where E : struct { public override void To(IMapDataDestination d, object o, int i, E? v) { /*if (null == v) d.SetNull(o, i); else*/ d.SetInt16 (o, i, (Int16)(object)v.Value); } }
198 sealed class NEI32<E> : MB<E?> where E : struct { public override void To(IMapDataDestination d, object o, int i, E? v) { /*if (null == v) d.SetNull(o, i); else*/ d.SetInt32 (o, i, (Int32)(object)v.Value); } }
199 sealed class NEI64<E> : MB<E?> where E : struct { public override void To(IMapDataDestination d, object o, int i, E? v) { /*if (null == v) d.SetNull(o, i); else*/ d.SetInt64 (o, i, (Int64)(object)v.Value); } }
200
201 sealed class NEU8<E> : MB<E?> where E : struct { public override void To(IMapDataDestination d, object o, int i, E? v) { /*if (null == v) d.SetNull(o, i); else*/ d.SetByte (o, i, (Byte)(object)v.Value); } }
202 sealed class NEU16<E> : MB<E?> where E : struct { public override void To(IMapDataDestination d, object o, int i, E? v) { /*if (null == v) d.SetNull(o, i); else*/ d.SetUInt16(o, i, (UInt16)(object)v.Value); } }
203 sealed class NEU32<E> : MB<E?> where E : struct { public override void To(IMapDataDestination d, object o, int i, E? v) { /*if (null == v) d.SetNull(o, i); else*/ d.SetUInt32(o, i, (UInt32)(object)v.Value); } }
204 sealed class NEU64<E> : MB<E?> where E : struct { public override void To(IMapDataDestination d, object o, int i, E? v) { /*if (null == v) d.SetNull(o, i); else*/ d.SetUInt64(o, i, (UInt64)(object)v.Value); } }
205
206 #if !SILVERLIGHT
207
208 // SqlTypes.
209 //
210 sealed class dbS : MB<SqlString> { public override void To(IMapDataDestination d, object o, int i, SqlString v) { d.SetSqlString (o, i, v); } }
211
212 sealed class dbU8 : MB<SqlByte> { public override void To(IMapDataDestination d, object o, int i, SqlByte v) { d.SetSqlByte (o, i, v); } }
213 sealed class dbI16 : MB<SqlInt16> { public override void To(IMapDataDestination d, object o, int i, SqlInt16 v) { d.SetSqlInt16 (o, i, v); } }
214 sealed class dbI32 : MB<SqlInt32> { public override void To(IMapDataDestination d, object o, int i, SqlInt32 v) { d.SetSqlInt32 (o, i, v); } }
215 sealed class dbI64 : MB<SqlInt64> { public override void To(IMapDataDestination d, object o, int i, SqlInt64 v) { d.SetSqlInt64 (o, i, v); } }
216
217 sealed class dbR4 : MB<SqlSingle> { public override void To(IMapDataDestination d, object o, int i, SqlSingle v) { d.SetSqlSingle (o, i, v); } }
218 sealed class dbR8 : MB<SqlDouble> { public override void To(IMapDataDestination d, object o, int i, SqlDouble v) { d.SetSqlDouble (o, i, v); } }
219 sealed class dbD : MB<SqlDecimal> { public override void To(IMapDataDestination d, object o, int i, SqlDecimal v) { d.SetSqlDecimal (o, i, v); } }
220 sealed class dbM : MB<SqlMoney> { public override void To(IMapDataDestination d, object o, int i, SqlMoney v) { d.SetSqlMoney (o, i, v); } }
221
222 sealed class dbB : MB<SqlBoolean> { public override void To(IMapDataDestination d, object o, int i, SqlBoolean v) { d.SetSqlBoolean (o, i, v); } }
223 sealed class dbG : MB<SqlGuid> { public override void To(IMapDataDestination d, object o, int i, SqlGuid v) { d.SetSqlGuid (o, i, v); } }
224 sealed class dbDT : MB<SqlDateTime> { public override void To(IMapDataDestination d, object o, int i, SqlDateTime v) { d.SetSqlDateTime (o, i, v); } }
225
226 #endif
227 }
228 }