コード例 #1
0
        public void EdmModelAddComplexTypeTest()
        {
            var model        = new EdmModel();
            var complexTypeA = model.AddComplexType("NS", "ComplexA");
            var complexTypeB = model.AddComplexType("NS", "ComplexB", complexTypeA);
            var complexTypeC = model.AddComplexType("NS", "ComplexC", complexTypeB, false);
            var complexTypeD = model.AddComplexType("NS", "ComplexD", complexTypeC, false, true);

            Assert.Equal(complexTypeA, model.FindDeclaredType("NS.ComplexA"));
            Assert.Equal(complexTypeB, model.FindDeclaredType("NS.ComplexB"));
            Assert.Equal(complexTypeC, model.FindDeclaredType("NS.ComplexC"));
            Assert.Equal(complexTypeD, model.FindDeclaredType("NS.ComplexD"));
        }
コード例 #2
0
        public void GetStructuralOrEnumType_should_return_correct_type_for_ComplexType()
        {
            var model       = new EdmModel(DataSpace.CSpace);
            var complexType = model.AddComplexType("Foo");

            Assert.Same(complexType, model.GetStructuralOrEnumType("Foo"));
        }
コード例 #3
0
ファイル: ModelBuilder.cs プロジェクト: sthagen/odata-rapid
        private EdmStructuredType AddStructuredType(RdmStructuredType definition)
        {
            // base type, the Build method ensure that the base type was added before the
            // sub-type and therefore FindType will succeed.
            IEdmStructuredType edmBaseType = null;

            if (definition.BaseType != null)
            {
                edmBaseType = edmModel.FindType(rdmModel.Namespace.NamespaceName + "." + definition.BaseType) as EdmStructuredType;
                if (edmBaseType == null)
                {
                    throw new TransformationException($"unable to find base type {definition.BaseType} for type {definition.Name}");
                }
            }

            if (definition.Keys.Any() || HasSingletonOfType(definition) || (edmBaseType != null && edmBaseType.TypeKind == EdmTypeKind.Entity))
            {
                var entity = edmModel.AddEntityType(rdmModel.Namespace.NamespaceName, definition.Name, (IEdmEntityType)edmBaseType, definition.IsAbstract, true);
                foreach (var annotation in definition.Annotations)
                {
                    annotationBuilder.AddAnnotation(edmModel, entity, annotation);
                }
                return(entity);
            }
            else
            {
                var complex = edmModel.AddComplexType(rdmModel.Namespace.NamespaceName, definition.Name, (IEdmComplexType)edmBaseType, definition.IsAbstract, true);
                foreach (var annotation in definition.Annotations)
                {
                    annotationBuilder.AddAnnotation(edmModel, complex, annotation);
                }
                return(complex);
            }
        }
コード例 #4
0
        public void GetStructuralType_should_return_entity_or_complex_type()
        {
            var model       = new EdmModel(DataSpace.CSpace);
            var entityType  = model.AddEntityType("E");
            var complexType = model.AddComplexType("C");

            Assert.Same(entityType, model.GetStructuralType("E"));
            Assert.Same(complexType, model.GetStructuralType("C"));
        }
コード例 #5
0
        public void GetComplexType_should_return_correct_type()
        {
            var model       = new EdmModel(DataSpace.CSpace);
            var complexType = model.AddComplexType("Foo");

            var foundComplexType = model.GetComplexType("Foo");

            Assert.NotNull(foundComplexType);
            Assert.Same(complexType, foundComplexType);
        }
コード例 #6
0
        public void AddComplexType_should_create_and_add_to_default_namespace()
        {
            var model = new EdmModel().Initialize();

            var complexType = model.AddComplexType("Foo");

            Assert.NotNull(complexType);
            Assert.Equal("Foo", complexType.Name);
            Assert.True(model.Namespaces.Single().ComplexTypes.Contains(complexType));
        }
コード例 #7
0
        public void AddComplexType_should_create_and_add_when_custom_namespace()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var complexType = model.AddComplexType("Foo", "Bar");

            Assert.NotNull(complexType);
            Assert.Equal("Foo", complexType.Name);
            Assert.Equal("Bar", complexType.NamespaceName);
            Assert.True(model.ComplexTypes.Contains(complexType));
        }
コード例 #8
0
        public void AddComplexType_should_create_and_add_with_default_namespace()
        {
            var model = new EdmModel(DataSpace.CSpace);

            var complexType = model.AddComplexType("Foo");

            Assert.NotNull(complexType);
            Assert.Equal("Foo", complexType.Name);
            Assert.Equal(EdmModelExtensions.DefaultModelNamespace, complexType.NamespaceName);
            Assert.True(model.ComplexTypes.Contains(complexType));
        }
コード例 #9
0
        public void Apply(EdmModel model)
        {
            Check.NotNull(model, "model");

            // Query the model for candidate complex types.
            //   - The rules for complex type discovery are as follows:
            //      1) The entity does not have a key or base type.
            //      2) The entity does not have explicit configuration or has only structural type configuration.
            //      3) The entity does not have any outbound navigation properties.
            //         The entity only has inbound associations where:
            //          4) The association does not have a constraint defined.
            //          5) The association does not have explicit configuration.
            //          6) The association is not self-referencing.
            //          7) The other end of the association is Optional.
            //      8) Any inbound navigation properties do not have explicit configuration.

            var candidates
                = from entityType in model.EntityTypes
                  where entityType.KeyProperties.Count == 0 && // (1)
                  entityType.BaseType == null
                  // (1)
                  let entityTypeConfiguration = entityType.GetConfiguration() as EntityTypeConfiguration
                                                where ((entityTypeConfiguration == null) || // (2)
                                                       (!entityTypeConfiguration.IsExplicitEntity &&
                                                        entityTypeConfiguration.IsStructuralConfigurationOnly)) && // (2)
                                                !entityType.NavigationProperties.Any()
                                                // (3)
                                                let matchingAssociations
                                                    = from associationType in model.AssociationTypes
                                                      where associationType.SourceEnd.GetEntityType() == entityType ||
                                                      associationType.TargetEnd.GetEntityType() == entityType
                                                      let declaringEnd
                                                      = associationType.SourceEnd.GetEntityType() == entityType
                                  ? associationType.SourceEnd
                                  : associationType.TargetEnd
                                                        let declaringEntity
                                                        = associationType.GetOtherEnd(declaringEnd).GetEntityType()
                                                          let navigationProperties
                                                          = declaringEntity.NavigationProperties
                                                            .Where(n => n.ResultEnd.GetEntityType() == entityType)
                                                            select new
                {
                DeclaringEnd         = declaringEnd,
                AssociationType      = associationType,
                DeclaringEntityType  = declaringEntity,
                NavigationProperties = navigationProperties.ToList()
                }
            where matchingAssociations.All(
                a => a.AssociationType.Constraint == null &&    // (4)
                a.AssociationType.GetConfiguration() == null &&            // (5)
                !a.AssociationType.IsSelfReferencing() &&            // (6)
                a.DeclaringEnd.IsOptional() &&            // (7)
                a.NavigationProperties.All(n => n.GetConfiguration() == null))
            // (8)
            select new
            {
                EntityType           = entityType,
                MatchingAssociations = matchingAssociations.ToList(),
            };

            // Transform candidate entities into complex types
            foreach (var candidate in candidates.ToList())
            {
                var complexType = model.AddComplexType(candidate.EntityType.Name, candidate.EntityType.NamespaceName);

                foreach (var property in candidate.EntityType.DeclaredProperties)
                {
                    complexType.AddMember(property);
                }

                foreach (var annotation in candidate.EntityType.Annotations)
                {
                    complexType.Annotations.Add(annotation);
                }

                foreach (var association in candidate.MatchingAssociations)
                {
                    foreach (var navigationProperty in association.NavigationProperties)
                    {
                        if (association.DeclaringEntityType.NavigationProperties.Contains(navigationProperty))
                        {
                            association.DeclaringEntityType.RemoveMember(navigationProperty);

                            var complexProperty
                                = association.DeclaringEntityType
                                  .AddComplexProperty(navigationProperty.Name, complexType);

                            foreach (var annotation in navigationProperty.Annotations)
                            {
                                complexProperty.Annotations.Add(annotation);
                            }
                        }
                    }

                    model.RemoveAssociationType(association.AssociationType);
                }

                model.RemoveEntityType(candidate.EntityType);
            }
        }