0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3
|
|
4 using BLToolkit.Common;
|
|
5 using BLToolkit.DataAccess;
|
|
6
|
|
7 namespace BLToolkit.Reflection.MetadataProvider
|
|
8 {
|
|
9 using Extension;
|
|
10 using Mapping;
|
|
11
|
|
12 public delegate void OnCreateProvider(MetadataProviderBase parentProvider);
|
|
13 public delegate MetadataProviderBase CreateProvider();
|
|
14 public delegate MemberMapper EnsureMapperHandler(string mapName, string origName);
|
|
15
|
|
16 public abstract class MetadataProviderBase
|
|
17 {
|
|
18 #region Provider Support
|
|
19
|
|
20 public virtual void AddProvider(MetadataProviderBase provider)
|
|
21 {
|
|
22 }
|
|
23
|
|
24 public virtual void InsertProvider(int index, MetadataProviderBase provider)
|
|
25 {
|
|
26 }
|
|
27
|
|
28 public virtual MetadataProviderBase[] GetProviders()
|
|
29 {
|
|
30 return new MetadataProviderBase[0];
|
|
31 }
|
|
32
|
|
33 #endregion
|
|
34
|
|
35 #region GetFieldName
|
|
36
|
|
37 public virtual string GetFieldName(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
38 {
|
|
39 isSet = false;
|
|
40 return member.Name;
|
|
41 }
|
|
42
|
|
43 #endregion
|
|
44
|
|
45 public virtual string GetSequenceName(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
46 {
|
|
47 isSet = false;
|
|
48 return null;
|
|
49 }
|
|
50
|
|
51 #region GetFieldStorage
|
|
52
|
|
53 public virtual string GetFieldStorage(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
54 {
|
|
55 isSet = false;
|
|
56 return null;
|
|
57 }
|
|
58
|
|
59 #endregion
|
|
60
|
|
61 #region GetInheritanceDiscriminator
|
|
62
|
|
63 public virtual bool GetInheritanceDiscriminator(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
64 {
|
|
65 isSet = false;
|
|
66 return false;
|
|
67 }
|
|
68
|
|
69 #endregion
|
|
70
|
|
71 #region EnsureMapper
|
|
72
|
|
73 public virtual void EnsureMapper(TypeAccessor typeAccessor, MappingSchema mappingSchema, EnsureMapperHandler handler)
|
|
74 {
|
|
75 }
|
|
76
|
|
77 #endregion
|
|
78
|
|
79 #region GetMapIgnore
|
|
80
|
|
81 public virtual bool GetMapIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
82 {
|
|
83 isSet = false;
|
|
84
|
|
85 return
|
|
86 TypeHelper.IsScalar(member.Type) == false;// ||
|
|
87 //(member.MemberInfo is FieldInfo && ((FieldInfo)member.MemberInfo).IsLiteral);
|
|
88 }
|
|
89
|
|
90 #endregion
|
|
91
|
|
92 #region GetMapField
|
|
93
|
|
94 public virtual MapFieldAttribute GetMapField(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
95 {
|
|
96 isSet = false;
|
|
97 return null;
|
|
98 }
|
|
99
|
|
100 #endregion
|
|
101
|
|
102 #region GetDbType
|
|
103
|
|
104 [CLSCompliant(false)]
|
|
105 public virtual DbTypeAttribute GetDbType(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
106 {
|
|
107 isSet = false;
|
|
108 return null;
|
|
109 }
|
|
110
|
|
111 #endregion
|
|
112
|
|
113 #region GetPrimaryKey
|
|
114
|
|
115 public virtual PrimaryKeyAttribute GetPrimaryKey(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
116 {
|
|
117 isSet = false;
|
|
118 return null;
|
|
119 }
|
|
120
|
|
121 #endregion
|
|
122
|
|
123 #region GetTrimmable
|
|
124
|
|
125 public virtual bool GetTrimmable(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
126 {
|
|
127 isSet = member.Type != typeof(string);
|
|
128 return isSet? false: TrimmableAttribute.Default.IsTrimmable;
|
|
129 }
|
|
130
|
|
131 #endregion
|
|
132
|
|
133 #region GetMapValues
|
|
134
|
|
135 public virtual MapValue[] GetMapValues(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
136 {
|
|
137 isSet = false;
|
|
138 return null;
|
|
139 }
|
|
140
|
|
141 public virtual MapValue[] GetMapValues(TypeExtension typeExtension, Type type, out bool isSet)
|
|
142 {
|
|
143 isSet = false;
|
|
144 return null;
|
|
145 }
|
|
146
|
|
147 #endregion
|
|
148
|
|
149 #region GetDefaultValue
|
|
150
|
|
151 public virtual object GetDefaultValue(MappingSchema mappingSchema, TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
152 {
|
|
153 isSet = false;
|
|
154 return null;
|
|
155 }
|
|
156
|
|
157 public virtual object GetDefaultValue(MappingSchema mappingSchema, TypeExtension typeExtension, Type type, out bool isSet)
|
|
158 {
|
|
159 isSet = false;
|
|
160 return null;
|
|
161 }
|
|
162
|
|
163 #endregion
|
|
164
|
|
165 #region GetNullable
|
|
166
|
|
167 public virtual bool GetNullable(MappingSchema mappingSchema, TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
168 {
|
|
169 isSet = false;
|
|
170 return
|
|
171 //member.Type.IsClass ||
|
|
172 member.Type.IsGenericType && member.Type.GetGenericTypeDefinition() == typeof (Nullable<>)
|
|
173 /*||
|
|
174 member.Type == typeof(System.Data.Linq.Binary) ||
|
|
175 member.Type == typeof(byte[])*/;
|
|
176 }
|
|
177
|
|
178 #endregion
|
|
179
|
|
180 #region GetLazyInstance
|
|
181
|
|
182 public virtual bool GetLazyInstance(MappingSchema mappingSchema, TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
183 {
|
|
184 isSet = false;
|
|
185 return false;
|
|
186 }
|
|
187
|
|
188 #endregion
|
|
189
|
|
190 #region GetNullValue
|
|
191
|
|
192 public virtual object GetNullValue(MappingSchema mappingSchema, TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
193 {
|
|
194 isSet = false;
|
|
195
|
|
196 if (member.Type.IsEnum)
|
|
197 return null;
|
|
198
|
|
199 var value = mappingSchema.GetNullValue(member.Type);
|
|
200
|
|
201 if (value is Type && (Type)value == typeof(DBNull))
|
|
202 {
|
|
203 value = DBNull.Value;
|
|
204
|
|
205 if (member.Type == typeof(string))
|
|
206 value = null;
|
|
207 }
|
|
208
|
|
209 return value;
|
|
210 }
|
|
211
|
|
212 #endregion
|
|
213
|
|
214 #region GetDbName
|
|
215
|
|
216 public virtual string GetDatabaseName(Type type, ExtensionList extensions, out bool isSet)
|
|
217 {
|
|
218 isSet = false;
|
|
219 return null;
|
|
220 }
|
|
221
|
|
222 #endregion
|
|
223
|
|
224 #region GetOwnerName
|
|
225
|
|
226 public virtual string GetOwnerName(Type type, ExtensionList extensions, out bool isSet)
|
|
227 {
|
|
228 isSet = false;
|
|
229 return null;
|
|
230 }
|
|
231
|
|
232 #endregion
|
|
233
|
|
234 #region GetTableName
|
|
235
|
|
236 public virtual string GetTableName(Type type, ExtensionList extensions, out bool isSet)
|
|
237 {
|
|
238 isSet = false;
|
|
239 return
|
|
240 type.IsInterface && type.Name.StartsWith("I")
|
|
241 ? type.Name.Substring(1)
|
|
242 : type.Name;
|
|
243 }
|
|
244
|
|
245 #endregion
|
|
246
|
|
247 #region GetPrimaryKeyOrder
|
|
248
|
|
249 public virtual int GetPrimaryKeyOrder(Type type, TypeExtension typeExt, MemberAccessor member, out bool isSet)
|
|
250 {
|
|
251 isSet = false;
|
|
252 return 0;
|
|
253 }
|
|
254
|
|
255 #endregion
|
|
256
|
|
257 #region GetNonUpdatableAttribute
|
|
258
|
|
259 public virtual NonUpdatableAttribute GetNonUpdatableAttribute(Type type, TypeExtension typeExt, MemberAccessor member, out bool isSet)
|
|
260 {
|
|
261 isSet = false;
|
|
262 return null;
|
|
263 }
|
|
264
|
|
265 #endregion
|
|
266
|
|
267 #region GetSqlIgnore
|
|
268
|
|
269 public virtual bool GetSqlIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
270 {
|
|
271 isSet = false;
|
|
272 return false;
|
|
273 }
|
|
274
|
|
275 #endregion
|
|
276
|
|
277 #region GetRelations
|
|
278
|
|
279 public virtual List<MapRelationBase> GetRelations(MappingSchema schema, ExtensionList typeExt, Type master, Type slave, out bool isSet)
|
|
280 {
|
|
281 isSet = false;
|
|
282 return new List<MapRelationBase>();
|
|
283 }
|
|
284
|
|
285 protected static List<string> GetPrimaryKeyFields(MappingSchema schema, TypeAccessor ta, TypeExtension tex)
|
|
286 {
|
|
287 var mdp = schema.MetadataProvider;
|
|
288 var keys = new List<string>();
|
|
289
|
|
290 foreach (MemberAccessor sma in ta)
|
|
291 {
|
|
292 bool isSetFlag;
|
|
293
|
|
294 mdp.GetPrimaryKeyOrder(ta.Type, tex, sma, out isSetFlag);
|
|
295
|
|
296 if (isSetFlag)
|
|
297 {
|
|
298 var name = mdp.GetFieldName(tex, sma, out isSetFlag);
|
|
299 keys.Add(name);
|
|
300 }
|
|
301 }
|
|
302
|
|
303 return keys;
|
|
304 }
|
|
305
|
|
306 #endregion
|
|
307
|
|
308 #region GetAssociation
|
|
309
|
|
310 public virtual Association GetAssociation(TypeExtension typeExtension, MemberAccessor member)
|
|
311 {
|
|
312 return null;
|
|
313 }
|
|
314
|
|
315 #endregion
|
|
316
|
|
317 #region GetInheritanceMapping
|
|
318
|
|
319 public virtual InheritanceMappingAttribute[] GetInheritanceMapping(Type type, TypeExtension typeExtension)
|
|
320 {
|
|
321 return Array<InheritanceMappingAttribute>.Empty;
|
|
322 }
|
|
323
|
|
324 #endregion
|
|
325
|
|
326 #region Static Members
|
|
327
|
|
328 public static event OnCreateProvider OnCreateProvider;
|
|
329
|
|
330 private static CreateProvider _createProvider = CreateInternal;
|
|
331 public static CreateProvider CreateProvider
|
|
332 {
|
|
333 get { return _createProvider; }
|
|
334 set { _createProvider = value ?? new CreateProvider(CreateInternal); }
|
|
335 }
|
|
336
|
|
337 private static MetadataProviderBase CreateInternal()
|
|
338 {
|
|
339 var list = new MetadataProviderList();
|
|
340
|
|
341 if (OnCreateProvider != null)
|
|
342 OnCreateProvider(list);
|
|
343
|
|
344 return list;
|
|
345 }
|
|
346
|
|
347 #endregion
|
|
348 }
|
|
349 }
|