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 = (PrimitiveTypeConfiguration)configuration; EdmPrimitiveTypeKind typeKind = EdmTypeBuilder.GetTypeKind(primitiveTypeConfiguration.ClrType); return(EdmCoreModel.Instance.GetPrimitive(typeKind, nullable)); } else { throw Error.InvalidOperation(SRResources.NoMatchingIEdmTypeFound, configuration.FullName); } } }
/// <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._members, builder._openTypes); }
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); }
/// <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._members, builder._openTypes)); }
private static Dictionary <Type, IEdmType> AddTypes(this EdmModel model, IEnumerable <StructuralTypeConfiguration> types, IEnumerable <EnumTypeConfiguration> enumTypes) { IEnumerable <IEdmTypeConfiguration> configTypes = types.Concat <IEdmTypeConfiguration>(enumTypes); // build types EdmTypeMap edmTypeMap = EdmTypeBuilder.GetTypesAndProperties(configTypes); Dictionary <Type, IEdmType> edmTypes = edmTypeMap.EdmTypes; // Add an annotate types model.AddTypes(edmTypes); model.AddClrTypeAnnotations(edmTypes); // add annotation for properties Dictionary <PropertyInfo, IEdmProperty> edmProperties = edmTypeMap.EdmProperties; model.AddClrPropertyInfoAnnotations(edmProperties); model.AddPropertyRestrictionsAnnotations(edmTypeMap.EdmPropertiesRestrictions); // add dynamic dictionary property annotation for open types model.AddDynamicPropertyDictionaryAnnotations(edmTypeMap.OpenTypes); return(edmTypes); }