/// <inheritdoc /> public override EnumTypeConfiguration AddEnumType(Type type) { if (type == null) { throw Error.ArgumentNull("type"); } if (!type.IsEnum) { throw Error.Argument("type", SRResources.TypeCannotBeEnum, type.FullName); } EnumTypeConfiguration enumTypeConfiguration = this.EnumTypes.SingleOrDefault(e => e.ClrType == type); if (enumTypeConfiguration == null) { enumTypeConfiguration = base.AddEnumType(type); foreach (object member in Enum.GetValues(type)) { bool addedExplicitly = enumTypeConfiguration.Members.Any(m => m.Name.Equals(member.ToString())); EnumMemberConfiguration enumMemberConfiguration = enumTypeConfiguration.AddMember((Enum)member); enumMemberConfiguration.AddedExplicitly = addedExplicitly; } this.ApplyEnumTypeConventions(enumTypeConfiguration); } return(enumTypeConfiguration); }
public static ODataModelBuilder Add_ShortEnum_EnumType(this ODataModelBuilder builder) { EnumTypeConfiguration <ShortEnum> shortEnum = builder.EnumType <ShortEnum>(); shortEnum.Member(ShortEnum.FirstShort); shortEnum.Member(ShortEnum.SecondShort); shortEnum.Member(ShortEnum.ThirdShort); return(builder); }
public static ODataModelBuilder Add_ByteEnum_EnumType(this ODataModelBuilder builder) { EnumTypeConfiguration <ByteEnum> byteEnum = builder.EnumType <ByteEnum>(); byteEnum.Member(ByteEnum.FirstByte); byteEnum.Member(ByteEnum.SecondByte); byteEnum.Member(ByteEnum.ThirdByte); return(builder); }
private static IEdmModel GetEdmModel() { ODataModelBuilder builder = new ODataModelBuilder(); // Configure LimitedEntity EntitySetConfiguration <LimitedEntity> limitedEntities = builder.EntitySet <LimitedEntity>("LimitedEntities"); limitedEntities.EntityType.HasKey(p => p.Id); limitedEntities.EntityType.ComplexProperty(c => c.ComplexProperty); limitedEntities.EntityType.CollectionProperty(c => c.ComplexCollectionProperty).IsNotCountable(); limitedEntities.EntityType.HasMany(l => l.EntityCollectionProperty).IsNotCountable(); limitedEntities.EntityType.CollectionProperty(cp => cp.Integers).IsNotCountable(); // Configure LimitedRelatedEntity EntitySetConfiguration <LimitedRelatedEntity> limitedRelatedEntities = builder.EntitySet <LimitedRelatedEntity>("LimitedRelatedEntities"); limitedRelatedEntities.EntityType.HasKey(p => p.Id); limitedRelatedEntities.EntityType.CollectionProperty(p => p.ComplexCollectionProperty).IsNotCountable(); // Configure Complextype ComplexTypeConfiguration <LimitedComplex> complexType = builder.ComplexType <LimitedComplex>(); complexType.CollectionProperty(p => p.Strings).IsNotCountable(); complexType.Property(p => p.Value); complexType.CollectionProperty(p => p.SimpleEnums).IsNotCountable(); // Configure EnumType EnumTypeConfiguration <SimpleEnum> enumType = builder.EnumType <SimpleEnum>(); enumType.Member(SimpleEnum.First); enumType.Member(SimpleEnum.Second); enumType.Member(SimpleEnum.Third); enumType.Member(SimpleEnum.Fourth); return(builder.GetEdmModel()); }
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 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); } if (complex.HasInstanceAnnotations) { // add a mapping between the complex type and its instance annotation dictionary. _instanceAnnotableTypes.Add(complexType, complex.InstanceAnnotationsContainer); } 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); } if (entity.HasInstanceAnnotations) { // add a mapping between the entity type and its instance annotation dictionary. _instanceAnnotableTypes.Add(entityType, entity.InstanceAnnotationsContainer); } 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); //} } }
public static IEdmModel GetEdmModel(ODataConventionModelBuilder builder) { builder.EntitySet <ConventionCustomer>("ConventionCustomers"); builder.EntitySet <ConventionOrder>("ConventionOrders"); EnumTypeConfiguration <ConventionGender> enumType = builder.EnumType <ConventionGender>(); enumType.Member(ConventionGender.Female); enumType.Member(ConventionGender.Male); #region functions FunctionConfiguration getAllCustomers = builder.Function("GetAllConventionCustomers"); getAllCustomers.ReturnsCollectionFromEntitySet <ConventionCustomer>("ConventionCustomers"); getAllCustomers.IsComposable = true; // Return all the customers whose name contains CustomerName FunctionConfiguration getAllCustomersOverload = builder.Function("GetAllConventionCustomers"); getAllCustomersOverload.ReturnsCollectionFromEntitySet <ConventionCustomer>("ConventionCustomers"); getAllCustomersOverload.Parameter <string>("CustomerName"); getAllCustomersOverload.IsComposable = true; FunctionConfiguration getCustomersById = builder.Function("GetConventionCustomerById"); getCustomersById.Parameter <int>("CustomerId"); getCustomersById.ReturnsFromEntitySet <ConventionCustomer>("ConventionCustomers"); getCustomersById.IsComposable = true; FunctionConfiguration getOrder = builder.Function("GetConventionOrderByCustomerIdAndOrderName"); getOrder.Parameter <int>("CustomerId"); getOrder.Parameter <string>("OrderName"); getOrder.ReturnsFromEntitySet <ConventionOrder>("ConventionOrders"); FunctionConfiguration getCustomerNameById = builder.Function("GetConventionCustomerNameById"); getCustomerNameById.Parameter <int>("CustomerId"); getCustomerNameById.Returns <string>(); FunctionConfiguration getDefinedGenders = builder.Function("GetDefinedGenders"); getDefinedGenders.ReturnsCollection <ConventionGender>() .IsComposable = true; FunctionConfiguration function = builder.Function("AdvancedFunction").Returns <bool>(); function.CollectionParameter <int>("nums"); function.CollectionParameter <ConventionGender>("genders"); function.Parameter <ConventionAddress>("location"); function.CollectionParameter <ConventionAddress>("addresses"); function.EntityParameter <ConventionCustomer>("customer"); function.CollectionEntityParameter <ConventionCustomer>("customers"); #endregion #region actions ActionConfiguration resetDataSource = builder.Action("ResetDataSource"); // bug: error message: non-binding parameter type must be either Primitive, Complex, Collection of Primitive or a Collection of Complex. /* * ActionConfiguration createCustomer = builder.Action("CreateCustomer"); * createCustomer.Parameter<ConventionCustomer>("Customer"); * createCustomer.ReturnsFromEntitySet<ConventionCustomer>("ConventionCustomers"); */ ActionConfiguration udpateAddress = builder.Action("UpdateAddress"); udpateAddress.Parameter <ConventionAddress>("Address"); udpateAddress.Parameter <int>("ID"); udpateAddress.ReturnsCollectionFromEntitySet <ConventionCustomer>("ConventionCustomers"); ActionConfiguration action = builder.Action("AdvancedAction"); action.CollectionParameter <int>("nums"); action.CollectionParameter <ConventionGender>("genders"); action.Parameter <ConventionAddress>("location"); action.CollectionParameter <ConventionAddress>("addresses"); action.EntityParameter <ConventionCustomer>("customer"); action.CollectionEntityParameter <ConventionCustomer>("customers"); #endregion var schemaNamespace = typeof(ConventionCustomer).Namespace; builder.Namespace = schemaNamespace; var edmModel = builder.GetEdmModel(); var container = edmModel.EntityContainer as EdmEntityContainer; #region function imports var entitySet = container.FindEntitySet("ConventionCustomers"); var getCustomersByIdOfEdmFunction = edmModel.FindDeclaredOperations(schemaNamespace + ".GetConventionCustomerById").First() as EdmFunction; container.AddFunctionImport("GetConventionCustomerByIdImport", getCustomersByIdOfEdmFunction, new EdmPathExpression(entitySet.Name)); var functionsOfGetAllConventionCustomers = edmModel.FindDeclaredOperations(schemaNamespace + ".GetAllConventionCustomers"); var getAllConventionCustomersOfEdmFunction = functionsOfGetAllConventionCustomers.FirstOrDefault(f => f.Parameters.Count() == 0) as EdmFunction; container.AddFunctionImport("GetAllConventionCustomersImport", getAllConventionCustomersOfEdmFunction, new EdmPathExpression(entitySet.Name)); // TODO delete this overload after bug 1640 is fixed: It can not find the correct overload function if the the function is exposed as a function import. var getAllConventionCustomersOverloadOfEdmFunction = functionsOfGetAllConventionCustomers.FirstOrDefault(f => f.Parameters.Count() > 0) as EdmFunction; container.AddFunctionImport("GetAllConventionCustomersImport", getAllConventionCustomersOverloadOfEdmFunction, new EdmPathExpression(entitySet.Name)); var entitySet1 = container.FindEntitySet("ConventionOrders"); var GetConventionOrderByCustomerIdAndOrderNameOfEdmFunction = edmModel.FindDeclaredOperations(schemaNamespace + ".GetConventionOrderByCustomerIdAndOrderName").First() as EdmFunction; container.AddFunctionImport("GetConventionOrderByCustomerIdAndOrderNameImport", GetConventionOrderByCustomerIdAndOrderNameOfEdmFunction, new EdmPathExpression(entitySet1.Name)); var getConventionCustomerNameByIdOfEdmFunction = edmModel.FindDeclaredOperations(schemaNamespace + ".GetConventionCustomerNameById").First() as EdmFunction; container.AddFunctionImport("GetConventionCustomerNameByIdImport", getConventionCustomerNameByIdOfEdmFunction, null); #endregion #region action imports var resetDataSourceOfEdmAction = edmModel.FindDeclaredOperations(schemaNamespace + ".ResetDataSource").FirstOrDefault() as EdmAction; container.AddActionImport("ResetDataSourceImport", resetDataSourceOfEdmAction); // TODO: it is a potential issue that entity can not be used as an un-bound parameter. /* * var createCustomerOfEdmAction = edmModel.FindDeclaredOperations(schemaNamespace + ".CreateCustomer").FirstOrDefault() as EdmAction; * container.AddActionImport("CreateCustomerImport", createCustomerOfEdmAction); */ var updateAddressOfEdmAction = edmModel.FindDeclaredOperations(schemaNamespace + ".UpdateAddress").FirstOrDefault() as EdmAction; container.AddActionImport("UpdateAddressImport", updateAddressOfEdmAction, new EdmPathExpression(entitySet.Name)); #endregion return(edmModel); }
private void ApplyEnumTypeConventions(EnumTypeConfiguration enumTypeConfiguration) { DataContractAttributeEnumTypeConvention typeConvention = new DataContractAttributeEnumTypeConvention(); typeConvention.Apply(enumTypeConfiguration, this); }