public void NonbindingParameterConfigurationSupportsParameterTypeAs(Type type, bool isNullable) { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); builder.EntityType<Customer>(); builder.ComplexType<Address>(); builder.EnumType<Color>(); // Act Type underlyingType = TypeHelper.GetUnderlyingTypeOrSelf(type); IEdmTypeConfiguration edmTypeConfiguration = builder.GetTypeConfigurationOrNull(type); if (underlyingType.IsEnum) { edmTypeConfiguration = builder.GetTypeConfigurationOrNull(underlyingType); if (edmTypeConfiguration != null && isNullable) { edmTypeConfiguration = ((EnumTypeConfiguration)edmTypeConfiguration).GetNullableEnumTypeConfiguration(); } } NonbindingParameterConfiguration parameter = new NonbindingParameterConfiguration("name", edmTypeConfiguration); // Assert Assert.Equal(isNullable, parameter.OptionalParameter); }
public void NonbindingParameterConfigurationSupportsParameterTypeAs(Type type, bool isNullable) { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); builder.EntityType <Customer>(); builder.ComplexType <Address>(); builder.EnumType <Color>(); // Act Type underlyingType = TypeHelper.GetUnderlyingTypeOrSelf(type); IEdmTypeConfiguration edmTypeConfiguration = builder.GetTypeConfigurationOrNull(type); if (underlyingType.IsEnum) { edmTypeConfiguration = builder.GetTypeConfigurationOrNull(underlyingType); if (edmTypeConfiguration != null && isNullable) { edmTypeConfiguration = ((EnumTypeConfiguration)edmTypeConfiguration).GetNullableEnumTypeConfiguration(); } } NonbindingParameterConfiguration parameter = new NonbindingParameterConfiguration("name", edmTypeConfiguration); // Assert Assert.Equal(isNullable, parameter.Nullable); }
private static IEdmTypeConfiguration GetOrAddComplexType(ODataModelBuilder builder, Type type) { Type collectionType; type = GetEnumerableType(type, out collectionType); IEdmTypeConfiguration typeConfigurationOrNull = builder.GetTypeConfigurationOrNull(type); if (typeConfigurationOrNull == null) { builder.AddComplexType(type); typeConfigurationOrNull = builder.GetTypeConfigurationOrNull(type); } return(collectionType == null ? typeConfigurationOrNull : new CollectionTypeConfiguration(typeConfigurationOrNull, collectionType)); }
public static ActionConfiguration ActionReturnsCollectionFromEntitySet <TElementEntityType>(ODataModelBuilder builder, ActionConfiguration action, string entitySetName) where TElementEntityType : class { Type clrCollectionType = typeof(IEnumerable <TElementEntityType>); action.NavigationSource = CreateOrReuseEntitySet <TElementEntityType>(builder, entitySetName); IEdmTypeConfiguration elementType = builder.GetTypeConfigurationOrNull(typeof(TElementEntityType)); action.ReturnType = new CollectionTypeConfiguration(elementType, clrCollectionType); return(action); }
public void NonbindingParameterConfigurationThrowsWhenParameterTypeIsEntity() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); builder.EntityType<Customer>(); // Act & Assert ArgumentException exception = Assert.Throws<ArgumentException>(() => { NonbindingParameterConfiguration configuration = new NonbindingParameterConfiguration("name", builder.GetTypeConfigurationOrNull(typeof(Customer))); }); Assert.True(exception.Message.Contains(string.Format("'{0}'", typeof(Customer).FullName))); Assert.Equal("parameterType", exception.ParamName); }
public void BindingParameterConfigurationThrowsWhenParameterTypeIsNotEntity() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); builder.ComplexType<Address>(); // Act & Assert ArgumentException exception = Assert.Throws<ArgumentException>(() => { BindingParameterConfiguration configuration = new BindingParameterConfiguration("name", builder.GetTypeConfigurationOrNull(typeof(Address)), true); }); Assert.True(exception.Message.Contains(string.Format("'{0}'", typeof(Address).FullName))); Assert.Equal("parameterType", exception.ParamName); }
/// <summary> /// If a caller wants to tweak the <see cref="ODataModelBuilder"/>, this method can be used instead of <see cref="BuildEdmModel"/>, /// so that the caller can modify <paramref name="modelBuilder"/> before or after the entity sets are added. /// </summary> /// <param name="modelBuilder">An <see cref="ODataModelBuilder"/> or <see cref="ODataConventionModelBuilder"/>.</param> public virtual void ConfigureModelBuilder(ODataModelBuilder modelBuilder) { modelBuilder.ContainerName = _containerMetadata.Name; modelBuilder.Namespace = _containerMetadata.Namespace; // Add all entity types foreach (IEntityTypeMetadata entityTypeMetadata in _containerMetadata.EntityTypes) { EntityTypeConfiguration entityTypeConfig = modelBuilder.AddEntity(entityTypeMetadata.ClrType); } // Add all entity sets foreach (IEntitySetMetadata entitySetMetadata in _containerMetadata.EntitySets) { string entitySetName = entitySetMetadata.Name; EntityTypeConfiguration entityTypeConfig = (EntityTypeConfiguration)modelBuilder.GetTypeConfigurationOrNull(entitySetMetadata.ElementTypeMetadata.ClrType); EntitySetConfiguration entitySetConfig = modelBuilder.AddEntitySet(entitySetName, entityTypeConfig); } }
public void NonbindingParameterConfigurationSupportsParameterCollectionTypeAs(Type type, bool isNullable) { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); builder.EntityType<Customer>(); builder.ComplexType<Address>(); builder.EnumType<Color>(); Type elementType; Assert.True(type.IsCollection(out elementType)); // Act Type underlyingType = TypeHelper.GetUnderlyingTypeOrSelf(elementType); IEdmTypeConfiguration elementTypeConfiguration = builder.GetTypeConfigurationOrNull(underlyingType); CollectionTypeConfiguration collectionType = new CollectionTypeConfiguration(elementTypeConfiguration, typeof(IEnumerable<>).MakeGenericType(elementType)); NonbindingParameterConfiguration parameter = new NonbindingParameterConfiguration("name", collectionType); // Assert Assert.Equal(isNullable, parameter.OptionalParameter); }
public void NonbindingParameterConfigurationSupportsParameterCollectionTypeAs(Type type, bool isNullable) { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); builder.EntityType <Customer>(); builder.ComplexType <Address>(); builder.EnumType <Color>(); Type elementType; Assert.True(TypeHelper.IsCollection(type, out elementType)); // Act Type underlyingType = TypeHelper.GetUnderlyingTypeOrSelf(elementType); IEdmTypeConfiguration elementTypeConfiguration = builder.GetTypeConfigurationOrNull(underlyingType); CollectionTypeConfiguration collectionType = new CollectionTypeConfiguration(elementTypeConfiguration, typeof(IEnumerable <>).MakeGenericType(elementType)); NonbindingParameterConfiguration parameter = new NonbindingParameterConfiguration("name", collectionType); // Assert Assert.Equal(isNullable, parameter.Nullable); }
public static ActionConfiguration ActionReturnsFromEntitySet <TEntityType>(ODataModelBuilder builder, ActionConfiguration action, string entitySetName) where TEntityType : class { action.NavigationSource = CreateOrReuseEntitySet <TEntityType>(builder, entitySetName); action.ReturnType = builder.GetTypeConfigurationOrNull(typeof(TEntityType)); return(action); }
public void BindingParameterConfigurationThrowsWhenParameterTypeIsNotEntity() { // Arrange ODataModelBuilder builder = new ODataModelBuilder(); builder.ComplexType <Address>(); // Act & Assert ArgumentException exception = ExceptionAssert.Throws <ArgumentException>(() => { BindingParameterConfiguration configuration = new BindingParameterConfiguration("name", builder.GetTypeConfigurationOrNull(typeof(Address))); }); Assert.Contains(string.Format("'{0}'", typeof(Address).FullName), exception.Message); Assert.Equal("parameterType", exception.ParamName); }
/// <summary> /// If a caller wants to tweak the <see cref="ODataModelBuilder"/>, this method can be used instead of <see cref="BuildEdmModel"/>, /// so that the caller can modify <paramref name="modelBuilder"/> before or after the entity sets are added. /// </summary> /// <param name="modelBuilder">An <see cref="ODataModelBuilder"/> or <see cref="ODataConventionModelBuilder"/>.</param> public virtual void ConfigureModelBuilder(ODataModelBuilder modelBuilder) { modelBuilder.ContainerName = _containerMetadata.Name; modelBuilder.Namespace = _containerMetadata.Namespace; // Add all entity types foreach (IEntityTypeMetadata entityTypeMetadata in _containerMetadata.EntityTypes) { EntityTypeConfiguration entityTypeConfig = modelBuilder.AddEntity(entityTypeMetadata.ClrType); } // Add all entity sets foreach (IEntitySetMetadata entitySetMetadata in _containerMetadata.EntitySets) { string entitySetName = entitySetMetadata.Name; EntityTypeConfiguration entityTypeConfig = (EntityTypeConfiguration) modelBuilder.GetTypeConfigurationOrNull(entitySetMetadata.ElementTypeMetadata.ClrType); EntitySetConfiguration entitySetConfig = modelBuilder.AddEntitySet(entitySetName, entityTypeConfig); } }