Exemplo n.º 1
0
        public EdmModel BuildEdmModel()
        {
            foreach (EntityTypeInfo typeInfo in _entityTypes.Values)
            {
                typeInfo.BuildProperties(_entityTypes, _enumTypes, _complexTypes);
            }

            foreach (EntityTypeInfo typeInfo in _entityTypes.Values)
            {
                foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                {
                    fkeyInfo.EdmNavigationProperty = CreateNavigationProperty(fkeyInfo);
                }
            }

            var edmModel  = new EdmModel();
            var container = new EdmEntityContainer("Default", "Container");

            edmModel.AddElements(_enumTypes.Values);
            foreach (KeyValuePair <Type, EdmEnumType> enumType in _enumTypes)
            {
                edmModel.SetAnnotationValue <OeClrTypeAnnotation>(enumType.Value, new OeClrTypeAnnotation(enumType.Key));
            }

            edmModel.AddElements(_complexTypes.Values);
            var entitySets = new Dictionary <IEdmEntityType, EdmEntitySet>(_entityTypes.Count);

            foreach (EntityTypeInfo typeInfo in _entityTypes.Values)
            {
                edmModel.AddElement(typeInfo.EdmType);
                entitySets.Add(typeInfo.EdmType, container.AddEntitySet(typeInfo.EntitySetName, typeInfo.EdmType));
            }

            foreach (EntityTypeInfo typeInfo in _entityTypes.Values)
            {
                foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                {
                    EdmEntitySet principal = entitySets[fkeyInfo.PrincipalInfo.EdmType];
                    EdmEntitySet dependent = entitySets[fkeyInfo.DependentInfo.EdmType];
                    dependent.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, principal);

                    if (fkeyInfo.EdmNavigationProperty.Partner != null)
                    {
                        principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty.Partner, dependent);
                    }
                }
            }

            edmModel.AddElement(container);
            return(edmModel);
        }
Exemplo n.º 2
0
        public void FindAllDerivedTypesWordsAcrossModels()
        {
            EdmModel model           = new EdmModel();
            EdmModel referencedModel = new EdmModel();

            var A     = new EdmEntityType("Referenced", "A");
            var AProp = A.AddStructuralProperty("ID", EdmCoreModel.Instance.GetString(false));

            A.AddKeys(new[] { AProp });

            var B = new EdmEntityType("Referenced", "B", A);
            var C = new EdmEntityType("Referenced", "C", B);

            var D = new EdmEntityType("Referenced", "D", C);

            referencedModel.AddElements(new[] { A, B, C });
            model.AddReferencedModel(referencedModel);
            model.AddElement(D);

            IEnumerable <IEdmStructuredType> derivedTypes = model.FindAllDerivedTypes(A);

            Assert.AreEqual(3, derivedTypes.Count(), "Correct number of derived types");
            Assert.IsTrue(derivedTypes.Contains(B), "Contains B");
            Assert.IsTrue(derivedTypes.Contains(C), "Contains C");
            Assert.IsTrue(derivedTypes.Contains(D), "Contains D");
        }
Exemplo n.º 3
0
        [InlineData("Cars/NS.Vehicle/WashMultiple", "WashMultiple", "Collection([NS.Vehicle Nullable=False])")] // downcast
        public void ActionOverloadResoultionTests(string path, string actionName, string expectedEntityBound)
        {
            // Arrange
            EdmModel           model     = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            var vehicle    = new EdmEntityType("NS", "Vehicle");
            var car        = new EdmEntityType("NS", "Car", vehicle);
            var motorcycle = new EdmEntityType("NS", "Motorcycle", vehicle);

            model.AddElements(new IEdmSchemaElement[] { vehicle, car, motorcycle, container });

            var washVehicle  = AddBindableAction(container, "Wash", vehicle, isCollection: false);
            var washCar      = AddBindableAction(container, "Wash", car, isCollection: false);
            var washVehicles = AddBindableAction(container, "WashMultiple", vehicle, isCollection: true);
            var washCars     = AddBindableAction(container, "WashMultiple", car, isCollection: true);

            container.AddEntitySet("Vehicles", vehicle);
            container.AddEntitySet("Cars", car);
            container.AddEntitySet("Motorcycles", motorcycle);

            // Act
            ODataPath odataPath = _parser.Parse(model, path);

            // Assert
            Assert.NotNull(odataPath);
            ActionPathSegment actionSegment = Assert.IsType <ActionPathSegment>(odataPath.Segments.Last());

            Assert.Equal("NS.Container." + actionName, actionSegment.ActionName);
            Assert.Equal(expectedEntityBound, actionSegment.Action.Parameters.First().Type.Definition.ToTraceString());
        }
Exemplo n.º 4
0
        public static IEdmModel Build()
        {
            var addressType = new EdmComplexType(Namespace, "address");

            addressType.AddStructuralProperty("street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("city", EdmPrimitiveTypeKind.String);

            var addressTypeRef   = new EdmComplexTypeReference(addressType, isNullable: false);
            var addressesTypeRef = EdmCoreModel.GetCollection(addressTypeRef);

            var nonNullableStringTypeRef = EdmCoreModel.Instance.GetString(isNullable: false);
            var tagsTypeRef = EdmCoreModel.GetCollection(nonNullableStringTypeRef);

            var docEntityType         = new EdmEntityType(Namespace, DocEntityTypeName);
            EdmStructuralProperty key = docEntityType.AddStructuralProperty("id", EdmPrimitiveTypeKind.String);

            docEntityType.AddKeys(key);
            docEntityType.AddStructuralProperty("addresses", addressesTypeRef);
            docEntityType.AddStructuralProperty("tags", tagsTypeRef);

            var docEntitySetType = EdmCoreModel.GetCollection(new EdmEntityTypeReference(docEntityType, isNullable: true));

            var container = new EdmEntityContainer("Default", "Container");

            container.AddEntitySet("Docs", docEntityType);

            var model = new EdmModel();

            model.AddElements(new IEdmSchemaElement[] { container, addressType, docEntityType });
            return(model);
        }
Exemplo n.º 5
0
        private static IEdmModel BuildModelFromTypes(IEnumerable <IEdmSchemaType> types)
        {
            var model = new EdmModel();

            model.AddElements(types);
            return(model);
        }
Exemplo n.º 6
0
        public static IEdmModel AddDynamicModel(this EdmModel model)
        {
            var addressType = new EdmComplexType(Namespace, "address");

            addressType.AddStructuralProperty("street", EdmPrimitiveTypeKind.String);
            addressType.AddStructuralProperty("city", EdmPrimitiveTypeKind.String);

            var addressTypeRef   = new EdmComplexTypeReference(addressType, isNullable: false);
            var addressesTypeRef = EdmCoreModel.GetCollection(addressTypeRef);

            var nonNullableStringTypeRef = EdmCoreModel.Instance.GetString(isNullable: false);
            var tagsTypeRef = EdmCoreModel.GetCollection(nonNullableStringTypeRef);

            var docEntityType         = new EdmEntityType(Namespace, DocEntityTypeName);
            EdmStructuralProperty key = docEntityType.AddStructuralProperty("id", EdmPrimitiveTypeKind.String);

            docEntityType.AddKeys(key);
            docEntityType.AddStructuralProperty("addresses", addressesTypeRef);
            docEntityType.AddStructuralProperty("tags", tagsTypeRef);

            var docEntitySetType = EdmCoreModel.GetCollection(new EdmEntityTypeReference(docEntityType, isNullable: true));

            var docComplexType = new EdmComplexType(Namespace, DocComplexTypeName);

            foreach (IEdmStructuralProperty property in docEntityType.StructuralProperties())
            {
                docComplexType.AddStructuralProperty(property.Name, property.Type);
            }

            var valueParameterTypeRef =
                EdmCoreModel.GetCollection(new EdmComplexTypeReference(docComplexType, isNullable: false));

            var container = (EdmEntityContainer)model.EntityContainer;

            container.AddEntitySet("Docs", docEntityType);

            var indexAction =
                new EdmAction(
                    Namespace,
                    "Index",
                    returnType: null,
                    isBound: true,
                    entitySetPathExpression: null);

            indexAction.AddParameter("bindingParameter", docEntitySetType);
            indexAction.AddParameter("value", valueParameterTypeRef);

            var indexActionImport = new EdmActionImport(container, indexAction.Name, indexAction);

            model.AddElements(new IEdmSchemaElement[] { addressType, docEntityType, docComplexType, indexActionImport.Operation });
            return(model);
        }
Exemplo n.º 7
0
        public static IEdmModel MultipleSchemasWithDifferentNamespacesEdm()
        {
            var namespaces = new string[]
            {
                "FindMethodsTestModelBuilder.MultipleSchemasWithDifferentNamespaces.first",
                "FindMethodsTestModelBuilder.MultipleSchemasWithDifferentNamespaces.second"
            };

            var model = new EdmModel();

            foreach (var namespaceName in namespaces)
            {
                var entityType1 = new EdmEntityType(namespaceName, "validEntityType1");
                entityType1.AddKeys(entityType1.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
                var entityType2 = new EdmEntityType(namespaceName, "VALIDeNTITYtYPE2");
                entityType2.AddKeys(entityType2.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
                var entityType3 = new EdmEntityType(namespaceName, "VALIDeNTITYtYPE3");
                entityType3.AddKeys(entityType3.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));

                entityType1.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                    Name = "Mumble", Target = entityType2, TargetMultiplicity = EdmMultiplicity.Many
                });

                var complexType = new EdmComplexType(namespaceName, "ValidNameComplexType1");
                complexType.AddStructuralProperty("aPropertyOne", new EdmComplexTypeReference(complexType, false));
                model.AddElements(new IEdmSchemaElement[] { entityType1, entityType2, entityType3, complexType });

                var function1 = new EdmFunction(namespaceName, "ValidFunction1", EdmCoreModel.Instance.GetSingle(false));
                var function2 = new EdmFunction(namespaceName, "ValidFunction1", EdmCoreModel.Instance.GetSingle(false));
                function2.AddParameter("param1", new EdmEntityTypeReference(entityType1, false));
                var function3 = new EdmFunction(namespaceName, "ValidFunction1", EdmCoreModel.Instance.GetSingle(false));
                function3.AddParameter("param1", EdmCoreModel.Instance.GetSingle(false));

                model.AddElements(new IEdmSchemaElement[] { function1, function2, function3 });
            }

            return(model);
        }
        public ODataReaderDerivedTypeConstraintTests()
        {
            // Create the basic model
            edmModel = new EdmModel();

            // Entity Type
            edmCustomerType = new EdmEntityType("NS", "Customer");
            edmCustomerType.AddKeys(edmCustomerType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));

            edmVipCustomerType = new EdmEntityType("NS", "VipCustomer", edmCustomerType);
            edmVipCustomerType.AddStructuralProperty("Vip", EdmPrimitiveTypeKind.String);

            edmNormalCustomerType = new EdmEntityType("NS", "NormalCustomer", edmCustomerType);
            edmNormalCustomerType.AddStructuralProperty("Email", EdmPrimitiveTypeKind.String);

            edmModel.AddElements(new[] { edmCustomerType, edmVipCustomerType, edmNormalCustomerType });

            // Complex type
            edmAddressType = new EdmComplexType("NS", "Address");
            edmAddressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);

            edmUsAddressType = new EdmComplexType("NS", "UsAddress", edmAddressType);
            edmUsAddressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.Int32);

            edmCnAddressType = new EdmComplexType("NS", "CnAddress", edmAddressType);
            edmCnAddressType.AddStructuralProperty("PostCode", EdmPrimitiveTypeKind.String);

            edmModel.AddElements(new[] { edmAddressType, edmUsAddressType, edmCnAddressType });

            // EntityContainer
            EdmEntityContainer container = new EdmEntityContainer("NS", "Default");

            edmMe        = container.AddSingleton("Me", edmCustomerType);
            edmCustomers = container.AddEntitySet("Customers", edmCustomerType);
            edmModel.AddElement(container);
        }
Exemplo n.º 9
0
        public void UnqualifiedTypeNameShouldNotBeTreatedAsTypeCast()
        {
            var model      = new EdmModel();
            var entityType = new EdmEntityType("NS", "Entity");

            entityType.AddKeys(entityType.AddStructuralProperty("IdStr", EdmPrimitiveTypeKind.String, false));
            var container = new EdmEntityContainer("NS", "Container");
            var set       = container.AddEntitySet("Set", entityType);

            model.AddElements(new IEdmSchemaElement[] { entityType, container });

            var svcRoot     = new Uri("http://host", UriKind.Absolute);
            var parseResult = new ODataUriParser(model, svcRoot, new Uri("http://host/Set/Date", UriKind.Absolute)).ParseUri();

            Assert.True(parseResult.Path.LastSegment is KeySegment);
        }
        private static IEdmModel GetEdmModel()
        {
            EdmModel model = new EdmModel();

            // ComplexType: Address
            EdmComplexType address = new EdmComplexType("NS", "Address");

            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(address);
            model.SetAnnotationValue(address, new ClrTypeAnnotation(typeof(MyAddress)));

            // ComplexType: CnAddress
            var cnAddress = new EdmComplexType("NS", "CnAddress", address);

            cnAddress.AddStructuralProperty("Zipcode", EdmPrimitiveTypeKind.String);
            model.AddElement(cnAddress);
            model.SetAnnotationValue(cnAddress, new ClrTypeAnnotation(typeof(CnMyAddress)));

            // EnumType: Color
            var color = new EdmEnumType("NS", "Color");

            model.AddElement(color);
            model.SetAnnotationValue(color, new ClrTypeAnnotation(typeof(MyColor)));

            // EntityType: MyCustomer
            var customer = new EdmEntityType("Microsoft.AspNetCore.OData.Tests.Edm", "MyCustomer");

            model.AddElement(customer);

            // Inheritance EntityType
            var baseEntity       = new EdmEntityType("NS", "BaseType");
            var derived1Entity   = new EdmEntityType("NS", "Derived1Type", baseEntity);
            var derived2Entity   = new EdmEntityType("NS", "Derived2Type", baseEntity);
            var subDerivedEntity = new EdmEntityType("NS", "SubDerivedType", derived1Entity);

            model.AddElements(new[] { baseEntity, derived1Entity, derived2Entity, subDerivedEntity });
            model.SetAnnotationValue(baseEntity, new ClrTypeAnnotation(typeof(BaseType)));
            model.SetAnnotationValue(derived1Entity, new ClrTypeAnnotation(typeof(Derived1Type)));
            model.SetAnnotationValue(derived2Entity, new ClrTypeAnnotation(typeof(Derived2Type)));
            model.SetAnnotationValue(subDerivedEntity, new ClrTypeAnnotation(typeof(SubDerivedType)));

            return(model);
        }
Exemplo n.º 11
0
        public void Invalid_CastTests(string path)
        {
            // Arrange
            EdmModel           model     = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            var vehicle    = new EdmEntityType("NS", "Vehicle");
            var car        = new EdmEntityType("NS", "Car", vehicle);
            var motorcycle = new EdmEntityType("NS", "Motorcycle", vehicle);

            model.AddElements(new IEdmSchemaElement[] { vehicle, car, motorcycle, container });

            container.AddEntitySet("Vehicles", vehicle);
            container.AddEntitySet("Cars", car);
            container.AddEntitySet("Motorcycles", motorcycle);

            // Act
            var exception = Assert.Throws <ODataException>(() => _parser.Parse(model, path));

            Assert.Contains("Invalid cast encountered.", exception.Message);
        }
Exemplo n.º 12
0
        public void CastTests(string path, string expectedEdmType)
        {
            // Arrange
            EdmModel           model     = new EdmModel();
            EdmEntityContainer container = new EdmEntityContainer("NS", "Container");
            var vehicle    = new EdmEntityType("NS", "Vehicle");
            var car        = new EdmEntityType("NS", "Car", vehicle);
            var motorcycle = new EdmEntityType("NS", "Motorcycle", vehicle);

            model.AddElements(new IEdmSchemaElement[] { vehicle, car, motorcycle, container });

            container.AddEntitySet("Vehicles", vehicle);
            container.AddEntitySet("Cars", car);
            container.AddEntitySet("Motorcycles", motorcycle);

            // Act
            ODataPath odataPath = _parser.Parse(model, path);

            // Assert
            Assert.NotNull(odataPath);
            Assert.Equal(expectedEdmType, odataPath.EdmType.ToTraceString());
        }
Exemplo n.º 13
0
        public EdmModel BuildEdmModel(params IEdmModel[] refModels)
        {
            AddOperations();

            Dictionary <Type, EntityTypeInfo> entityTypeInfos = BuildEntityTypes(refModels);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                if (!typeInfo.IsRefModel)
                {
                    typeInfo.BuildProperties(entityTypeInfos, _enumTypes, _complexTypes);
                }
            }

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                if (!typeInfo.IsRefModel)
                {
                    foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                    {
                        fkeyInfo.BuildNavigationProperty();
                    }
                }
            }

            var edmModel = new EdmModel(false);

            edmModel.AddElements(_enumTypes.Values);
            foreach (KeyValuePair <Type, EdmEnumType> enumType in _enumTypes)
            {
                edmModel.SetClrType(enumType.Value, enumType.Key);
            }

            edmModel.AddElements(_complexTypes.Values);
            foreach (KeyValuePair <Type, EdmComplexType> complexType in _complexTypes)
            {
                edmModel.SetClrType(complexType.Value, complexType.Key);
            }

            var container = new EdmEntityContainer(_dataAdapter.DataContextType.Namespace, _dataAdapter.DataContextType.Name);

            edmModel.SetDataAdapter(container, _dataAdapter);

            var entitySets = new Dictionary <IEdmEntityType, EdmEntitySet>(entityTypeInfos.Count);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                if (!typeInfo.IsRefModel)
                {
                    edmModel.AddElement(typeInfo.EdmType);
                    edmModel.SetClrType(typeInfo.EdmType, typeInfo.ClrType);
                }

                Db.OeEntitySetAdapter entitySetAdapter = _dataAdapter.EntitySetAdapters.Find(typeInfo.ClrType);
                if (entitySetAdapter != null)
                {
                    EdmEntitySet entitySet = container.AddEntitySet(entitySetAdapter.EntitySetName, typeInfo.EdmType);
                    edmModel.SetEntitySetAdapter(entitySet, entitySetAdapter);
                    entitySets.Add(typeInfo.EdmType, entitySet);
                }
            }

            var manyToManyBuilder = new ManyToManyBuilder(edmModel, _metadataProvider, entityTypeInfos);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                if (!typeInfo.IsRefModel)
                {
                    foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                    {
                        EdmEntitySet principal = entitySets[fkeyInfo.PrincipalInfo.EdmType];
                        EdmEntitySet dependent = entitySets[fkeyInfo.DependentInfo.EdmType];

                        if (fkeyInfo.DependentNavigationProperty == null)
                        {
                            principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, dependent);
                        }
                        else
                        {
                            dependent.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, principal);
                            if (fkeyInfo.EdmNavigationProperty.Partner != null)
                            {
                                principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty.Partner, dependent);
                            }
                        }
                    }

                    manyToManyBuilder.Build(typeInfo);
                }
            }

            foreach (OeOperationConfiguration operationConfiguration in _operationConfigurations)
            {
                if (operationConfiguration.IsEdmFunction)
                {
                    EdmFunction edmFunction = BuildFunction(operationConfiguration, entityTypeInfos);
                    edmModel.AddElement(edmFunction);

                    if (edmFunction.IsBound)
                    {
                        edmModel.SetMethodInfo(edmFunction, operationConfiguration.MethodInfo);
                    }
                    else
                    {
                        container.AddFunctionImport(operationConfiguration.ImportName, edmFunction, edmFunction.EntitySetPath);
                        edmModel.SetIsDbFunction(edmFunction, operationConfiguration.IsDbFunction);
                    }
                }
                else
                {
                    EdmAction edmAction = BuildAction(operationConfiguration, entityTypeInfos);
                    edmModel.AddElement(edmAction);
                    container.AddActionImport(operationConfiguration.ImportName, edmAction);
                    edmModel.SetIsDbFunction(edmAction, operationConfiguration.IsDbFunction);
                }
            }

            edmModel.AddElement(container);
            _dataAdapter.SetEdmModel(edmModel);
            foreach (IEdmModel refModel in refModels)
            {
                edmModel.AddReferencedModel(refModel);
            }

            return(edmModel);
        }
Exemplo n.º 14
0
        public void ShouldWriteDynamicNullableCollectionValuedProperty()
        {
            // setup model
            var model      = new EdmModel();
            var entityType = new EdmEntityType("NS", "EntityType", null, false, true);

            entityType.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);
            var container   = new EdmEntityContainer("NS", "Container");
            var entitySet   = container.AddEntitySet("EntitySet", entityType);
            var complexType = new EdmComplexType("NS", "ComplexType");

            complexType.AddStructuralProperty("Prop1", EdmPrimitiveTypeKind.Int32);
            complexType.AddStructuralProperty("Prop2", EdmPrimitiveTypeKind.Int32);
            model.AddElements(new IEdmSchemaElement[] { entityType, complexType, container });

            // setup writer
            var stream  = new MemoryStream();
            var message = new InMemoryMessage {
                Stream = stream
            };
            var settings = new ODataMessageWriterSettings
            {
                ODataUri = new ODataUri
                {
                    ServiceRoot = new Uri("http://svc/")
                }
            };
            var writer = new ODataMessageWriter((IODataResponseMessage)message, settings, model)
                         .CreateODataResourceWriter(entitySet, entityType);

            // write payload
            writer.Write(new ODataResource
            {
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "ID", Value = 1
                    },
                    new ODataProperty
                    {
                        Name  = "DynamicPrimitive",
                        Value = new ODataCollectionValue
                        {
                            TypeName = "Collection(Edm.Int64)",
                            Items    = new object[] { 1L, 2L, null }
                        }
                    }
                }
            }, () => writer
                         .Write(new ODataNestedResourceInfo
            {
                Name         = "DynamicComplex",
                IsCollection = true,
            }, () => writer
                                .Write(new ODataResourceSet
            {
                TypeName = "Collection(NS.ComplexType)"
            }, () => writer
                                       .Write((ODataResource)null)
                                       .Write((ODataResource)null)
                                       .Write(new ODataResource
            {
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "Prop1", Value = 1
                    },
                    new ODataProperty {
                        Name = "Prop2", Value = 2
                    }
                }
            }))));
            var str = Encoding.UTF8.GetString(stream.ToArray());

            Assert.Equal(str,
                         "{" +
                         "\"@odata.context\":\"http://svc/$metadata#EntitySet/$entity\"," +
                         "\"ID\":1," +
                         "\"[email protected]\":\"#Collection(Int64)\"," +
                         "\"DynamicPrimitive\":[1,2,null]," +
                         "\"[email protected]\":\"#Collection(NS.ComplexType)\"," +
                         "\"DynamicComplex\":[null,null,{\"Prop1\":1,\"Prop2\":2}]" +
                         "}");
        }
Exemplo n.º 15
0
        public void ValidateWriteMethodGroup()
        {
            // setup model
            var model       = new EdmModel();
            var complexType = new EdmComplexType("NS", "ComplexType");

            complexType.AddStructuralProperty("PrimitiveProperty1", EdmPrimitiveTypeKind.Int64);
            complexType.AddStructuralProperty("PrimitiveProperty2", EdmPrimitiveTypeKind.Int64);
            var entityType = new EdmEntityType("NS", "EntityType", null, false, true);

            entityType.AddKeys(
                entityType.AddStructuralProperty("PrimitiveProperty", EdmPrimitiveTypeKind.Int64));
            var container = new EdmEntityContainer("NS", "Container");
            var entitySet = container.AddEntitySet("EntitySet", entityType);

            model.AddElements(new IEdmSchemaElement[] { complexType, entityType, container });

            // write payload using new API
            string str1;
            {
                // setup
                var stream  = new MemoryStream();
                var message = new InMemoryMessage {
                    Stream = stream
                };
                message.SetHeader("Content-Type", "application/json;odata.metadata=full");
                var settings = new ODataMessageWriterSettings
                {
                    ODataUri = new ODataUri
                    {
                        ServiceRoot = new Uri("http://svc/")
                    },
                };
                var writer = new ODataMessageWriter((IODataResponseMessage)message, settings, model);

                var entitySetWriter = writer.CreateODataResourceSetWriter(entitySet);
                entitySetWriter.Write(new ODataResourceSet(), () => entitySetWriter
                                      .Write(new ODataResource
                {
                    Properties = new[] { new ODataProperty {
                                             Name = "PrimitiveProperty", Value = 1L
                                         } }
                })
                                      .Write(new ODataResource
                {
                    Properties = new[] { new ODataProperty {
                                             Name = "PrimitiveProperty", Value = 2L
                                         } }
                }, () => entitySetWriter
                                             .Write(new ODataNestedResourceInfo {
                    Name = "DynamicNavProperty"
                })
                                             .Write(new ODataNestedResourceInfo
                {
                    Name         = "DynamicCollectionProperty",
                    IsCollection = true
                }, () => entitySetWriter
                                                    .Write(new ODataResourceSet {
                    TypeName = "Collection(NS.ComplexType)"
                })))
                                      );
                str1 = Encoding.UTF8.GetString(stream.ToArray());
            }
            // write payload using old API
            string str2;

            {
                // setup
                var stream  = new MemoryStream();
                var message = new InMemoryMessage {
                    Stream = stream
                };
                message.SetHeader("Content-Type", "application/json;odata.metadata=full");
                var settings = new ODataMessageWriterSettings
                {
                    ODataUri = new ODataUri
                    {
                        ServiceRoot = new Uri("http://svc/")
                    },
                };
                var writer = new ODataMessageWriter((IODataResponseMessage)message, settings, model);

                var entitySetWriter = writer.CreateODataResourceSetWriter(entitySet);
                entitySetWriter.WriteStart(new ODataResourceSet());
                entitySetWriter.WriteStart(new ODataResource
                {
                    Properties = new[] { new ODataProperty {
                                             Name = "PrimitiveProperty", Value = 1L
                                         } }
                });
                entitySetWriter.WriteEnd();
                entitySetWriter.WriteStart(new ODataResource
                {
                    Properties = new[] { new ODataProperty {
                                             Name = "PrimitiveProperty", Value = 2L
                                         } }
                });
                entitySetWriter.WriteStart(new ODataNestedResourceInfo {
                    Name = "DynamicNavProperty"
                });
                entitySetWriter.WriteEnd();
                entitySetWriter.WriteStart(new ODataNestedResourceInfo
                {
                    Name         = "DynamicCollectionProperty",
                    IsCollection = true
                });
                entitySetWriter.WriteStart(new ODataResourceSet {
                    TypeName = "Collection(NS.ComplexType)"
                });
                entitySetWriter.WriteEnd();
                entitySetWriter.WriteEnd();
                entitySetWriter.WriteEnd();
                entitySetWriter.WriteEnd();
                str2 = Encoding.UTF8.GetString(stream.ToArray());
            }
            Assert.Equal(str1, str2);
        }
Exemplo n.º 16
0
        public void SetNavigationPropertyPartnerTest()
        {
            // build model
            var model        = new EdmModel();
            var entityType1  = new EdmEntityType("NS", "EntityType1");
            var entityType2  = new EdmEntityType("NS", "EntityType2");
            var entityType3  = new EdmEntityType("NS", "EntityType3", entityType2);
            var complexType1 = new EdmComplexType("NS", "ComplexType1");
            var complexType2 = new EdmComplexType("NS", "ComplexType2");

            model.AddElements(new IEdmSchemaElement[] { entityType1, entityType2, entityType3, complexType1, complexType2 });
            entityType1.AddKeys(entityType1.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            entityType2.AddKeys(entityType2.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            var outerNav1A = entityType1.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "OuterNavA",
                Target             = entityType2,
                TargetMultiplicity = EdmMultiplicity.One
            });
            var outerNav2A = entityType2.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "OuterNavA",
                Target             = entityType1,
                TargetMultiplicity = EdmMultiplicity.One
            });

            entityType1.SetNavigationPropertyPartner(
                outerNav1A, new EdmPathExpression("OuterNavA"), outerNav2A, new EdmPathExpression("OuterNavA"));
            entityType1.AddStructuralProperty(
                "ComplexProp",
                new EdmCollectionTypeReference(
                    new EdmCollectionType(
                        new EdmComplexTypeReference(complexType1, false))));
            entityType2.AddStructuralProperty("ComplexProp", new EdmComplexTypeReference(complexType2, false));
            var innerNav1 = complexType1.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "InnerNav",
                Target             = entityType2,
                TargetMultiplicity = EdmMultiplicity.One
            });
            var innerNav2 = complexType2.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "InnerNav",
                Target             = entityType1,
                TargetMultiplicity = EdmMultiplicity.One
            });
            var outerNav2B = entityType2.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "OuterNavB",
                Target             = entityType1,
                TargetMultiplicity = EdmMultiplicity.One
            });

            entityType2.SetNavigationPropertyPartner(
                outerNav2B, new EdmPathExpression("OuterNavB"), innerNav1, new EdmPathExpression("ComplexProp/InnerNav"));
            var outerNav2C = entityType3.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "OuterNavC",
                Target             = entityType1,
                TargetMultiplicity = EdmMultiplicity.Many
            });
            var outerNav1B = entityType1.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "OuterNavB",
                Target             = entityType2,
                TargetMultiplicity = EdmMultiplicity.Many
            });

            entityType1.SetNavigationPropertyPartner(
                outerNav1B, new EdmPathExpression("OuterNavB"), outerNav2C, new EdmPathExpression("NS.EntityType3/OuterNavC"));
            var str = GetCsdl(model, CsdlTarget.OData);

            Assert.Equal(
                "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
                "<edmx:Edmx Version=\"4.0\" xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\">" +
                "<edmx:DataServices>" +
                "<Schema Namespace=\"NS\" xmlns=\"http://docs.oasis-open.org/odata/ns/edm\">" +
                "<EntityType Name=\"EntityType1\">" +
                "<Key><PropertyRef Name=\"ID\" /></Key>" +
                "<Property Name=\"ID\" Type=\"Edm.Int32\" />" +
                "<Property Name=\"ComplexProp\" Type=\"Collection(NS.ComplexType1)\" Nullable=\"false\" />" +
                "<NavigationProperty Name=\"OuterNavA\" Type=\"NS.EntityType2\" Nullable=\"false\" Partner=\"OuterNavA\" />" +
                "<NavigationProperty Name=\"OuterNavB\" Type=\"Collection(NS.EntityType2)\" Partner=\"NS.EntityType3/OuterNavC\" />" +
                "</EntityType>" +
                "<EntityType Name=\"EntityType2\">" +
                "<Key><PropertyRef Name=\"ID\" /></Key>" +
                "<Property Name=\"ID\" Type=\"Edm.Int32\" />" +
                "<Property Name=\"ComplexProp\" Type=\"NS.ComplexType2\" Nullable=\"false\" />" +
                "<NavigationProperty Name=\"OuterNavA\" Type=\"NS.EntityType1\" Nullable=\"false\" Partner=\"OuterNavA\" />" +
                "<NavigationProperty Name=\"OuterNavB\" Type=\"NS.EntityType1\" Nullable=\"false\" Partner=\"ComplexProp/InnerNav\" />" +
                "</EntityType>" +
                "<EntityType Name=\"EntityType3\" BaseType=\"NS.EntityType2\">" +
                "<NavigationProperty Name=\"OuterNavC\" Type=\"Collection(NS.EntityType1)\" Partner=\"OuterNavB\" />" +
                "</EntityType>" +
                "<ComplexType Name=\"ComplexType1\">" +
                "<NavigationProperty Name=\"InnerNav\" Type=\"NS.EntityType2\" Nullable=\"false\" />" +
                "</ComplexType>" +
                "<ComplexType Name=\"ComplexType2\">" +
                "<NavigationProperty Name=\"InnerNav\" Type=\"NS.EntityType1\" Nullable=\"false\" />" +
                "</ComplexType>" +
                "</Schema>" +
                "</edmx:DataServices>" +
                "</edmx:Edmx>",
                str);
        }
        public ODataWriterDerivedTypeConstraintTests()
        {
            // Create the basic model
            edmModel = new EdmModel();

            // Entity Type
            edmCustomerType = new EdmEntityType("NS", "Customer");
            edmCustomerType.AddKeys(edmCustomerType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));

            edmVipCustomerType = new EdmEntityType("NS", "VipCustomer", edmCustomerType);
            edmVipCustomerType.AddStructuralProperty("Vip", EdmPrimitiveTypeKind.String);

            edmNormalCustomerType = new EdmEntityType("NS", "NormalCustomer", edmCustomerType);
            edmNormalCustomerType.AddStructuralProperty("Email", EdmPrimitiveTypeKind.String);

            edmModel.AddElements(new[] { edmCustomerType, edmVipCustomerType, edmNormalCustomerType });

            // Complex type
            edmAddressType = new EdmComplexType("NS", "Address");
            edmAddressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);

            edmUsAddressType = new EdmComplexType("NS", "UsAddress", edmAddressType);
            edmUsAddressType.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.Int32);

            edmCnAddressType = new EdmComplexType("NS", "CnAddress", edmAddressType);
            edmCnAddressType.AddStructuralProperty("PostCode", EdmPrimitiveTypeKind.String);

            edmModel.AddElements(new[] { edmAddressType, edmUsAddressType, edmCnAddressType });

            // EntityContainer
            EdmEntityContainer container = new EdmEntityContainer("NS", "Default");

            edmMe        = container.AddSingleton("Me", edmCustomerType);
            edmCustomers = container.AddEntitySet("Customers", edmCustomerType);
            edmModel.AddElement(container);

            // resource
            odataCustomerResource = new ODataResource
            {
                TypeName   = "NS.Customer",
                Properties = new[] { new ODataProperty {
                                         Name = "Id", Value = 7
                                     } }
            };

            odataVipResource = new ODataResource
            {
                TypeName   = "NS.VipCustomer",
                Properties = new[] { new ODataProperty {
                                         Name = "Id", Value = 8
                                     }, new ODataProperty {
                                         Name = "Vip", Value = "Boss"
                                     } }
            };

            odataNormalResource = new ODataResource
            {
                TypeName   = "NS.NormalCustomer",
                Properties = new[] { new ODataProperty {
                                         Name = "Id", Value = 9
                                     }, new ODataProperty {
                                         Name = "Email", Value = "*****@*****.**"
                                     } }
            };

            odataAddressResource = new ODataResource
            {
                TypeName   = "NS.Address",
                Properties = new[] { new ODataProperty {
                                         Name = "Street", Value = "Way"
                                     } }
            };

            odataUsAddressResource = new ODataResource
            {
                TypeName   = "NS.UsAddress",
                Properties = new[] { new ODataProperty {
                                         Name = "Street", Value = "RedWay"
                                     }, new ODataProperty {
                                         Name = "ZipCode", Value = 98052
                                     } }
            };

            odataCnAddressResource = new ODataResource
            {
                TypeName   = "NS.CnAddress",
                Properties = new[] { new ODataProperty {
                                         Name = "Street", Value = "ShaWay"
                                     }, new ODataProperty {
                                         Name = "PostCode", Value = "201100"
                                     } }
            };
        }
Exemplo n.º 18
0
        public void SetNavigationPropertyPartnerTypeHierarchyTest()
        {
            var model        = new EdmModel();
            var entityTypeA1 = new EdmEntityType("NS", "EntityTypeA1");
            var entityTypeA2 = new EdmEntityType("NS", "EntityTypeA2", entityTypeA1);
            var entityTypeA3 = new EdmEntityType("NS", "EntityTypeA3", entityTypeA2);
            var entityTypeB  = new EdmEntityType("NS", "EntityTypeB");

            model.AddElements(new IEdmSchemaElement[] { entityTypeA1, entityTypeA2, entityTypeA3, entityTypeB });
            entityTypeA1.AddKeys(entityTypeA1.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            var a1Nav = entityTypeA1.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "A1Nav",
                Target             = entityTypeB,
                TargetMultiplicity = EdmMultiplicity.One
            });
            var a3Nav = entityTypeA3.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "A3Nav",
                Target             = entityTypeB,
                TargetMultiplicity = EdmMultiplicity.One
            });

            entityTypeB.AddKeys(entityTypeB.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            var bNav1 = entityTypeB.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "BNav1",
                Target             = entityTypeA2,
                TargetMultiplicity = EdmMultiplicity.One
            });
            var bNav2 = entityTypeB.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "BNav2",
                Target             = entityTypeA3,
                TargetMultiplicity = EdmMultiplicity.One
            });

            entityTypeA2.SetNavigationPropertyPartner(a1Nav, new EdmPathExpression("A1Nav"), bNav1, new EdmPathExpression("BNav1"));
            entityTypeA2.SetNavigationPropertyPartner(a3Nav, new EdmPathExpression("NS.EntityTypeA3/A3Nav"), bNav2, new EdmPathExpression("BNav2"));
            var str = GetCsdl(model, CsdlTarget.OData);

            Assert.Equal(
                "<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
                "<edmx:Edmx Version=\"4.0\" xmlns:edmx=\"http://docs.oasis-open.org/odata/ns/edmx\">" +
                "<edmx:DataServices>" +
                "<Schema Namespace=\"NS\" xmlns=\"http://docs.oasis-open.org/odata/ns/edm\">" +
                "<EntityType Name=\"EntityTypeA1\">" +
                "<Key><PropertyRef Name=\"ID\" /></Key>" +
                "<Property Name=\"ID\" Type=\"Edm.Int32\" />" +
                "<NavigationProperty Name=\"A1Nav\" Type=\"NS.EntityTypeB\" Nullable=\"false\" Partner=\"BNav1\" />" +
                "</EntityType>" +
                "<EntityType Name=\"EntityTypeA2\" BaseType=\"NS.EntityTypeA1\" />" +
                "<EntityType Name=\"EntityTypeA3\" BaseType=\"NS.EntityTypeA2\">" +
                "<NavigationProperty Name=\"A3Nav\" Type=\"NS.EntityTypeB\" Nullable=\"false\" Partner=\"BNav2\" />" +
                "</EntityType>" +
                "<EntityType Name=\"EntityTypeB\">" +
                "<Key><PropertyRef Name=\"ID\" /></Key>" +
                "<Property Name=\"ID\" Type=\"Edm.Int32\" />" +
                "<NavigationProperty Name=\"BNav1\" Type=\"NS.EntityTypeA2\" Nullable=\"false\" Partner=\"A1Nav\" />" +
                "<NavigationProperty Name=\"BNav2\" Type=\"NS.EntityTypeA3\" Nullable=\"false\" Partner=\"NS.EntityTypeA3/A3Nav\" />" +
                "</EntityType>" +
                "</Schema>" +
                "</edmx:DataServices>" +
                "</edmx:Edmx>",
                str);
        }
Exemplo n.º 19
0
        public EdmModel BuildEdmModel()
        {
            Dictionary <Type, EntityTypeInfo> entityTypeInfos = BuildEntityTypes();

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                typeInfo.BuildProperties(entityTypeInfos, _enumTypes, _complexTypes);
            }

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                {
                    fkeyInfo.EdmNavigationProperty = CreateNavigationProperty(fkeyInfo);
                }
            }

            var edmModel  = new EdmModel();
            var container = new EdmEntityContainer("Default", "Container");

            edmModel.AddElements(_enumTypes.Values);
            foreach (KeyValuePair <Type, EdmEnumType> enumType in _enumTypes)
            {
                edmModel.SetClrType(enumType.Value, enumType.Key);
            }

            edmModel.AddElements(_complexTypes.Values);
            foreach (KeyValuePair <Type, EdmComplexType> complexType in _complexTypes)
            {
                edmModel.SetClrType(complexType.Value, complexType.Key);
            }

            var entitySets = new Dictionary <IEdmEntityType, EdmEntitySet>(entityTypeInfos.Count);

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                edmModel.AddElement(typeInfo.EdmType);
                edmModel.SetClrType(typeInfo.EdmType, typeInfo.ClrType);

                foreach (KeyValuePair <String, Type> pair in _entitySets)
                {
                    if (pair.Value == typeInfo.ClrType)
                    {
                        entitySets.Add(typeInfo.EdmType, container.AddEntitySet(pair.Key, typeInfo.EdmType));
                        break;
                    }
                }
            }

            foreach (EntityTypeInfo typeInfo in entityTypeInfos.Values)
            {
                foreach (FKeyInfo fkeyInfo in typeInfo.NavigationClrProperties)
                {
                    EdmEntitySet principal = entitySets[fkeyInfo.PrincipalInfo.EdmType];
                    EdmEntitySet dependent = entitySets[fkeyInfo.DependentInfo.EdmType];
                    dependent.AddNavigationTarget(fkeyInfo.EdmNavigationProperty, principal);

                    if (fkeyInfo.EdmNavigationProperty.Partner != null)
                    {
                        principal.AddNavigationTarget(fkeyInfo.EdmNavigationProperty.Partner, dependent);
                    }
                }
            }


            foreach (OeOperationConfiguration operationConfiguration in _operationConfigurations)
            {
                if (operationConfiguration.IsEdmFunction)
                {
                    EdmFunction edmFunction = BuildFunction(operationConfiguration, entityTypeInfos);
                    if (edmFunction != null)
                    {
                        edmModel.AddElement(edmFunction);
                        container.AddFunctionImport(operationConfiguration.Name, edmFunction);
                        edmModel.SetIsDbFunction(edmFunction, operationConfiguration.IsDbFunction);
                    }
                }
                else
                {
                    EdmAction edmAction = BuildAction(operationConfiguration, entityTypeInfos);
                    if (edmAction != null)
                    {
                        edmModel.AddElement(edmAction);
                        container.AddActionImport(operationConfiguration.Name, edmAction);
                        edmModel.SetIsDbFunction(edmAction, operationConfiguration.IsDbFunction);
                    }
                }
            }

            edmModel.AddElement(container);
            return(edmModel);
        }