-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathEdmModelHelperMethods.cs
More file actions
940 lines (822 loc) · 44 KB
/
EdmModelHelperMethods.cs
File metadata and controls
940 lines (822 loc) · 44 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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
// 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.Linq;
using System.Reflection;
using Microsoft.AspNet.OData.Common;
using Microsoft.AspNet.OData.Query;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OData.Edm.Validation;
using Microsoft.OData.Edm.Vocabularies;
namespace Microsoft.AspNet.OData.Builder
{
internal static class EdmModelHelperMethods
{
public static IEdmModel BuildEdmModel(ODataModelBuilder builder)
{
if (builder == null)
{
throw Error.ArgumentNull("builder");
}
EdmModel model = new EdmModel();
EdmEntityContainer container = new EdmEntityContainer(builder.Namespace, builder.ContainerName);
// add types and sets, building an index on the way.
IEnumerable<IEdmTypeConfiguration> configTypes = builder.StructuralTypes.Concat<IEdmTypeConfiguration>(builder.EnumTypes);
EdmTypeMap edmMap = EdmTypeBuilder.GetTypesAndProperties(configTypes);
Dictionary<Type, IEdmType> edmTypeMap = model.AddTypes(edmMap);
// Add EntitySets and build the mapping between the EdmEntitySet and the NavigationSourceConfiguration
NavigationSourceAndAnnotations[] entitySets = container.AddEntitySetAndAnnotations(builder, edmTypeMap);
// Add Singletons and build the mapping between the EdmSingleton and the NavigationSourceConfiguration
NavigationSourceAndAnnotations[] singletons = container.AddSingletonAndAnnotations(builder, edmTypeMap);
// Merge EntitySets and Singletons together
IEnumerable<NavigationSourceAndAnnotations> navigationSources = entitySets.Concat(singletons);
// Build the navigation source map
IDictionary<string, EdmNavigationSource> navigationSourceMap = model.GetNavigationSourceMap(edmMap, navigationSources);
// Add the core vocabulary annotations
model.AddCoreVocabularyAnnotations(navigationSources, edmMap);
// TODO: support etags on contained nav props
// Support for this in 5.x adds annotations to navigation properties. Ideally would add annotations to entity set/singleton for
// containing type(s) with nav paths to the contained nav property.
// model.AddNavPropAnnotations(builder, edmMap); // implemented in EdmModelHelperMethods.cs of 5.x branch
// Add the capabilities vocabulary annotations
model.AddCapabilitiesVocabularyAnnotations(navigationSources, edmMap);
// add operations
model.AddOperations(builder.Operations, container, edmTypeMap, navigationSourceMap);
// finish up
model.AddElement(container);
// build the map from IEdmEntityType to IEdmFunctionImport
model.SetAnnotationValue<BindableOperationFinder>(model, new BindableOperationFinder(model));
return model;
}
private static void AddTypes(this EdmModel model, Dictionary<Type, IEdmType> types)
{
Contract.Assert(model != null);
Contract.Assert(types != null);
foreach (IEdmType type in types.Values)
{
model.AddType(type);
}
}
private static NavigationSourceAndAnnotations[] AddEntitySetAndAnnotations(this EdmEntityContainer container,
ODataModelBuilder builder, Dictionary<Type, IEdmType> edmTypeMap)
{
IEnumerable<EntitySetConfiguration> configurations = builder.EntitySets;
// build the entitysets
IEnumerable<Tuple<EdmEntitySet, EntitySetConfiguration>> entitySets = AddEntitySets(configurations, container, edmTypeMap);
// return the annotation array
return entitySets.Select(e => new NavigationSourceAndAnnotations()
{
NavigationSource = e.Item1,
Configuration = e.Item2,
LinkBuilder = new NavigationSourceLinkBuilderAnnotation(e.Item2),
Url = new NavigationSourceUrlAnnotation { Url = e.Item2.GetUrl() }
}).ToArray();
}
private static NavigationSourceAndAnnotations[] AddSingletonAndAnnotations(this EdmEntityContainer container,
ODataModelBuilder builder, Dictionary<Type, IEdmType> edmTypeMap)
{
IEnumerable<SingletonConfiguration> configurations = builder.Singletons;
// build the singletons
IEnumerable<Tuple<EdmSingleton, SingletonConfiguration>> singletons = AddSingletons(configurations, container, edmTypeMap);
// return the annotation array
return singletons.Select(e => new NavigationSourceAndAnnotations()
{
NavigationSource = e.Item1,
Configuration = e.Item2,
LinkBuilder = new NavigationSourceLinkBuilderAnnotation(e.Item2),
Url = new NavigationSourceUrlAnnotation { Url = e.Item2.GetUrl() }
}).ToArray();
}
private static IDictionary<string, EdmNavigationSource> GetNavigationSourceMap(this EdmModel model, EdmTypeMap edmMap,
IEnumerable<NavigationSourceAndAnnotations> navigationSourceAndAnnotations)
{
// index the navigation source by name
Dictionary<string, EdmNavigationSource> edmNavigationSourceMap = navigationSourceAndAnnotations.ToDictionary(e => e.NavigationSource.Name, e => e.NavigationSource);
// apply the annotations
foreach (NavigationSourceAndAnnotations navigationSourceAndAnnotation in navigationSourceAndAnnotations)
{
EdmNavigationSource navigationSource = navigationSourceAndAnnotation.NavigationSource;
model.SetAnnotationValue(navigationSource, navigationSourceAndAnnotation.Url);
model.SetNavigationSourceLinkBuilder(navigationSource, navigationSourceAndAnnotation.LinkBuilder);
AddNavigationBindings(edmMap, navigationSourceAndAnnotation.Configuration, navigationSource, navigationSourceAndAnnotation.LinkBuilder,
edmNavigationSourceMap);
}
return edmNavigationSourceMap;
}
private static void AddNavigationBindings(EdmTypeMap edmMap,
NavigationSourceConfiguration navigationSourceConfiguration,
EdmNavigationSource navigationSource,
NavigationSourceLinkBuilderAnnotation linkBuilder,
Dictionary<string, EdmNavigationSource> edmNavigationSourceMap)
{
foreach (var binding in navigationSourceConfiguration.Bindings)
{
NavigationPropertyConfiguration navigationProperty = binding.NavigationProperty;
bool isContained = navigationProperty.ContainsTarget;
IEdmType edmType = edmMap.EdmTypes[navigationProperty.DeclaringType.ClrType];
IEdmStructuredType structuraType = edmType as IEdmStructuredType;
IEdmNavigationProperty edmNavigationProperty = structuraType.NavigationProperties()
.Single(np => np.Name == navigationProperty.Name);
string bindingPath = ConvertBindingPath(edmMap, binding);
if (!isContained)
{
// calculate the binding path
navigationSource.AddNavigationTarget(
edmNavigationProperty,
edmNavigationSourceMap[binding.TargetNavigationSource.Name],
new EdmPathExpression(bindingPath));
}
NavigationLinkBuilder linkBuilderFunc = navigationSourceConfiguration.GetNavigationPropertyLink(navigationProperty);
if (linkBuilderFunc != null)
{
linkBuilder.AddNavigationPropertyLinkBuilder(edmNavigationProperty, linkBuilderFunc);
}
}
}
private static string ConvertBindingPath(EdmTypeMap edmMap, NavigationPropertyBindingConfiguration binding)
{
IList<string> bindings = new List<string>();
foreach (MemberInfo bindingInfo in binding.Path)
{
Type typeCast = TypeHelper.AsType(bindingInfo);
PropertyInfo propertyInfo = bindingInfo as PropertyInfo;
MethodInfo methodInfo = bindingInfo as MethodInfo;
if (typeCast != null)
{
IEdmType edmType = edmMap.EdmTypes[typeCast];
bindings.Add(edmType.FullTypeName());
}
else if (propertyInfo != null)
{
PropertyDescriptor propDescr = new PropertyDescriptor(propertyInfo);
bindings.Add(edmMap.EdmProperties[propDescr].Name);
}
else if (methodInfo != null)
{
PropertyDescriptor propDescr = new PropertyDescriptor(methodInfo);
bindings.Add(edmMap.EdmProperties[propDescr].Name);
}
}
return String.Join("/", bindings);
}
private static void AddOperationParameters(EdmOperation operation, OperationConfiguration operationConfiguration, Dictionary<Type, IEdmType> edmTypeMap)
{
foreach (ParameterConfiguration parameter in operationConfiguration.Parameters)
{
bool isParameterNullable = parameter.Nullable;
IEdmTypeReference parameterTypeReference = GetEdmTypeReference(edmTypeMap, parameter.TypeConfiguration, nullable: isParameterNullable);
IEdmOperationParameter operationParameter = new EdmOperationParameter(operation, parameter.Name, parameterTypeReference);
operation.AddParameter(operationParameter);
}
}
private static void AddOperationLinkBuilder(IEdmModel model, IEdmOperation operation, OperationConfiguration operationConfiguration)
{
ActionConfiguration actionConfiguration = operationConfiguration as ActionConfiguration;
IEdmAction action = operation as IEdmAction;
FunctionConfiguration functionConfiguration = operationConfiguration as FunctionConfiguration;
IEdmFunction function = operation as IEdmFunction;
if (operationConfiguration.BindingParameter.TypeConfiguration.Kind == EdmTypeKind.Entity)
{
if (actionConfiguration != null && actionConfiguration.GetActionLink() != null && action != null)
{
model.SetOperationLinkBuilder(
action,
new OperationLinkBuilder(actionConfiguration.GetActionLink(), actionConfiguration.FollowsConventions));
}
else if (functionConfiguration != null && functionConfiguration.GetFunctionLink() != null && function != null)
{
model.SetOperationLinkBuilder(
function,
new OperationLinkBuilder(functionConfiguration.GetFunctionLink(), functionConfiguration.FollowsConventions));
}
}
else if (operationConfiguration.BindingParameter.TypeConfiguration.Kind == EdmTypeKind.Collection)
{
CollectionTypeConfiguration collectionTypeConfiguration =
(CollectionTypeConfiguration)operationConfiguration.BindingParameter.TypeConfiguration;
if (collectionTypeConfiguration.ElementType.Kind == EdmTypeKind.Entity)
{
if (actionConfiguration != null && actionConfiguration.GetFeedActionLink() != null && action != null)
{
model.SetOperationLinkBuilder(
action,
new OperationLinkBuilder(actionConfiguration.GetFeedActionLink(), actionConfiguration.FollowsConventions));
}
else if (functionConfiguration != null && functionConfiguration.GetFeedFunctionLink() != null && function != null)
{
model.SetOperationLinkBuilder(
function,
new OperationLinkBuilder(functionConfiguration.GetFeedFunctionLink(), functionConfiguration.FollowsConventions));
}
}
}
}
private static void ValidateOperationEntitySetPath(IEdmModel model, IEdmOperationImport operationImport, OperationConfiguration operationConfiguration)
{
IEdmOperationParameter operationParameter;
Dictionary<IEdmNavigationProperty, IEdmPathExpression> relativeNavigations;
IEnumerable<EdmError> edmErrors;
if (operationConfiguration.EntitySetPath != null && !operationImport.TryGetRelativeEntitySetPath(model, out operationParameter, out relativeNavigations, out edmErrors))
{
throw Error.InvalidOperation(SRResources.OperationHasInvalidEntitySetPath, String.Join("/", operationConfiguration.EntitySetPath), operationConfiguration.FullyQualifiedName);
}
}
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling",
Justification = "The majority of types referenced by this method are EdmLib types this method needs to know about to operate correctly")]
private static void AddOperations(this EdmModel model, IEnumerable<OperationConfiguration> configurations, EdmEntityContainer container,
Dictionary<Type, IEdmType> edmTypeMap, IDictionary<string, EdmNavigationSource> edmNavigationSourceMap)
{
Contract.Assert(model != null, "Model can't be null");
ValidateActionOverload(configurations.OfType<ActionConfiguration>());
foreach (OperationConfiguration operationConfiguration in configurations)
{
IEdmTypeReference returnReference = GetEdmTypeReference(edmTypeMap,
operationConfiguration.ReturnType,
operationConfiguration.ReturnType != null && operationConfiguration.ReturnNullable);
IEdmExpression expression = GetEdmEntitySetExpression(edmNavigationSourceMap, operationConfiguration);
IEdmPathExpression pathExpression = operationConfiguration.EntitySetPath != null
? new EdmPathExpression(operationConfiguration.EntitySetPath)
: null;
EdmOperationImport operationImport;
switch (operationConfiguration.Kind)
{
case OperationKind.Action:
operationImport = CreateActionImport(operationConfiguration, container, returnReference, expression, pathExpression);
break;
case OperationKind.Function:
operationImport = CreateFunctionImport((FunctionConfiguration)operationConfiguration, container, returnReference, expression, pathExpression);
break;
case OperationKind.ServiceOperation:
Contract.Assert(false, "ServiceOperations are not supported.");
goto default;
default:
Contract.Assert(false, "Unsupported OperationKind");
return;
}
EdmOperation operation = (EdmOperation)operationImport.Operation;
if (operationConfiguration.IsBindable && operationConfiguration.Title != null && operationConfiguration.Title != operationConfiguration.Name)
{
model.SetOperationTitleAnnotation(operation, new OperationTitleAnnotation(operationConfiguration.Title));
}
if (operationConfiguration.IsBindable &&
operationConfiguration.NavigationSource != null &&
edmNavigationSourceMap.ContainsKey(operationConfiguration.NavigationSource.Name))
{
model.SetAnnotationValue(operation, new ReturnedEntitySetAnnotation(operationConfiguration.NavigationSource.Name));
}
AddOperationParameters(operation, operationConfiguration, edmTypeMap);
if (operationConfiguration.IsBindable)
{
AddOperationLinkBuilder(model, operation, operationConfiguration);
ValidateOperationEntitySetPath(model, operationImport, operationConfiguration);
}
else
{
container.AddElement(operationImport);
}
model.AddElement(operation);
}
}
private static EdmOperationImport CreateActionImport(
OperationConfiguration operationConfiguration,
EdmEntityContainer container,
IEdmTypeReference returnReference,
IEdmExpression expression,
IEdmPathExpression pathExpression)
{
EdmAction operation = new EdmAction(
operationConfiguration.Namespace,
operationConfiguration.Name,
returnReference,
operationConfiguration.IsBindable,
pathExpression);
return new EdmActionImport(container, operationConfiguration.Name, operation, expression);
}
private static EdmOperationImport CreateFunctionImport(
FunctionConfiguration function,
EdmEntityContainer container,
IEdmTypeReference returnReference,
IEdmExpression expression,
IEdmPathExpression pathExpression)
{
EdmFunction operation = new EdmFunction(
function.Namespace,
function.Name,
returnReference,
function.IsBindable,
pathExpression,
function.IsComposable);
return new EdmFunctionImport(container, function.Name, operation, expression, includeInServiceDocument: function.IncludeInServiceDocument);
}
// 11.5.4.2 Action Overload Resolution
// The same action name may be used multiple times within a schema provided there is at most one unbound overload,
// and each bound overload specifies a different binding parameter type. If the action is bound and the binding
// parameter type is part of an inheritance hierarchy, the action overload is selected based on the type of the
// URL segment preceding the action name. A type-cast segment can be used to select an action defined on a
// particular type in the hierarchy.
private static void ValidateActionOverload(IEnumerable<ActionConfiguration> configurations)
{
// 1. validate at most one unbound overload
ActionConfiguration[] unboundActions = configurations.Where(a => !a.IsBindable).ToArray();
if (unboundActions.Length > 0)
{
HashSet<string> unboundActionNames = new HashSet<string>();
foreach (ActionConfiguration action in unboundActions)
{
if (!unboundActionNames.Contains(action.Name))
{
unboundActionNames.Add(action.Name);
}
else
{
throw Error.InvalidOperation(SRResources.MoreThanOneUnboundActionFound, action.Name);
}
}
}
// 2. validate each bound overload action specifies a different binding parameter type
ActionConfiguration[] boundActions = configurations.Where(a => a.IsBindable).ToArray();
if (boundActions.Length > 0)
{
var actionNamesToBindingTypes = new Dictionary<string, IList<IEdmTypeConfiguration>>();
foreach (ActionConfiguration action in boundActions)
{
IEdmTypeConfiguration newBindingType = action.BindingParameter.TypeConfiguration;
if (actionNamesToBindingTypes.ContainsKey(action.Name))
{
IList<IEdmTypeConfiguration> bindingTypes = actionNamesToBindingTypes[action.Name];
foreach (IEdmTypeConfiguration type in bindingTypes)
{
if (type == newBindingType)
{
throw Error.InvalidOperation(SRResources.MoreThanOneOverloadActionBoundToSameTypeFound,
action.Name, type.FullName);
}
}
bindingTypes.Add(newBindingType);
}
else
{
IList<IEdmTypeConfiguration> bindingTypes = new List<IEdmTypeConfiguration>();
bindingTypes.Add(newBindingType);
actionNamesToBindingTypes.Add(action.Name, bindingTypes);
}
}
}
}
private static Dictionary<Type, IEdmType> AddTypes(this EdmModel model, EdmTypeMap edmTypeMap)
{
// build types
Dictionary<Type, IEdmType> edmTypes = edmTypeMap.EdmTypes;
// Add an annotate types
model.AddTypes(edmTypes);
model.AddClrTypeAnnotations(edmTypes);
// add annotation for properties
Dictionary<PropertyDescriptor, IEdmProperty> edmProperties = edmTypeMap.EdmProperties;
model.AddClrPropertyInfoAnnotations(edmProperties);
model.AddPropertyRestrictionsAnnotations(edmTypeMap.EdmPropertiesRestrictions);
model.AddPropertiesQuerySettings(edmTypeMap.EdmPropertiesQuerySettings);
model.AddStructuredTypeQuerySettings(edmTypeMap.EdmStructuredTypeQuerySettings);
// add dynamic dictionary property annotation for open types
model.AddDynamicPropertyDictionaryAnnotations(edmTypeMap.OpenTypes);
return edmTypes;
}
private static void AddType(this EdmModel model, IEdmType type)
{
if (type.TypeKind == EdmTypeKind.Complex)
{
model.AddElement(type as IEdmComplexType);
}
else if (type.TypeKind == EdmTypeKind.Entity)
{
model.AddElement(type as IEdmEntityType);
}
else if (type.TypeKind == EdmTypeKind.Enum)
{
model.AddElement(type as IEdmEnumType);
}
else
{
Contract.Assert(false, "Only ComplexTypes, EntityTypes and EnumTypes are supported.");
}
}
private static EdmEntitySet AddEntitySet(this EdmEntityContainer container, EntitySetConfiguration entitySet, IDictionary<Type, IEdmType> edmTypeMap)
{
return container.AddEntitySet(entitySet.Name, (IEdmEntityType)edmTypeMap[entitySet.EntityType.ClrType]);
}
private static IEnumerable<Tuple<EdmEntitySet, EntitySetConfiguration>> AddEntitySets(IEnumerable<EntitySetConfiguration> entitySets, EdmEntityContainer container, Dictionary<Type, IEdmType> edmTypeMap)
{
return entitySets.Select(es => Tuple.Create(container.AddEntitySet(es, edmTypeMap), es));
}
private static EdmSingleton AddSingleton(this EdmEntityContainer container, SingletonConfiguration singletonType, IDictionary<Type, IEdmType> edmTypeMap)
{
return container.AddSingleton(singletonType.Name, (IEdmEntityType)edmTypeMap[singletonType.EntityType.ClrType]);
}
private static IEnumerable<Tuple<EdmSingleton, SingletonConfiguration>> AddSingletons(IEnumerable<SingletonConfiguration> singletons, EdmEntityContainer container, Dictionary<Type, IEdmType> edmTypeMap)
{
return singletons.Select(sg => Tuple.Create(container.AddSingleton(sg, edmTypeMap), sg));
}
private static void AddClrTypeAnnotations(this EdmModel model, Dictionary<Type, IEdmType> edmTypes)
{
foreach (KeyValuePair<Type, IEdmType> map in edmTypes)
{
// pre-populate the model with clr-type annotations so that we dont have to scan
// all loaded assemblies to find the clr type for an edm type that we build.
IEdmType edmType = map.Value;
Type clrType = map.Key;
model.SetAnnotationValue<ClrTypeAnnotation>(edmType, new ClrTypeAnnotation(clrType));
}
}
private static void AddClrPropertyInfoAnnotations(this EdmModel model, Dictionary<PropertyDescriptor, IEdmProperty> edmProperties)
{
foreach (KeyValuePair<PropertyDescriptor, IEdmProperty> edmPropertyMap in edmProperties)
{
IEdmProperty edmProperty = edmPropertyMap.Value;
PropertyDescriptor clrProperty = edmPropertyMap.Key;
if (clrProperty.MethodInfo != null || edmProperty.Name != clrProperty.Name)
{
model.SetAnnotationValue(edmProperty, new ClrPropertyInfoAnnotation(clrProperty));
}
}
}
private static void AddDynamicPropertyDictionaryAnnotations(this EdmModel model,
Dictionary<IEdmStructuredType, PropertyInfo> openTypes)
{
foreach (KeyValuePair<IEdmStructuredType, PropertyInfo> openType in openTypes)
{
IEdmStructuredType edmStructuredType = openType.Key;
PropertyInfo propertyInfo = openType.Value;
model.SetAnnotationValue(edmStructuredType, new DynamicPropertyDictionaryAnnotation(propertyInfo));
}
}
private static void AddPropertiesQuerySettings(this EdmModel model,
Dictionary<IEdmProperty, ModelBoundQuerySettings> edmPropertiesQuerySettings)
{
foreach (KeyValuePair<IEdmProperty, ModelBoundQuerySettings> edmPropertiesQuerySetting in
edmPropertiesQuerySettings)
{
IEdmProperty edmProperty = edmPropertiesQuerySetting.Key;
ModelBoundQuerySettings querySettings = edmPropertiesQuerySetting.Value;
model.SetAnnotationValue(edmProperty, querySettings);
}
}
private static void AddStructuredTypeQuerySettings(this EdmModel model,
Dictionary<IEdmStructuredType, ModelBoundQuerySettings> edmStructuredTypeQuerySettings)
{
foreach (
KeyValuePair<IEdmStructuredType, ModelBoundQuerySettings> edmStructuredTypeQuerySetting in
edmStructuredTypeQuerySettings)
{
IEdmStructuredType structuredType = edmStructuredTypeQuerySetting.Key;
ModelBoundQuerySettings querySettings = edmStructuredTypeQuerySetting.Value;
model.SetAnnotationValue(structuredType, querySettings);
}
}
private static void AddPropertyRestrictionsAnnotations(this EdmModel model, Dictionary<IEdmProperty, QueryableRestrictions> edmPropertiesRestrictions)
{
foreach (KeyValuePair<IEdmProperty, QueryableRestrictions> edmPropertyRestriction in edmPropertiesRestrictions)
{
IEdmProperty edmProperty = edmPropertyRestriction.Key;
QueryableRestrictions restrictions = edmPropertyRestriction.Value;
model.SetAnnotationValue(edmProperty, new QueryableRestrictionsAnnotation(restrictions));
}
}
private static void AddCoreVocabularyAnnotations(this EdmModel model, IEnumerable<NavigationSourceAndAnnotations> navigationSources, EdmTypeMap edmTypeMap)
{
Contract.Assert(model != null);
Contract.Assert(edmTypeMap != null);
if (navigationSources == null)
{
return;
}
foreach (NavigationSourceAndAnnotations source in navigationSources)
{
IEdmVocabularyAnnotatable navigationSource = source.NavigationSource as IEdmVocabularyAnnotatable;
if (navigationSource == null)
{
continue;
}
NavigationSourceConfiguration navigationSourceConfig = source.Configuration as NavigationSourceConfiguration;
if (navigationSourceConfig == null)
{
continue;
}
model.AddOptimisticConcurrencyAnnotation(navigationSource, navigationSourceConfig, edmTypeMap);
}
}
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling",
Justification = "Relies on many ODataLib classes.")]
private static void AddOptimisticConcurrencyAnnotation(this EdmModel model, IEdmVocabularyAnnotatable target,
NavigationSourceConfiguration navigationSourceConfiguration, EdmTypeMap edmTypeMap)
{
EntityTypeConfiguration entityTypeConfig = navigationSourceConfiguration.EntityType;
IEnumerable<StructuralPropertyConfiguration> concurrencyPropertyies =
entityTypeConfig.Properties.OfType<StructuralPropertyConfiguration>().Where(property => property.ConcurrencyToken);
IList<IEdmStructuralProperty> edmProperties = new List<IEdmStructuralProperty>();
foreach (StructuralPropertyConfiguration property in concurrencyPropertyies)
{
IEdmProperty value;
if (edmTypeMap.EdmProperties.TryGetValue(property.PropertyInfo, out value))
{
var item = value as IEdmStructuralProperty;
if (item != null)
{
edmProperties.Add(item);
}
}
}
if (edmProperties.Any())
{
IEdmCollectionExpression collectionExpression = new EdmCollectionExpression(edmProperties.Select(p => new EdmPropertyPathExpression(p.Name)).ToArray());
IEdmTerm term = Microsoft.OData.Edm.Vocabularies.V1.CoreVocabularyModel.ConcurrencyTerm;
EdmVocabularyAnnotation annotation = new EdmVocabularyAnnotation(target, term, collectionExpression);
annotation.SetSerializationLocation(model, EdmVocabularyAnnotationSerializationLocation.Inline);
model.SetVocabularyAnnotation(annotation);
}
}
private static void AddCapabilitiesVocabularyAnnotations(this EdmModel model, IEnumerable<NavigationSourceAndAnnotations> navigationSources, EdmTypeMap edmTypeMap)
{
Contract.Assert(model != null);
Contract.Assert(edmTypeMap != null);
if (navigationSources == null)
{
return;
}
foreach (NavigationSourceAndAnnotations source in navigationSources)
{
IEdmEntitySet entitySet = source.NavigationSource as IEdmEntitySet;
if (entitySet == null)
{
continue;
}
EntitySetConfiguration entitySetConfig = source.Configuration as EntitySetConfiguration;
if (entitySetConfig == null)
{
continue;
}
model.AddCountRestrictionsAnnotation(entitySet, entitySetConfig, edmTypeMap);
model.AddNavigationRestrictionsAnnotation(entitySet, entitySetConfig, edmTypeMap);
model.AddFilterRestrictionsAnnotation(entitySet, entitySetConfig, edmTypeMap);
model.AddSortRestrictionsAnnotation(entitySet, entitySetConfig, edmTypeMap);
model.AddExpandRestrictionsAnnotation(entitySet, entitySetConfig, edmTypeMap);
}
}
private static void AddCountRestrictionsAnnotation(this EdmModel model, IEdmEntitySet target,
EntitySetConfiguration entitySetConfiguration, EdmTypeMap edmTypeMap)
{
EntityTypeConfiguration entityTypeConfig = entitySetConfiguration.EntityType;
IEnumerable<PropertyConfiguration> notCountableProperties = entityTypeConfig.Properties.Where(property => property.NotCountable);
IList<IEdmProperty> nonCountableProperties = new List<IEdmProperty>();
IList<IEdmNavigationProperty> nonCountableNavigationProperties = new List<IEdmNavigationProperty>();
foreach (PropertyConfiguration property in notCountableProperties)
{
IEdmProperty value;
if (edmTypeMap.EdmProperties.TryGetValue(property.PropertyInfo, out value))
{
if (value != null && value.Type.TypeKind() == EdmTypeKind.Collection)
{
if (value.PropertyKind == EdmPropertyKind.Navigation)
{
nonCountableNavigationProperties.Add((IEdmNavigationProperty)value);
}
else
{
nonCountableProperties.Add(value);
}
}
}
}
if (nonCountableProperties.Any() || nonCountableNavigationProperties.Any())
{
model.SetCountRestrictionsAnnotation(target, true, nonCountableProperties, nonCountableNavigationProperties);
}
}
private static void AddNavigationRestrictionsAnnotation(this EdmModel model, IEdmEntitySet target,
EntitySetConfiguration entitySetConfiguration, EdmTypeMap edmTypeMap)
{
EntityTypeConfiguration entityTypeConfig = entitySetConfiguration.EntityType;
IEnumerable<PropertyConfiguration> notNavigableProperties = entityTypeConfig.Properties.Where(property => property.NotNavigable);
IList<Tuple<IEdmNavigationProperty, CapabilitiesNavigationType>> properties =
new List<Tuple<IEdmNavigationProperty, CapabilitiesNavigationType>>();
foreach (PropertyConfiguration property in notNavigableProperties)
{
IEdmProperty value;
if (edmTypeMap.EdmProperties.TryGetValue(property.PropertyInfo, out value))
{
if (value != null && value.PropertyKind == EdmPropertyKind.Navigation)
{
properties.Add(new Tuple<IEdmNavigationProperty, CapabilitiesNavigationType>(
(IEdmNavigationProperty)value, CapabilitiesNavigationType.Recursive));
}
}
}
if (properties.Any())
{
model.SetNavigationRestrictionsAnnotation(target, CapabilitiesNavigationType.Recursive, properties);
}
}
private static void AddFilterRestrictionsAnnotation(this EdmModel model, IEdmEntitySet target,
EntitySetConfiguration entitySetConfiguration, EdmTypeMap edmTypeMap)
{
EntityTypeConfiguration entityTypeConfig = entitySetConfiguration.EntityType;
IEnumerable<PropertyConfiguration> notFilterProperties = entityTypeConfig.Properties.Where(property => property.NonFilterable);
IList<IEdmProperty> properties = new List<IEdmProperty>();
foreach (PropertyConfiguration property in notFilterProperties)
{
IEdmProperty value;
if (edmTypeMap.EdmProperties.TryGetValue(property.PropertyInfo, out value))
{
if (value != null)
{
properties.Add(value);
}
}
}
if (properties.Any())
{
model.SetFilterRestrictionsAnnotation(target, true, true, null, properties);
}
}
private static void AddSortRestrictionsAnnotation(this EdmModel model, IEdmEntitySet target,
EntitySetConfiguration entitySetConfiguration, EdmTypeMap edmTypeMap)
{
EntityTypeConfiguration entityTypeConfig = entitySetConfiguration.EntityType;
IEnumerable<PropertyConfiguration> nonSortableProperties = entityTypeConfig.Properties.Where(property => property.Unsortable);
IList<IEdmProperty> properties = new List<IEdmProperty>();
foreach (PropertyConfiguration property in nonSortableProperties)
{
IEdmProperty value;
if (edmTypeMap.EdmProperties.TryGetValue(property.PropertyInfo, out value))
{
if (value != null)
{
properties.Add(value);
}
}
}
if (properties.Any())
{
model.SetSortRestrictionsAnnotation(target, true, null, null, properties);
}
}
private static void AddExpandRestrictionsAnnotation(this EdmModel model, IEdmEntitySet target,
EntitySetConfiguration entitySetConfiguration, EdmTypeMap edmTypeMap)
{
EntityTypeConfiguration entityTypeConfig = entitySetConfiguration.EntityType;
IEnumerable<PropertyConfiguration> nonExpandableProperties = entityTypeConfig.Properties.Where(property => property.NotExpandable);
IList<IEdmNavigationProperty> properties = new List<IEdmNavigationProperty>();
foreach (PropertyConfiguration property in nonExpandableProperties)
{
IEdmProperty value;
if (edmTypeMap.EdmProperties.TryGetValue(property.PropertyInfo, out value))
{
if (value != null && value.PropertyKind == EdmPropertyKind.Navigation)
{
properties.Add((IEdmNavigationProperty)value);
}
}
}
if (properties.Any())
{
model.SetExpandRestrictionsAnnotation(target, true, properties);
}
}
private static IEdmExpression GetEdmEntitySetExpression(IDictionary<string, EdmNavigationSource> navigationSources, OperationConfiguration operationConfiguration)
{
if (operationConfiguration.NavigationSource != null)
{
EdmNavigationSource navigationSource;
if (navigationSources.TryGetValue(operationConfiguration.NavigationSource.Name, out navigationSource))
{
EdmEntitySet entitySet = navigationSource as EdmEntitySet;
if (entitySet != null)
{
return new EdmPathExpression(entitySet.Name);
}
}
else
{
throw Error.InvalidOperation(SRResources.EntitySetNotFoundForName, operationConfiguration.NavigationSource.Name);
}
}
else if (operationConfiguration.EntitySetPath != null)
{
return new EdmPathExpression(operationConfiguration.EntitySetPath);
}
return null;
}
private static IEdmTypeReference GetEdmTypeReference(Dictionary<Type, IEdmType> availableTypes, IEdmTypeConfiguration configuration, bool nullable)
{
Contract.Assert(availableTypes != null);
if (configuration == null)
{
return null;
}
EdmTypeKind kind = configuration.Kind;
if (kind == EdmTypeKind.Collection)
{
CollectionTypeConfiguration collectionType = (CollectionTypeConfiguration)configuration;
EdmCollectionType edmCollectionType =
new EdmCollectionType(GetEdmTypeReference(availableTypes, collectionType.ElementType, nullable));
return new EdmCollectionTypeReference(edmCollectionType);
}
else
{
Type configurationClrType = TypeHelper.GetUnderlyingTypeOrSelf(configuration.ClrType);
if (!TypeHelper.IsEnum(configurationClrType))
{
configurationClrType = configuration.ClrType;
}
IEdmType type;
if (availableTypes.TryGetValue(configurationClrType, out type))
{
if (kind == EdmTypeKind.Complex)
{
return new EdmComplexTypeReference((IEdmComplexType)type, nullable);
}
else if (kind == EdmTypeKind.Entity)
{
return new EdmEntityTypeReference((IEdmEntityType)type, nullable);
}
else if (kind == EdmTypeKind.Enum)
{
return new EdmEnumTypeReference((IEdmEnumType)type, nullable);
}
else
{
throw Error.InvalidOperation(SRResources.UnsupportedEdmTypeKind, kind.ToString());
}
}
else if (configuration.Kind == EdmTypeKind.Primitive)
{
PrimitiveTypeConfiguration primitiveTypeConfiguration = configuration as PrimitiveTypeConfiguration;
return new EdmPrimitiveTypeReference(primitiveTypeConfiguration.EdmPrimitiveType, nullable);
}
else
{
throw Error.InvalidOperation(SRResources.NoMatchingIEdmTypeFound, configuration.FullName);
}
}
}
internal static string GetNavigationSourceUrl(this IEdmModel model, IEdmNavigationSource navigationSource)
{
if (model == null)
{
throw Error.ArgumentNull("model");
}
if (navigationSource == null)
{
throw Error.ArgumentNull("navigationSource");
}
NavigationSourceUrlAnnotation annotation = model.GetAnnotationValue<NavigationSourceUrlAnnotation>(navigationSource);
if (annotation == null)
{
return navigationSource.Name;
}
else
{
return annotation.Url;
}
}
internal static IEnumerable<IEdmAction> GetAvailableActions(this IEdmModel model, IEdmEntityType entityType)
{
return model.GetAvailableOperations(entityType, false).OfType<IEdmAction>();
}
internal static IEnumerable<IEdmFunction> GetAvailableFunctions(this IEdmModel model, IEdmEntityType entityType)
{
return model.GetAvailableOperations(entityType, false).OfType<IEdmFunction>();
}
internal static IEnumerable<IEdmOperation> GetAvailableOperationsBoundToCollection(this IEdmModel model, IEdmEntityType entityType)
{
return model.GetAvailableOperations(entityType, true);
}
internal static IEnumerable<IEdmOperation> GetAvailableOperations(this IEdmModel model, IEdmEntityType entityType, bool boundToCollection = false)
{
if (model == null)
{
throw Error.ArgumentNull("model");
}
if (entityType == null)
{
throw Error.ArgumentNull("entityType");
}
BindableOperationFinder annotation = model.GetAnnotationValue<BindableOperationFinder>(model);
if (annotation == null)
{
annotation = new BindableOperationFinder(model);
model.SetAnnotationValue(model, annotation);
}
if (boundToCollection)
{
return annotation.FindOperationsBoundToCollection(entityType);
}
else
{
return annotation.FindOperations(entityType);
}
}
}
}