-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathEdmTypeBuilder.cs
More file actions
539 lines (468 loc) · 23.4 KB
/
EdmTypeBuilder.cs
File metadata and controls
539 lines (468 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Microsoft.AspNet.OData.Common;
using Microsoft.AspNet.OData.Formatter;
using Microsoft.AspNet.OData.Query;
using Microsoft.OData.Edm;
namespace Microsoft.AspNet.OData.Builder
{
/// <summary>
/// <see cref="EdmTypeBuilder"/> builds <see cref="IEdmType"/>'s from <see cref="StructuralTypeConfiguration"/>'s.
/// </summary>
internal class EdmTypeBuilder
{
private readonly List<IEdmTypeConfiguration> _configurations;
private readonly Dictionary<Type, IEdmType> _types = new Dictionary<Type, IEdmType>();
private readonly Dictionary<PropertyDescriptor, IEdmProperty> _properties = new Dictionary<PropertyDescriptor, IEdmProperty>();
private readonly Dictionary<IEdmProperty, QueryableRestrictions> _propertiesRestrictions = new Dictionary<IEdmProperty, QueryableRestrictions>();
private readonly Dictionary<IEdmProperty, ModelBoundQuerySettings> _propertiesQuerySettings = new Dictionary<IEdmProperty, ModelBoundQuerySettings>();
private readonly Dictionary<IEdmStructuredType, ModelBoundQuerySettings> _structuredTypeQuerySettings = new Dictionary<IEdmStructuredType, ModelBoundQuerySettings>();
private readonly Dictionary<Enum, IEdmEnumMember> _members = new Dictionary<Enum, IEdmEnumMember>();
private readonly Dictionary<IEdmStructuredType, PropertyInfo> _openTypes = new Dictionary<IEdmStructuredType, PropertyInfo>();
internal EdmTypeBuilder(IEnumerable<IEdmTypeConfiguration> configurations)
{
_configurations = configurations.ToList();
}
private Dictionary<Type, IEdmType> GetEdmTypes()
{
// Reset
_types.Clear();
_properties.Clear();
_members.Clear();
_openTypes.Clear();
// Create headers to allow CreateEdmTypeBody to blindly references other things.
foreach (IEdmTypeConfiguration config in _configurations)
{
CreateEdmTypeHeader(config);
}
foreach (IEdmTypeConfiguration config in _configurations)
{
CreateEdmTypeBody(config);
}
foreach (StructuralTypeConfiguration structrual in _configurations.OfType<StructuralTypeConfiguration>())
{
CreateNavigationProperty(structrual);
}
return _types;
}
private void CreateEdmTypeHeader(IEdmTypeConfiguration config)
{
IEdmType edmType = GetEdmType(config.ClrType);
if (edmType == null)
{
if (config.Kind == EdmTypeKind.Complex)
{
ComplexTypeConfiguration complex = (ComplexTypeConfiguration)config;
IEdmComplexType baseType = null;
if (complex.BaseType != null)
{
CreateEdmTypeHeader(complex.BaseType);
baseType = GetEdmType(complex.BaseType.ClrType) as IEdmComplexType;
Contract.Assert(baseType != null);
}
EdmComplexType complexType = new EdmComplexType(config.Namespace, config.Name,
baseType, complex.IsAbstract ?? false, complex.IsOpen);
_types.Add(config.ClrType, complexType);
if (complex.IsOpen)
{
// add a mapping between the open complex type and its dynamic property dictionary.
_openTypes.Add(complexType, complex.DynamicPropertyDictionary);
}
edmType = complexType;
}
else if (config.Kind == EdmTypeKind.Entity)
{
EntityTypeConfiguration entity = config as EntityTypeConfiguration;
Contract.Assert(entity != null);
IEdmEntityType baseType = null;
if (entity.BaseType != null)
{
CreateEdmTypeHeader(entity.BaseType);
baseType = GetEdmType(entity.BaseType.ClrType) as IEdmEntityType;
Contract.Assert(baseType != null);
}
EdmEntityType entityType = new EdmEntityType(config.Namespace, config.Name, baseType,
entity.IsAbstract ?? false, entity.IsOpen, entity.HasStream);
_types.Add(config.ClrType, entityType);
if (entity.IsOpen)
{
// add a mapping between the open entity type and its dynamic property dictionary.
_openTypes.Add(entityType, entity.DynamicPropertyDictionary);
}
edmType = entityType;
}
else
{
EnumTypeConfiguration enumTypeConfiguration = config as EnumTypeConfiguration;
// The config has to be enum.
Contract.Assert(enumTypeConfiguration != null);
_types.Add(enumTypeConfiguration.ClrType,
new EdmEnumType(enumTypeConfiguration.Namespace, enumTypeConfiguration.Name,
GetTypeKind(enumTypeConfiguration.UnderlyingType), enumTypeConfiguration.IsFlags));
}
}
IEdmStructuredType structuredType = edmType as IEdmStructuredType;
StructuralTypeConfiguration structuralTypeConfiguration = config as StructuralTypeConfiguration;
if (structuredType != null && structuralTypeConfiguration != null &&
!_structuredTypeQuerySettings.ContainsKey(structuredType))
{
ModelBoundQuerySettings querySettings =
structuralTypeConfiguration.QueryConfiguration.ModelBoundQuerySettings;
if (querySettings != null)
{
_structuredTypeQuerySettings.Add(structuredType,
structuralTypeConfiguration.QueryConfiguration.ModelBoundQuerySettings);
}
}
}
private void CreateEdmTypeBody(IEdmTypeConfiguration config)
{
IEdmType edmType = GetEdmType(config.ClrType);
if (edmType.TypeKind == EdmTypeKind.Complex)
{
CreateComplexTypeBody((EdmComplexType)edmType, (ComplexTypeConfiguration)config);
}
else if (edmType.TypeKind == EdmTypeKind.Entity)
{
CreateEntityTypeBody((EdmEntityType)edmType, (EntityTypeConfiguration)config);
}
else
{
Contract.Assert(edmType.TypeKind == EdmTypeKind.Enum);
CreateEnumTypeBody((EdmEnumType)edmType, (EnumTypeConfiguration)config);
}
}
private static IEdmTypeReference AddPrecisionConfigInPrimitiveTypeReference(
PrecisionPropertyConfiguration precisionProperty,
IEdmTypeReference primitiveTypeReference)
{
if (primitiveTypeReference is EdmTemporalTypeReference && precisionProperty.Precision.HasValue)
{
return new EdmTemporalTypeReference(
(IEdmPrimitiveType)primitiveTypeReference.Definition,
primitiveTypeReference.IsNullable,
precisionProperty.Precision);
}
return primitiveTypeReference;
}
private static IEdmTypeReference AddLengthConfigInPrimitiveTypeReference(
LengthPropertyConfiguration lengthProperty,
IEdmTypeReference primitiveTypeReference)
{
if (lengthProperty.MaxLength.HasValue)
{
if (primitiveTypeReference is EdmStringTypeReference)
{
return new EdmStringTypeReference(
(IEdmPrimitiveType)primitiveTypeReference.Definition,
primitiveTypeReference.IsNullable,
false,
lengthProperty.MaxLength,
true);
}
if (primitiveTypeReference is EdmBinaryTypeReference)
{
return new EdmBinaryTypeReference(
(IEdmPrimitiveType)primitiveTypeReference.Definition,
primitiveTypeReference.IsNullable,
false,
lengthProperty.MaxLength);
}
}
return primitiveTypeReference;
}
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Class coupling acceptable")]
private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConfiguration config)
{
foreach (PropertyConfiguration property in config.Properties)
{
IEdmProperty edmProperty = null;
switch (property.Kind)
{
case PropertyKind.Primitive:
PrimitivePropertyConfiguration primitiveProperty = (PrimitivePropertyConfiguration)property;
EdmPrimitiveTypeKind typeKind = primitiveProperty.TargetEdmTypeKind ??
GetTypeKind(primitiveProperty.PropertyInfo.PropertyType);
IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
typeKind,
primitiveProperty.OptionalProperty);
if (typeKind == EdmPrimitiveTypeKind.Decimal)
{
DecimalPropertyConfiguration decimalProperty =
primitiveProperty as DecimalPropertyConfiguration;
if (decimalProperty.Precision.HasValue || decimalProperty.Scale.HasValue)
{
primitiveTypeReference = new EdmDecimalTypeReference(
(IEdmPrimitiveType)primitiveTypeReference.Definition,
primitiveTypeReference.IsNullable,
decimalProperty.Precision,
decimalProperty.Scale.HasValue ? decimalProperty.Scale : 0);
}
}
else if (EdmLibHelpers.HasPrecision(typeKind))
{
PrecisionPropertyConfiguration precisionProperty =
primitiveProperty as PrecisionPropertyConfiguration;
primitiveTypeReference = AddPrecisionConfigInPrimitiveTypeReference(
precisionProperty,
primitiveTypeReference);
}
else if (EdmLibHelpers.HasLength(typeKind))
{
LengthPropertyConfiguration lengthProperty =
primitiveProperty as LengthPropertyConfiguration;
primitiveTypeReference = AddLengthConfigInPrimitiveTypeReference(
lengthProperty,
primitiveTypeReference);
}
edmProperty = type.AddStructuralProperty(
primitiveProperty.Name,
primitiveTypeReference,
defaultValue: null);
break;
case PropertyKind.Complex:
ComplexPropertyConfiguration complexProperty = property as ComplexPropertyConfiguration;
IEdmComplexType complexType = GetEdmType(complexProperty.RelatedClrType) as IEdmComplexType;
edmProperty = type.AddStructuralProperty(
complexProperty.Name,
new EdmComplexTypeReference(complexType, complexProperty.OptionalProperty));
break;
case PropertyKind.Collection:
edmProperty = CreateStructuralTypeCollectionPropertyBody(type, (CollectionPropertyConfiguration)property);
break;
case PropertyKind.Enum:
edmProperty = CreateStructuralTypeEnumPropertyBody(type, (EnumPropertyConfiguration)property);
break;
default:
break;
}
if (edmProperty != null)
{
if (property.PropertyInfo != null)
{
_properties[property.PropertyInfo] = edmProperty;
}
if (property.IsRestricted)
{
_propertiesRestrictions[edmProperty] = new QueryableRestrictions(property);
}
if (property.QueryConfiguration.ModelBoundQuerySettings != null)
{
_propertiesQuerySettings.Add(edmProperty, property.QueryConfiguration.ModelBoundQuerySettings);
}
}
}
}
private IEdmProperty CreateStructuralTypeCollectionPropertyBody(EdmStructuredType type, CollectionPropertyConfiguration collectionProperty)
{
IEdmTypeReference elementTypeReference = null;
Type clrType = TypeHelper.GetUnderlyingTypeOrSelf(collectionProperty.ElementType);
if (TypeHelper.IsEnum(clrType))
{
IEdmType edmType = GetEdmType(clrType);
if (edmType == null)
{
throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, clrType.Name);
}
IEdmEnumType enumElementType = (IEdmEnumType)edmType;
bool isNullable = collectionProperty.ElementType != clrType;
elementTypeReference = new EdmEnumTypeReference(enumElementType, isNullable);
}
else
{
IEdmType edmType = GetEdmType(collectionProperty.ElementType);
if (edmType != null)
{
IEdmComplexType elementType = edmType as IEdmComplexType;
Contract.Assert(elementType != null);
elementTypeReference = new EdmComplexTypeReference(elementType, collectionProperty.OptionalProperty);
}
else
{
elementTypeReference =
EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType);
Contract.Assert(elementTypeReference != null);
}
}
return type.AddStructuralProperty(
collectionProperty.Name,
new EdmCollectionTypeReference(new EdmCollectionType(elementTypeReference)));
}
private IEdmProperty CreateStructuralTypeEnumPropertyBody(EdmStructuredType type, EnumPropertyConfiguration enumProperty)
{
Type enumPropertyType = TypeHelper.GetUnderlyingTypeOrSelf(enumProperty.RelatedClrType);
IEdmType edmType = GetEdmType(enumPropertyType);
if (edmType == null)
{
throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, enumPropertyType.Name);
}
IEdmEnumType enumType = (IEdmEnumType)edmType;
IEdmTypeReference enumTypeReference = new EdmEnumTypeReference(enumType, enumProperty.OptionalProperty);
return type.AddStructuralProperty(
enumProperty.Name,
enumTypeReference,
defaultValue: null);
}
private void CreateComplexTypeBody(EdmComplexType type, ComplexTypeConfiguration config)
{
Contract.Assert(type != null);
Contract.Assert(config != null);
CreateStructuralTypeBody(type, config);
}
private void CreateEntityTypeBody(EdmEntityType type, EntityTypeConfiguration config)
{
Contract.Assert(type != null);
Contract.Assert(config != null);
CreateStructuralTypeBody(type, config);
IEnumerable<IEdmStructuralProperty> keys = config.Keys.Select(p => type.DeclaredProperties.OfType<IEdmStructuralProperty>().First(dp => dp.Name == p.Name));
type.AddKeys(keys);
// Add the Enum keys
keys = config.EnumKeys.Select(p => type.DeclaredProperties.OfType<IEdmStructuralProperty>().First(dp => dp.Name == p.Name));
type.AddKeys(keys);
}
private void CreateNavigationProperty(StructuralTypeConfiguration config)
{
Contract.Assert(config != null);
EdmStructuredType type = (EdmStructuredType)(GetEdmType(config.ClrType));
foreach (NavigationPropertyConfiguration navProp in config.NavigationProperties)
{
EdmNavigationPropertyInfo info = new EdmNavigationPropertyInfo
{
Name = navProp.Name,
TargetMultiplicity = navProp.Multiplicity,
Target = GetEdmType(navProp.RelatedClrType) as IEdmEntityType,
ContainsTarget = navProp.ContainsTarget,
OnDelete = navProp.OnDeleteAction
};
// Principal properties
if (navProp.PrincipalProperties.Any())
{
info.PrincipalProperties = GetDeclaringPropertyInfo(navProp.PrincipalProperties);
}
// Dependent properties
if (navProp.DependentProperties.Any())
{
info.DependentProperties = GetDeclaringPropertyInfo(navProp.DependentProperties);
}
IEdmProperty edmProperty = type.AddUnidirectionalNavigation(info);
if (navProp.PropertyInfo != null && edmProperty != null)
{
_properties[navProp.PropertyInfo] = edmProperty;
}
if (edmProperty != null)
{
if (navProp.IsRestricted)
{
_propertiesRestrictions[edmProperty] = new QueryableRestrictions(navProp);
}
if (navProp.QueryConfiguration.ModelBoundQuerySettings != null)
{
_propertiesQuerySettings.Add(edmProperty, navProp.QueryConfiguration.ModelBoundQuerySettings);
}
}
}
}
private IList<IEdmStructuralProperty> GetDeclaringPropertyInfo(IEnumerable<PropertyInfo> propertyInfos)
{
IList<IEdmProperty> edmProperties = new List<IEdmProperty>();
foreach (PropertyInfo propInfo in propertyInfos)
{
IEdmProperty edmProperty;
PropertyDescriptor propDescr = new PropertyDescriptor(propInfo);
if (_properties.TryGetValue(propDescr, out edmProperty))
{
edmProperties.Add(edmProperty);
}
else
{
Contract.Assert(TypeHelper.GetReflectedType(propInfo) != null);
Type baseType = TypeHelper.GetBaseType(TypeHelper.GetReflectedType(propInfo));
while (baseType != null)
{
PropertyInfo basePropInfo = baseType.GetProperty(propInfo.Name);
PropertyDescriptor basePropDescr = new PropertyDescriptor(basePropInfo);
if (_properties.TryGetValue(basePropDescr, out edmProperty))
{
edmProperties.Add(edmProperty);
break;
}
baseType = TypeHelper.GetBaseType(baseType);
}
Contract.Assert(baseType != null);
}
}
return edmProperties.OfType<IEdmStructuralProperty>().ToList();
}
private void CreateEnumTypeBody(EdmEnumType type, EnumTypeConfiguration config)
{
Contract.Assert(type != null);
Contract.Assert(config != null);
foreach (EnumMemberConfiguration member in config.Members)
{
// EdmIntegerConstant can only support a value of long type.
long value;
try
{
value = Convert.ToInt64(member.MemberInfo, CultureInfo.InvariantCulture);
}
catch
{
throw Error.Argument("value", SRResources.EnumValueCannotBeLong, Enum.GetName(member.MemberInfo.GetType(), member.MemberInfo));
}
EdmEnumMember edmMember = new EdmEnumMember(type, member.Name,
new EdmEnumMemberValue(value));
type.AddMember(edmMember);
_members[member.MemberInfo] = edmMember;
}
}
private IEdmType GetEdmType(Type clrType)
{
Contract.Assert(clrType != null);
IEdmType edmType;
_types.TryGetValue(clrType, out edmType);
return edmType;
}
/// <summary>
/// Builds <see cref="IEdmType"/> and <see cref="IEdmProperty"/>'s from <paramref name="configurations"/>
/// </summary>
/// <param name="configurations">A collection of <see cref="IEdmTypeConfiguration"/>'s</param>
/// <returns>The built dictionary of <see cref="StructuralTypeConfiguration"/>'s indexed by their backing CLR type,
/// and dictionary of <see cref="StructuralTypeConfiguration"/>'s indexed by their backing CLR property info</returns>
public static EdmTypeMap GetTypesAndProperties(IEnumerable<IEdmTypeConfiguration> configurations)
{
if (configurations == null)
{
throw Error.ArgumentNull("configurations");
}
EdmTypeBuilder builder = new EdmTypeBuilder(configurations);
return new EdmTypeMap(builder.GetEdmTypes(),
builder._properties,
builder._propertiesRestrictions,
builder._propertiesQuerySettings,
builder._structuredTypeQuerySettings,
builder._members,
builder._openTypes);
}
/// <summary>
/// Gets the <see cref="EdmPrimitiveTypeKind"/> that maps to the <see cref="Type"/>
/// </summary>
/// <param name="clrType">The clr type</param>
/// <returns>The corresponding Edm primitive kind.</returns>
public static EdmPrimitiveTypeKind GetTypeKind(Type clrType)
{
IEdmPrimitiveType primitiveType = EdmLibHelpers.GetEdmPrimitiveTypeOrNull(clrType);
if (primitiveType == null)
{
throw Error.Argument("clrType", SRResources.MustBePrimitiveType, clrType.FullName);
}
return primitiveType.PrimitiveKind;
}
}
}