Example #1
0
        public void CanCreateActionWithComplexReturnType()
        {
            // Arrange
            // Act
            ODataModelBuilder builder = new ODataModelBuilder();

            ActionConfiguration createAddress = new ActionConfiguration(builder, "CreateAddress");

            createAddress.Returns <Address>();

            ActionConfiguration createAddresses = new ActionConfiguration(builder, "CreateAddresses");

            createAddresses.ReturnsCollection <Address>();

            // Assert
            IComplexTypeConfiguration address = createAddress.ReturnType as IComplexTypeConfiguration;

            Assert.NotNull(address);
            Assert.Equal(typeof(Address).FullName, address.FullName);
            Assert.Null(createAddress.EntitySet);

            ICollectionTypeConfiguration addresses = createAddresses.ReturnType as ICollectionTypeConfiguration;

            Assert.NotNull(addresses);
            Assert.Equal(string.Format("Collection({0})", typeof(Address).FullName), addresses.FullName);
            address = addresses.ElementType as IComplexTypeConfiguration;
            Assert.NotNull(address);
            Assert.Equal(typeof(Address).FullName, address.FullName);
            Assert.Null(createAddresses.EntitySet);
        }
        public override IComplexTypeConfiguration AddComplexType(Type type)
        {
            IComplexTypeConfiguration complexTypeConfiguration = base.AddComplexType(type);

            if (_isModelBeingBuilt)
            {
                MapType(complexTypeConfiguration);
            }

            return(complexTypeConfiguration);
        }
        public override IComplexTypeConfiguration AddComplexType(Type type)
        {
            bool alreadyExists = (GetStructuralTypeOrNull(type) != null);

            IComplexTypeConfiguration complexTypeConfiguration = base.AddComplexType(type);

            if (_isModelBeingBuilt && !alreadyExists)
            {
                MapComplexType(complexTypeConfiguration);
                ApplyTypeConventions(complexTypeConfiguration);
            }

            return(complexTypeConfiguration);
        }
 private void MapComplexType(IComplexTypeConfiguration complexType)
 {
     PropertyInfo[] properties = ConventionsHelpers.GetProperties(complexType.ClrType);
     foreach (PropertyInfo property in properties)
     {
         if (EdmLibHelpers.GetEdmPrimitiveTypeOrNull(property.PropertyType) != null)
         {
             PrimitivePropertyConfiguration primitiveProperty = complexType.AddProperty(property);
             primitiveProperty.OptionalProperty = IsNullable(property.PropertyType);
         }
         else
         {
             complexType.AddComplexProperty(property);
         }
     }
 }
Example #5
0
        private void MapComplexType(IComplexTypeConfiguration complexType)
        {
            PropertyInfo[] properties = ConventionsHelpers.GetProperties(complexType);
            foreach (PropertyInfo property in properties)
            {
                bool isCollection;
                IStructuralTypeConfiguration mappedType;

                PropertyKind propertyKind = GetPropertyType(property, out isCollection, out mappedType);

                if (propertyKind == PropertyKind.Primitive || propertyKind == PropertyKind.Complex)
                {
                    MapStructuralProperty(complexType, property, propertyKind, isCollection);
                }
                else
                {
                    // navigation property in a complex type ?
                    if (!isCollection)
                    {
                        if (mappedType == null)
                        {
                            // the user told nothing about this type and this is the first time we are seeing this type.
                            // complex types cannot contain entities. So, treat it as complex property.
                            complexType.AddComplexProperty(property);
                        }
                        else
                        {
                            if (_explicitlyAddedTypes.Contains(mappedType))
                            {
                                // user told us that this an entity type.
                                throw Error.InvalidOperation(SRResources.ComplexTypeRefersToEntityType, complexType.ClrType.FullName, mappedType.ClrType.FullName, property.Name);
                            }
                            else
                            {
                                // we tried to be over-smart earlier and made the bad choice. so patch up now.
                                ReconfigureEntityTypesAsComplexType(new IEntityTypeConfiguration[] { mappedType as IEntityTypeConfiguration });
                                complexType.AddComplexProperty(property);
                            }
                        }
                    }
                    else
                    {
                        throw Error.NotSupported(SRResources.CollectionPropertiesNotSupported, property.Name, property.ReflectedType.FullName);
                    }
                }
            }
        }
        private void ReconfigureEntityTypesAsComplexType(IEnumerable <IEntityTypeConfiguration> misconfiguredEntityTypes)
        {
            IEnumerable <IEntityTypeConfiguration> actualEntityTypes = StructuralTypes
                                                                       .Except(misconfiguredEntityTypes)
                                                                       .OfType <IEntityTypeConfiguration>()
                                                                       .ToArray();

            foreach (IEntityTypeConfiguration misconfiguredEntityType in misconfiguredEntityTypes)
            {
                RemoveStructuralType(misconfiguredEntityType.ClrType);

                IComplexTypeConfiguration newComplexType = AddComplexType(misconfiguredEntityType.ClrType);
                foreach (PropertyInfo ignoredProperty in misconfiguredEntityType.IgnoredProperties)
                {
                    newComplexType.RemoveProperty(ignoredProperty);
                }

                foreach (IEntityTypeConfiguration entityToBePatched in actualEntityTypes)
                {
                    NavigationPropertyConfiguration[] propertiesToBeRemoved = entityToBePatched
                                                                              .NavigationProperties
                                                                              .Where(navigationProperty => navigationProperty.RelatedClrType == misconfiguredEntityType.ClrType)
                                                                              .ToArray();
                    foreach (NavigationPropertyConfiguration propertyToBeRemoved in propertiesToBeRemoved)
                    {
                        entityToBePatched.RemoveProperty(propertyToBeRemoved.PropertyInfo);

                        if (propertyToBeRemoved.Multiplicity == EdmMultiplicity.Many)
                        {
                            entityToBePatched.AddCollectionProperty(propertyToBeRemoved.PropertyInfo);
                        }
                        else
                        {
                            entityToBePatched.AddComplexProperty(propertyToBeRemoved.PropertyInfo);
                        }
                    }
                }
            }
        }
Example #7
0
        private void ReconfigureEntityTypesAsComplexType(IEnumerable <IEntityTypeConfiguration> misconfiguredEntityTypes)
        {
            IEnumerable <IEntityTypeConfiguration> actualEntityTypes = StructuralTypes
                                                                       .Except(misconfiguredEntityTypes)
                                                                       .OfType <IEntityTypeConfiguration>()
                                                                       .ToArray();

            foreach (IEntityTypeConfiguration misconfiguredEntityType in misconfiguredEntityTypes)
            {
                RemoveStructuralType(misconfiguredEntityType.ClrType);

                IComplexTypeConfiguration newComplexType = AddComplexType(misconfiguredEntityType.ClrType);
                foreach (var ignoredProperty in misconfiguredEntityType.IgnoredProperties)
                {
                    newComplexType.RemoveProperty(ignoredProperty);
                }

                foreach (IEntityTypeConfiguration entityToBePatched in actualEntityTypes)
                {
                    NavigationPropertyConfiguration[] propertiesToBeRemoved = entityToBePatched
                                                                              .NavigationProperties
                                                                              .Where(navigationProperty => navigationProperty.RelatedClrType == misconfiguredEntityType.ClrType)
                                                                              .ToArray();
                    foreach (NavigationPropertyConfiguration propertyToBeRemoved in propertiesToBeRemoved)
                    {
                        if (propertyToBeRemoved.Multiplicity == EdmMultiplicity.Many)
                        {
                            // complex collections are not supported.
                            throw Error.NotSupported(SRResources.CollectionPropertiesNotSupported, propertyToBeRemoved.PropertyInfo.Name, propertyToBeRemoved.PropertyInfo.ReflectedType.FullName);
                        }
                        entityToBePatched.RemoveProperty(propertyToBeRemoved.PropertyInfo);
                        entityToBePatched.AddComplexProperty(propertyToBeRemoved.PropertyInfo);
                    }
                }

                AddComplexType(misconfiguredEntityType.ClrType);
            }
        }
 internal ComplexTypeConfiguration(IComplexTypeConfiguration configuration)
     : base(configuration)
 {
 }
 private void CreateComplexTypeBody(EdmComplexType type, IComplexTypeConfiguration config)
 {
     CreateStructuralTypeBody(type, config);
 }
 private void CreateComplexTypeBody(EdmComplexType type, IComplexTypeConfiguration config)
 {
     CreateStructuralTypeBody(type, config);
 }
        private void MapComplexType(IComplexTypeConfiguration complexType)
        {
            PropertyInfo[] properties = ConventionsHelpers.GetProperties(complexType);
            foreach (PropertyInfo property in properties)
            {
                bool isCollection;
                IStructuralTypeConfiguration mappedType;

                PropertyKind propertyKind = GetPropertyType(property, out isCollection, out mappedType);

                if (propertyKind == PropertyKind.Primitive || propertyKind == PropertyKind.Complex)
                {
                    MapStructuralProperty(complexType, property, propertyKind, isCollection);
                }
                else
                {
                    // navigation property in a complex type ?
                    if (!isCollection)
                    {
                        if (mappedType == null)
                        {
                            // the user told nothing about this type and this is the first time we are seeing this type.
                            // complex types cannot contain entities. So, treat it as complex property.
                            complexType.AddComplexProperty(property);
                        }
                        else
                        {
                            if (_explicitlyAddedTypes.Contains(mappedType))
                            {
                                // user told us that this an entity type.
                                throw Error.InvalidOperation(SRResources.ComplexTypeRefersToEntityType, complexType.ClrType.FullName, mappedType.ClrType.FullName, property.Name);
                            }
                            else
                            {
                                // we tried to be over-smart earlier and made the bad choice. so patch up now.
                                ReconfigureEntityTypesAsComplexType(new IEntityTypeConfiguration[] { mappedType as IEntityTypeConfiguration });
                                complexType.AddComplexProperty(property);
                            }
                        }
                    }
                    else
                    {
                        throw Error.NotSupported(SRResources.CollectionPropertiesNotSupported, property.Name, property.ReflectedType.FullName);
                    }
                }
            }
        }