comparison Source/Mapping/MemberMapper.tt @ 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 <#@ template language="C#" debug="True" #>
2 <#@ output extension="generated.cs" #>
3 //---------------------------------------------------------------------------------------------------
4 // <auto-generated>
5 // This code was generated by BLToolkit template for T4.
6 // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
7 // </auto-generated>
8 //---------------------------------------------------------------------------------------------------
9 using System;
10 using System.Data.SqlTypes;
11 using System.IO;
12 using System.Xml;
13
14 using Convert = BLToolkit.Common.Convert;
15
16 namespace BLToolkit.Mapping
17 {
18 public partial class MemberMapper
19 {
20 <#
21 CreateMapper(SimpleMapper,
22 "SByte", "Int16", "Int32", "Int64",
23 "Byte", "UInt16", "UInt32", "UInt64",
24 "Single", "Double", "Char", "Boolean",
25 "DateTime", "DateTimeOffset", "Decimal", "Guid", "Stream");
26 CreateMapper(EnumNullableMapper, "SByte", "Int16", "Int32", "Int64", "Byte", "UInt16", "UInt32", "UInt64");
27 CreateMapper(NullableMapper, "Char", "Double", "Single", "Boolean", "DateTime", "Decimal", "Guid");
28 #>
29
30 #if !SILVERLIGHT
31 <#
32 CreateMapper(SimpleMapper, "XmlReader", "XmlDocument");
33 CreateMapper(SqlTypeMapper, "Byte", "Int16", "Int32", "Int64", "Single", "Boolean", "Double", "DateTime", "Decimal", "Money", "Guid", "String");
34 #>
35
36 #endif
37 }
38 }
39
40 <#+
41 void CreateMapper(Action<string> mapper, params string[] types)
42 {
43 foreach (string t in types)
44 mapper(t);
45 }
46
47 void SimpleMapper(string type)
48 {
49 string getset = type;
50 string cast = "(" + type + ")";
51
52 switch (type)
53 {
54 case "Stream" :
55 case "XmlReader" :
56 case "XmlDocument" :
57 getset = "Value";
58 cast = "";
59 break;
60 }
61
62 #>
63
64 class <#= type #>Mapper : MemberMapper
65 {
66 <#= type #> _nullValue;
67 <#+
68 switch (type)
69 {
70 case "Stream" :
71 case "XmlReader" :
72 case "XmlDocument" :
73 break;
74 default :
75 #>
76
77 public override bool IsNull (object o) { return false; }
78 public override void SetNull(object o) { MemberAccessor.Set<#= type #>(o, _nullValue); }
79 <#+
80 break;
81 }
82 #>
83
84 public override void SetValue(object o, object value)
85 {
86 MemberAccessor.Set<#= getset #>(
87 o,
88 value is <#= type #> ? <#= cast #>value :
89 value == null ? _nullValue : MappingSchema.ConvertTo<#= type #>(value));
90 }
91
92 public override void Init(MapMemberInfo mapMemberInfo)
93 {
94 if (mapMemberInfo == null) throw new ArgumentNullException("mapMemberInfo");
95 <#+
96 switch (type)
97 {
98 case "Guid" :
99 #>
100
101 if (mapMemberInfo.NullValue != null)
102 _nullValue = mapMemberInfo.NullValue is Guid ?
103 (Guid)mapMemberInfo.NullValue : new Guid(mapMemberInfo.NullValue.ToString());
104 <#+
105 break;
106
107 case "Stream" :
108 case "XmlReader" :
109 case "XmlDocument" :
110 #>
111
112 if (mapMemberInfo.NullValue != null)
113 _nullValue = mapMemberInfo.MappingSchema.ConvertTo<#= type #>(mapMemberInfo.NullValue);
114 <#+
115 break;
116
117 default:
118 #>
119
120 _nullValue = Convert.To<#= type #>(mapMemberInfo.NullValue);
121 <#+
122
123 break;
124 }
125 #>
126
127 base.Init(mapMemberInfo);
128 }
129
130 public class Nullable : <#= type #>Mapper
131 {
132 <#+
133 switch (type)
134 {
135 case "Stream" :
136 case "XmlReader" :
137 case "XmlDocument" :
138 break;
139 default:
140 #>
141 public override bool IsNull(object o) { return Get<#= type #>(o) == _nullValue; }
142
143 <#+
144
145 break;
146 }
147 #>
148 public override object GetValue(object o)
149 {
150 var value = MemberAccessor.Get<#= getset #>(o);
151 return value == _nullValue ? null : (object)value;
152 }
153 }
154 }
155 <#+
156 }
157
158 void NullableMapper(string type)
159 {
160 #>
161
162 class Nullable<#= type #>Mapper : MemberMapper
163 {
164 public override bool IsNull (object o) { return GetNullable<#= type #>(o) == null; }
165 public override void SetNull(object o) { MemberAccessor.SetNullable<#= type #>(o, null); }
166
167 public override void SetValue(object o, object value)
168 {
169 MemberAccessor.SetNullable<#= type #>(
170 o, value == null || value is <#= type #>? (<#= type #>?)value: MappingSchema.ConvertToNullable<#= type #>(value));
171 }
172 }
173 <#+
174 }
175
176 void EnumNullableMapper(string type)
177 {
178 #>
179
180 class Nullable<#= type #>Mapper : MemberMapper
181 {
182 public override bool IsNull (object o) { return GetNullable<#= type #>(o) == null; }
183 public override void SetNull(object o) { MemberAccessor.SetNullable<#= type #>(o, null); }
184
185 public override void SetValue(object o, object value)
186 {
187 MemberAccessor.SetNullable<#= type #>(
188 o, value == null || value is <#= type #>? (<#= type #>?)value: MappingSchema.ConvertToNullable<#= type #>(value));
189 }
190
191 public class Enum : NullableEnumMapper
192 {
193 public override void SetValue(object o, object value)
194 {
195 if (value != null)
196 {
197 var valueType = value.GetType();
198
199 if (valueType != MemberType)
200 {
201 if (valueType != UnderlyingType)
202 value = MappingSchema.ConvertToNullable<#= type #>(value);
203
204 value = System.Enum.ToObject(MemberType, (<#= type #>)value);
205 }
206 }
207
208 MemberAccessor.SetValue(o, value);
209 }
210 }
211 }
212 <#+
213 }
214
215 void SqlTypeMapper(string type)
216 {
217 #>
218
219 class Sql<#= type #>Mapper : MemberMapper
220 {
221 public override bool IsNull (object o) { return GetSql<#= type #>(o).IsNull; }
222 public override void SetNull(object o) { MemberAccessor.SetSql<#= type #>(o, Sql<#= type #>.Null); }
223
224 public override object GetValue(object o)
225 {
226 var value = MemberAccessor.GetSql<#= type #>(o);
227 return value.IsNull? null: (object)value.Value;
228 }
229
230 public override void SetValue(object o, object value)
231 {
232 MemberAccessor.SetSql<#= type #>(
233 o, value is Sql<#= type #>? (Sql<#= type #>)value: MappingSchema.ConvertToSql<#= type #>(value));
234 }
235
236 public class Default : Sql<#= type #>Mapper
237 {
238 public override bool SupportsValue { get { return false; } }
239
240 public override object GetValue(object o)
241 {
242 return MapTo(base.GetValue(o));
243 }
244
245 public override void SetValue(object o, object value)
246 {
247 base.SetValue(o, MapFrom(value));
248 }
249 }
250 }
251 <#+
252 }
253
254 #>