public ODataJsonLightCollectionWriterTests()
        {
            collectionStartWithoutSerializationInfo = new ODataCollectionStart();

            collectionStartWithSerializationInfo = new ODataCollectionStart();
            collectionStartWithSerializationInfo.SetSerializationInfo(new ODataCollectionStartSerializationInfo {
                CollectionTypeName = "Collection(ns.Address)"
            });

            address = new ODataComplexValue {
                Properties = new[] { new ODataProperty {
                                         Name = "Street", Value = "1 Microsoft Way"
                                     }, new ODataProperty {
                                         Name = "Zipcode", Value = 98052
                                     }, new ODataProperty {
                                         Name = "State", Value = new ODataEnumValue("WA", "ns.StateEnum")
                                     } }
            };
            items = new[] { address };

            EdmComplexType addressType = new EdmComplexType("ns", "Address");

            addressType.AddProperty(new EdmStructuralProperty(addressType, "Street", EdmCoreModel.Instance.GetString(isNullable: true)));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "Zipcode", EdmCoreModel.Instance.GetInt32(isNullable: true)));
            var stateEnumType = new EdmEnumType("ns", "StateEnum", isFlags: true);

            stateEnumType.AddMember("IL", new EdmIntegerConstant(1));
            stateEnumType.AddMember("WA", new EdmIntegerConstant(2));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "State", new EdmEnumTypeReference(stateEnumType, true)));

            addressTypeReference = new EdmComplexTypeReference(addressType, isNullable: false);
        }
        public void SerializeTopLevelPropertyOfComplexTypeShouldWork()
        {
            EdmModel model = new EdmModel();

            EdmComplexType complexType = new EdmComplexType("ns", "complex");

            complexType.AddProperty(new EdmStructuralProperty(complexType, "propertyName1", EdmCoreModel.Instance.GetInt32(isNullable: false)));
            complexType.AddProperty(new EdmStructuralProperty(complexType, "propertyName2", EdmCoreModel.Instance.GetString(isNullable: false)));
            model.AddElement(complexType);

            EdmComplexTypeReference complexReference = new EdmComplexTypeReference(complexType, isNullable: false);

            EdmEntityType entityType = new EdmEntityType("ns", "entityType", baseType: null, isAbstract: false, isOpen: false);

            entityType.AddStructuralProperty("complexPropertyName", complexReference);
            model.AddElement(entityType);

            ODataComplexValue complexValue = new ODataComplexValue {
                Properties = new[] { new ODataProperty {
                                         Name = "propertyName1", Value = 1
                                     }, new ODataProperty {
                                         Name = "propertyName2", Value = "stringValue"
                                     } }, TypeName = "ns.complex"
            };
            ODataProperty complexProperty = new ODataProperty {
                Name = "complexPropertyName", Value = complexValue
            };

            string val = SerializeProperty(model, complexProperty);

            val.Should().Be("<?xml version=\"1.0\" encoding=\"utf-8\"?><m:value xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" xmlns:georss=\"http://www.georss.org/georss\" xmlns:gml=\"http://www.opengis.net/gml\" m:context=\"http://odata.org/$metadata#ns.complex\" m:type=\"#ns.complex\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\"><d:propertyName1 m:type=\"Int32\">1</d:propertyName1><d:propertyName2>stringValue</d:propertyName2></m:value>");
        }
예제 #3
0
        public void AmbiguousPropertyTest()
        {
            EdmComplexType complex = new EdmComplexType("Bar", "Foo");

            StubEdmStructuralProperty prop1 = new StubEdmStructuralProperty("Foo");
            StubEdmStructuralProperty prop2 = new StubEdmStructuralProperty("Foo");
            StubEdmStructuralProperty prop3 = new StubEdmStructuralProperty("Foo");

            prop1.DeclaringType = complex;
            prop2.DeclaringType = complex;
            prop3.DeclaringType = complex;

            complex.AddProperty(prop1);
            complex.AddProperty(prop2);
            complex.AddProperty(prop3);

            IEdmProperty ambiguous = complex.FindProperty("Foo");

            Assert.IsTrue(ambiguous.IsBad(), "Ambiguous binding is bad");

            Assert.AreEqual(EdmPropertyKind.None, ambiguous.PropertyKind, "No property kind.");
            Assert.AreEqual(complex, ambiguous.DeclaringType, "Correct declaring type.");
            Assert.IsTrue(ambiguous.Type.IsBad(), "Type is bad.");

            complex             = new EdmComplexType("Bar", "Foo");
            prop3               = new StubEdmStructuralProperty("Foo");
            prop3.DeclaringType = complex;
            complex.AddProperty(prop3);
            Assert.AreEqual(prop3, complex.FindProperty("Foo"), "Correct last item remaining.");
        }
        public ODataAtomReaderEnumIntegrationTests()
        {
            this.userModel = new EdmModel();

            // enum without flags
            var enumType = new EdmEnumType("NS", "Color");
            var red      = new EdmEnumMember(enumType, "Red", new EdmIntegerConstant(1));

            enumType.AddMember(red);
            enumType.AddMember("Green", new EdmIntegerConstant(2));
            enumType.AddMember("Blue", new EdmIntegerConstant(3));
            this.userModel.AddElement(enumType);

            // enum with flags
            var enumFlagsType = new EdmEnumType("NS", "ColorFlags", isFlags: true);

            enumFlagsType.AddMember("Red", new EdmIntegerConstant(1));
            enumFlagsType.AddMember("Green", new EdmIntegerConstant(2));
            enumFlagsType.AddMember("Blue", new EdmIntegerConstant(4));
            this.userModel.AddElement(enumFlagsType);

            this.entityType = new EdmEntityType("NS", "MyEntityType", isAbstract: false, isOpen: true, baseType: null);
            EdmStructuralProperty floatId = new EdmStructuralProperty(this.entityType, "FloatId", EdmCoreModel.Instance.GetSingle(false));

            this.entityType.AddKeys(floatId);
            this.entityType.AddProperty(floatId);
            var enumTypeReference = new EdmEnumTypeReference(enumType, true);

            this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "Color", enumTypeReference));
            var enumFlagsTypeReference = new EdmEnumTypeReference(enumFlagsType, false);

            this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "ColorFlags", enumFlagsTypeReference));

            // enum in complex type
            EdmComplexType myComplexType = new EdmComplexType("NS", "MyComplexType");

            myComplexType.AddProperty(new EdmStructuralProperty(myComplexType, "MyColorFlags", enumFlagsTypeReference));
            myComplexType.AddProperty(new EdmStructuralProperty(myComplexType, "Height", EdmCoreModel.Instance.GetDouble(false)));
            this.userModel.AddElement(myComplexType);
            this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "MyComplexType", new EdmComplexTypeReference(myComplexType, true)));

            // enum in collection type
            EdmCollectionType myCollectionType = new EdmCollectionType(enumFlagsTypeReference);

            this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "MyCollectionType", new EdmCollectionTypeReference(myCollectionType)));

            this.userModel.AddElement(this.entityType);

            var defaultContainer = new EdmEntityContainer("NS", "DefaultContainer");

            this.entitySet = new EdmEntitySet(defaultContainer, "MySet", this.entityType);
            defaultContainer.AddEntitySet(this.entitySet.Name, this.entityType);
            this.userModel.AddElement(defaultContainer);
        }
예제 #5
0
        public static IEdmModel GetModelFunctionsOnNonEntityTypes()
        {
            EdmComplexType colorInfoType = new EdmComplexType("Test", "ColorInfo");

            colorInfoType.AddProperty(new EdmStructuralProperty(colorInfoType, "Red", EdmCoreModel.Instance.GetInt32(false)));
            colorInfoType.AddProperty(new EdmStructuralProperty(colorInfoType, "Green", EdmCoreModel.Instance.GetInt32(false)));
            colorInfoType.AddProperty(new EdmStructuralProperty(colorInfoType, "Blue", EdmCoreModel.Instance.GetInt32(false)));

            EdmEntityType          vegetableType = new EdmEntityType("Test", "Vegetable");
            IEdmStructuralProperty id            = vegetableType.AddStructuralProperty("ID", EdmCoreModel.Instance.GetInt32(false));

            vegetableType.AddKeys(id);
            vegetableType.AddStructuralProperty("Color", new EdmComplexTypeReference(colorInfoType, false));

            EdmEntityContainer container = new EdmEntityContainer("Test", "Container");
            var set = container.AddEntitySet("Vegetables", vegetableType);

            var function1 = new EdmFunction("Test", "IsPrime", EdmCoreModel.Instance.GetBoolean(false), true, null, true);

            function1.AddParameter("integer", EdmCoreModel.Instance.GetInt32(false));
            container.AddFunctionImport("IsPrime", function1);

            var action = new EdmAction("Test", "Subtract", EdmCoreModel.Instance.GetInt32(false), true /*isBound*/, null /*entitySetPath*/);

            action.AddParameter("integer", EdmCoreModel.Instance.GetInt32(false));
            container.AddActionImport(action);

            var function2 = new EdmFunction("Test", "IsDark", EdmCoreModel.Instance.GetBoolean(false), true, null, true);

            function2.AddParameter("color", new EdmComplexTypeReference(colorInfoType, false));
            container.AddFunctionImport("IsDark", function2);

            var function3 = new EdmFunction("Test", "IsDarkerThan", EdmCoreModel.Instance.GetBoolean(false), true, null, true);

            function3.AddParameter("color", new EdmComplexTypeReference(colorInfoType, false));
            function3.AddParameter("other", new EdmComplexTypeReference(colorInfoType, true));
            container.AddFunctionImport("IsDarkerThan", function3);

            var function4 = new EdmFunction("Test", "GetMostPopularVegetableWithThisColor", new EdmEntityTypeReference(vegetableType, true), true, new EdmPathExpression("color"), true);

            function4.AddParameter("color", new EdmComplexTypeReference(colorInfoType, false));

            EdmModel model = new EdmModel();

            model.AddElement(container);
            model.AddElement(vegetableType);
            model.AddElement(action);
            model.AddElement(function1);
            model.AddElement(function2);
            model.AddElement(function3);
            model.AddElement(function4);
            return(model);
        }
        public void SerializeTopLevelPropertyOfCollectionTypeShouldWork()
        {
            EdmModel model = new EdmModel();

            EdmComplexType complexType = new EdmComplexType("ns", "complex");

            complexType.AddProperty(new EdmStructuralProperty(complexType, "propertyName1", EdmCoreModel.Instance.GetInt32(isNullable: false)));
            complexType.AddProperty(new EdmStructuralProperty(complexType, "propertyName2", EdmCoreModel.Instance.GetString(isNullable: false)));
            model.AddElement(complexType);

            ODataCollectionValue primitiveCollectionValue = new ODataCollectionValue {
                Items = new[] { "value1", "value2" }
            };

            primitiveCollectionValue.TypeName = "Collection(Edm.String)";
            ODataProperty primitiveCollectionProperty = new ODataProperty {
                Name = "PrimitiveCollectionProperty", Value = primitiveCollectionValue
            };

            string pval = SerializeProperty(model, primitiveCollectionProperty);

            pval.Should().Be("<?xml version=\"1.0\" encoding=\"utf-8\"?><m:value xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" xmlns:georss=\"http://www.georss.org/georss\" xmlns:gml=\"http://www.opengis.net/gml\" m:context=\"http://odata.org/$metadata#Collection(Edm.String)\" m:type=\"#Collection(String)\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\"><m:element>value1</m:element><m:element>value2</m:element></m:value>");

            ODataComplexValue complexValue1 = new ODataComplexValue {
                Properties = new[] { new ODataProperty {
                                         Name = "propertyName1", Value = 1
                                     }, new ODataProperty {
                                         Name = "propertyName2", Value = "stringValue"
                                     } }, TypeName = "ns.complex"
            };
            ODataComplexValue complexValue2 = new ODataComplexValue {
                Properties = new[] { new ODataProperty {
                                         Name = "propertyName1", Value = 1
                                     }, new ODataProperty {
                                         Name = "propertyName2", Value = "stringValue"
                                     } }, TypeName = "ns.complex"
            };
            ODataCollectionValue complexCollectionValue = new ODataCollectionValue {
                Items = new[] { complexValue1, complexValue2 }, TypeName = "Collection(ns.complex)"
            };
            ODataProperty complexCollectionProperty = new ODataProperty {
                Name = "ComplexCollectionProperty", Value = complexCollectionValue
            };

            string cval = SerializeProperty(model, complexCollectionProperty);

            cval.Should().Be("<?xml version=\"1.0\" encoding=\"utf-8\"?><m:value xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" xmlns:georss=\"http://www.georss.org/georss\" xmlns:gml=\"http://www.opengis.net/gml\" m:context=\"http://odata.org/$metadata#Collection(ns.complex)\" m:type=\"#Collection(ns.complex)\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\"><m:element><d:propertyName1 m:type=\"Int32\">1</d:propertyName1><d:propertyName2>stringValue</d:propertyName2></m:element><m:element><d:propertyName1 m:type=\"Int32\">1</d:propertyName1><d:propertyName2>stringValue</d:propertyName2></m:element></m:value>");
        }
        public void Initialize()
        {
            EdmModel      edmModel      = new EdmModel();
            EdmEntityType edmEntityType = new EdmEntityType("TestNamespace", "EntityType", baseType: null, isAbstract: false, isOpen: true);

            edmModel.AddElement(edmEntityType);
            EdmComplexType ComplexType = new EdmComplexType("TestNamespace", "TestComplexType");

            ComplexType.AddProperty(new EdmStructuralProperty(ComplexType, "StringProperty", EdmCoreModel.Instance.GetString(false)));
            edmModel.AddElement(ComplexType);
            EdmComplexType derivedComplexType = new EdmComplexType("TestNamespace", "TestDerivedComplexType", ComplexType, true);

            derivedComplexType.AddProperty(new EdmStructuralProperty(derivedComplexType, "DerivedStringProperty", EdmCoreModel.Instance.GetString(false)));
            edmModel.AddElement(derivedComplexType);

            var defaultContainer = new EdmEntityContainer("TestNamespace", "DefaultContainer_sub");

            edmModel.AddElement(defaultContainer);
            this.entitySet = new EdmEntitySet(defaultContainer, "TestEntitySet", edmEntityType);
            defaultContainer.AddElement(this.entitySet);

            this.model       = TestUtils.WrapReferencedModelsToMainModel("TestNamespace", "DefaultContainer", edmModel);
            this.entityType  = edmEntityType;
            this.complexType = ComplexType;
        }
예제 #8
0
        public void ShouldWriteCollectionOfDerivedResourceValueItem()
        {
            EdmModel       currentModel = new EdmModel();
            EdmComplexType addressType  = new EdmComplexType("ns", "Address");

            addressType.AddProperty(new EdmStructuralProperty(addressType, "Street", EdmCoreModel.Instance.GetString(isNullable: true)));
            currentModel.AddElement(addressType);
            EdmComplexType homeAddressType = new EdmComplexType("ns", "HomeAddress", addressType);

            homeAddressType.AddProperty(new EdmStructuralProperty(homeAddressType, "City", EdmCoreModel.Instance.GetString(isNullable: true)));
            currentModel.AddElement(homeAddressType);
            this.model = currentModel;

            var address = new ODataResourceValue
            {
                TypeName   = "ns.HomeAddress",
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "Street", Value = "1 Microsoft Way"
                    },
                    new ODataProperty {
                        Name = "City", Value = "Redmond"
                    },
                }
            };

            WriteAndValidate(new ODataCollectionStart(),
                             new object[] { address },
                             "{\"@odata.context\":\"http://odata.org/test/$metadata#Collection(ns.Address)\",\"value\":[{\"@odata.type\":\"#ns.HomeAddress\",\"Street\":\"1 Microsoft Way\",\"City\":\"Redmond\"}]}",
                             true,
                             new EdmComplexTypeReference(addressType, false));
        }
예제 #9
0
        static ODataEntityReferenceLinksTests()
        {
            EdmModel       tmpModel    = new EdmModel();
            EdmComplexType complexType = new EdmComplexType("TestNamespace", "TestComplexType");

            complexType.AddProperty(new EdmStructuralProperty(complexType, "StringProperty", EdmCoreModel.Instance.GetString(false)));
            tmpModel.AddElement(complexType);

            EntityType = new EdmEntityType("TestNamespace", "TestEntityType");
            tmpModel.AddElement(EntityType);
            var keyProperty = new EdmStructuralProperty(EntityType, "ID", EdmCoreModel.Instance.GetInt32(false));

            EntityType.AddKeys(new IEdmStructuralProperty[] { keyProperty });
            EntityType.AddProperty(keyProperty);

            var defaultContainer = new EdmEntityContainer("TestNamespace", "DefaultContainer_sub");

            tmpModel.AddElement(defaultContainer);
            EntitySet = new EdmEntitySet(defaultContainer, "Customers", EntityType);
            defaultContainer.AddElement(EntitySet);

            EdmModel = TestUtils.WrapReferencedModelsToMainModel("TestNamespace", "DefaultContainer", tmpModel);
            MessageReaderSettingsReadAndValidateCustomInstanceAnnotations = new ODataMessageReaderSettings {
                ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*")
            };
        }
예제 #10
0
        static CustomInstanceAnnotationAcceptanceTests()
        {
            Model      = new EdmModel();
            EntityType = new EdmEntityType("TestNamespace", "TestEntityType");
            Model.AddElement(EntityType);

            var keyProperty = new EdmStructuralProperty(EntityType, "ID", EdmCoreModel.Instance.GetInt32(false));

            EntityType.AddKeys(new IEdmStructuralProperty[] { keyProperty });
            EntityType.AddProperty(keyProperty);
            var resourceNavigationProperty = EntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ResourceNavigationProperty", Target = EntityType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne
            });
            var resourceSetNavigationProperty = EntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ResourceSetNavigationProperty", Target = EntityType, TargetMultiplicity = EdmMultiplicity.Many
            });

            var defaultContainer = new EdmEntityContainer("TestNamespace", "DefaultContainer");

            Model.AddElement(defaultContainer);
            EntitySet = new EdmEntitySet(defaultContainer, "TestEntitySet", EntityType);
            EntitySet.AddNavigationTarget(resourceNavigationProperty, EntitySet);
            EntitySet.AddNavigationTarget(resourceSetNavigationProperty, EntitySet);
            defaultContainer.AddElement(EntitySet);

            ComplexType = new EdmComplexType("TestNamespace", "TestComplexType");
            ComplexType.AddProperty(new EdmStructuralProperty(ComplexType, "StringProperty", EdmCoreModel.Instance.GetString(false)));
            Model.AddElement(ComplexType);
        }
예제 #11
0
        private static IEdmModel GetEdmModel()
        {
            EdmModel model       = new EdmModel();
            var      complexType = new EdmComplexType("NS", "Entity");
            var      property    = new EdmStructuralProperty(complexType, "Sample", EdmCoreModel.Instance.GetString(false));

            complexType.AddProperty(property);
            model.AddElement(complexType);
            return(model);
        }
예제 #12
0
        public static IEdmModel SimpleEdmx()
        {
            var model = new EdmModel();

            EdmComplexType simpleType = new EdmComplexType("DefaultNamespace", "SimpleType");

            simpleType.AddProperty(new EdmStructuralProperty(simpleType, "Data", EdmCoreModel.Instance.GetString(true)));
            model.AddElement(simpleType);

            EdmEntityContainer container = new EdmEntityContainer("DefaultNamespace", "Container");

            model.AddElement(container);

            return(model);
        }
예제 #13
0
        public void Init()
        {
            model = new EdmModel();
            EdmComplexType complexType = new EdmComplexType("ns", "complex");

            complexType.AddProperty(new EdmStructuralProperty(complexType, "StringProperty", EdmCoreModel.Instance.GetString(isNullable: false)));
            model.AddElement(complexType);

            this.stream   = new MemoryStream();
            this.settings = new ODataMessageWriterSettings {
                Version = ODataVersion.V4, ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*")
            };
            this.settings.SetServiceDocumentUri(ServiceDocumentUri);
            this.serializer = new ODataAtomPropertyAndValueSerializer(this.CreateAtomOutputContext(model, this.stream));
            this.instanceAnnotationWriteTracker = new InstanceAnnotationWriteTracker();
        }
예제 #14
0
        public void Exception_From_Compute_Does_Not_Make_Cache_To_Fail_With_NullRefException()
        {
            var complexType = new EdmComplexType("Bar", "Foo");
            var property    = new StubEdmStructuralProperty(null)
            {
                DeclaringType = complexType
            };

            complexType.AddProperty(property);

            for (int i = 0; i < 10; i++)
            {
                try
                {
                    complexType.FindProperty(null);
                }
                catch (ArgumentNullException) { }
            }
        }
        static ODataJsonLightEntryAndFeedDeserializerTests()
        {
            EdmModel       tmpModel    = new EdmModel();
            EdmComplexType complexType = new EdmComplexType("TestNamespace", "TestComplexType");

            complexType.AddProperty(new EdmStructuralProperty(complexType, "StringProperty", EdmCoreModel.Instance.GetString(false)));
            tmpModel.AddElement(complexType);

            EntityType = new EdmEntityType("TestNamespace", "TestEntityType");
            tmpModel.AddElement(EntityType);
            var keyProperty = new EdmStructuralProperty(EntityType, "ID", EdmCoreModel.Instance.GetInt32(false));

            EntityType.AddKeys(new IEdmStructuralProperty[] { keyProperty });
            EntityType.AddProperty(keyProperty);

            var defaultContainer = new EdmEntityContainer("TestNamespace", "DefaultContainer_sub");

            tmpModel.AddElement(defaultContainer);
            EntitySet = new EdmEntitySet(defaultContainer, "TestEntitySet", EntityType);
            defaultContainer.AddElement(EntitySet);

            Action = new EdmAction("TestNamespace", "DoSomething", null, true, null);
            Action.AddParameter("p1", new EdmEntityTypeReference(EntityType, false));
            tmpModel.AddElement(Action);

            ActionImport = defaultContainer.AddActionImport("DoSomething", Action);

            var serviceOperationFunction = new EdmFunction("TestNamespace", "ServiceOperation", EdmCoreModel.Instance.GetInt32(true));

            defaultContainer.AddFunctionImport("ServiceOperation", serviceOperationFunction);
            tmpModel.AddElement(serviceOperationFunction);

            tmpModel.AddElement(new EdmTerm("custom", "DateTimeOffsetAnnotation", EdmPrimitiveTypeKind.DateTimeOffset));
            tmpModel.AddElement(new EdmTerm("custom", "DateAnnotation", EdmPrimitiveTypeKind.Date));
            tmpModel.AddElement(new EdmTerm("custom", "TimeOfDayAnnotation", EdmPrimitiveTypeKind.TimeOfDay));

            EdmModel = TestUtils.WrapReferencedModelsToMainModel("TestNamespace", "DefaultContainer", tmpModel);
            MessageReaderSettingsReadAndValidateCustomInstanceAnnotations = new ODataMessageReaderSettings {
                ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*")
            };
            MessageReaderSettingsIgnoreInstanceAnnotations = new ODataMessageReaderSettings();
        }
예제 #16
0
        public void ValidateSerializationBlockingErrors()
        {
            EdmModel model = new EdmModel();

            EdmComplexType complexWithBadProperty = new EdmComplexType("Foo", "Bar");

            complexWithBadProperty.AddProperty(new EdmStructuralProperty(complexWithBadProperty, "baz", new EdmComplexTypeReference(new EdmComplexType("", ""), false)));
            model.AddElement(complexWithBadProperty);

            EdmEntityType          baseType = new EdmEntityType("Foo", "");
            IEdmStructuralProperty keyProp  = new EdmStructuralProperty(baseType, "Id", EdmCoreModel.Instance.GetInt32(false));

            baseType.AddProperty(keyProp);
            baseType.AddKeys(keyProp);

            EdmEntityType derivedType = new EdmEntityType("Foo", "Quip", baseType);

            model.AddElement(baseType);
            model.AddElement(derivedType);
            EdmNavigationProperty navProp = derivedType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name = "navProp", Target = derivedType, TargetMultiplicity = EdmMultiplicity.One
            });

            EdmEntityContainer container = new EdmEntityContainer("Foo", "Container");

            model.AddElement(container);
            container.AddElement(new EdmEntitySet(container, "badNameSet", baseType));

            StringBuilder          sb = new StringBuilder();
            IEnumerable <EdmError> errors;
            bool written        = model.TryWriteSchema(XmlWriter.Create(sb), out errors);
            var  expectedErrors = new EdmLibTestErrors()
            {
                { "([. Nullable=False])", EdmErrorCode.ReferencedTypeMustHaveValidName },
                { "(Foo.Quip)", EdmErrorCode.ReferencedTypeMustHaveValidName },
                { "(Microsoft.OData.Edm.EdmEntitySet)", EdmErrorCode.ReferencedTypeMustHaveValidName },
            };

            this.CompareErrors(errors, expectedErrors);
        }
        public void Init()
        {
            EdmModel model = new EdmModel();

            EdmComplexType complex1 = new EdmComplexType("ns", "complex1");

            complex1.AddProperty(new EdmStructuralProperty(complex1, "p1", EdmCoreModel.Instance.GetInt32(isNullable: false)));
            model.AddElement(complex1);

            EdmComplexType complex2 = new EdmComplexType("ns", "complex2");

            complex2.AddProperty(new EdmStructuralProperty(complex2, "p1", EdmCoreModel.Instance.GetInt32(isNullable: false)));
            model.AddElement(complex2);

            EdmComplexTypeReference    complex2Reference                = new EdmComplexTypeReference(complex2, isNullable: false);
            EdmCollectionType          primitiveCollectionType          = new EdmCollectionType(EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false));
            EdmCollectionType          complexCollectionType            = new EdmCollectionType(complex2Reference);
            EdmCollectionTypeReference primitiveCollectionTypeReference = new EdmCollectionTypeReference(primitiveCollectionType);
            EdmCollectionTypeReference complexCollectionTypeReference   = new EdmCollectionTypeReference(complexCollectionType);

            model.AddElement(new EdmTerm("custom", "int", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "string", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "double", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Double, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "bool", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: true)));
            model.AddElement(new EdmTerm("custom", "decimal", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Decimal, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "timespan", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Duration, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "guid", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Guid, isNullable: false)));
            model.AddElement(new EdmTerm("custom", "complex", complex2Reference));
            model.AddElement(new EdmTerm("custom", "primitiveCollection", primitiveCollectionTypeReference));
            model.AddElement(new EdmTerm("custom", "complexCollection", complexCollectionTypeReference));

            this.stream   = new MemoryStream();
            this.settings = new ODataMessageWriterSettings {
                Version = ODataVersion.V4, ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*")
            };
            this.settings.SetServiceDocumentUri(ServiceDocumentUri);
            this.serializer = new ODataAtomPropertyAndValueSerializer(this.CreateAtomOutputContext(model, this.stream));
        }
예제 #18
0
        static InstanceAnnotationsReaderIntegrationTests()
        {
            EdmModel modelTmp = new EdmModel();

            EntityType = new EdmEntityType("TestNamespace", "TestEntityType");
            modelTmp.AddElement(EntityType);

            var keyProperty = new EdmStructuralProperty(EntityType, "ID", EdmCoreModel.Instance.GetInt32(false));

            EntityType.AddKeys(new IEdmStructuralProperty[] { keyProperty });
            EntityType.AddProperty(keyProperty);
            var resourceNavigationProperty = EntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ResourceNavigationProperty", Target = EntityType, TargetMultiplicity = EdmMultiplicity.ZeroOrOne
            });
            var resourceSetNavigationProperty = EntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ResourceSetNavigationProperty", Target = EntityType, TargetMultiplicity = EdmMultiplicity.Many
            });

            var defaultContainer = new EdmEntityContainer("TestNamespace", "DefaultContainer_sub");

            modelTmp.AddElement(defaultContainer);
            EntitySet = new EdmEntitySet(defaultContainer, "TestEntitySet", EntityType);
            EntitySet.AddNavigationTarget(resourceNavigationProperty, EntitySet);
            EntitySet.AddNavigationTarget(resourceSetNavigationProperty, EntitySet);
            defaultContainer.AddElement(EntitySet);

            Singleton = new EdmSingleton(defaultContainer, "TestSingleton", EntityType);
            Singleton.AddNavigationTarget(resourceNavigationProperty, EntitySet);
            Singleton.AddNavigationTarget(resourceSetNavigationProperty, EntitySet);
            defaultContainer.AddElement(Singleton);

            ComplexType = new EdmComplexType("TestNamespace", "TestComplexType");
            ComplexType.AddProperty(new EdmStructuralProperty(ComplexType, "StringProperty", EdmCoreModel.Instance.GetString(false)));
            modelTmp.AddElement(ComplexType);

            Model = TestUtils.WrapReferencedModelsToMainModel("TestNamespace", "DefaultContainer", modelTmp);
        }
예제 #19
0
        public static IEdmModel CreateModel(string ns)
        {
            EdmModel model            = new EdmModel();
            var      defaultContainer = new EdmEntityContainer(ns, "DefaultContainer");

            model.AddElement(defaultContainer);

            var addressType = new EdmComplexType(ns, "Address");

            addressType.AddProperty(new EdmStructuralProperty(addressType, "Road", EdmCoreModel.Instance.GetString(false)));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "City", EdmCoreModel.Instance.GetString(false)));
            model.AddElement(addressType);

            var personType       = new EdmEntityType(ns, "Person");
            var personIdProperty = new EdmStructuralProperty(personType, "PersonId", EdmCoreModel.Instance.GetInt32(false));

            personType.AddProperty(personIdProperty);
            personType.AddKeys(new IEdmStructuralProperty[] { personIdProperty });
            personType.AddProperty(new EdmStructuralProperty(personType, "FirstName", EdmCoreModel.Instance.GetString(false)));
            personType.AddProperty(new EdmStructuralProperty(personType, "LastName", EdmCoreModel.Instance.GetString(false)));
            personType.AddProperty(new EdmStructuralProperty(personType, "Address", new EdmComplexTypeReference(addressType, true)));
            personType.AddProperty(new EdmStructuralProperty(personType, "Descriptions", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(false)))));

            model.AddElement(personType);
            var peopleSet = new EdmEntitySet(defaultContainer, "People", personType);

            defaultContainer.AddElement(peopleSet);

            var numberComboType = new EdmComplexType(ns, "NumberCombo");

            numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Small", EdmCoreModel.Instance.GetInt32(false)));
            numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Middle", EdmCoreModel.Instance.GetInt64(false)));
            numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Large", EdmCoreModel.Instance.GetDecimal(false)));
            model.AddElement(numberComboType);

            var productType       = new EdmEntityType(ns, "Product");
            var productIdProperty = new EdmStructuralProperty(productType, "ProductId", EdmCoreModel.Instance.GetInt32(false));

            productType.AddProperty(productIdProperty);
            productType.AddKeys(new IEdmStructuralProperty[] { productIdProperty });
            productType.AddProperty(new EdmStructuralProperty(productType, "Quantity", EdmCoreModel.Instance.GetInt64(false)));
            productType.AddProperty(new EdmStructuralProperty(productType, "LifeTimeInSeconds", EdmCoreModel.Instance.GetDecimal(false)));
            productType.AddProperty(new EdmStructuralProperty(productType, "TheCombo", new EdmComplexTypeReference(numberComboType, true)));
            productType.AddProperty(new EdmStructuralProperty(productType, "LargeNumbers", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetDecimal(false)))));

            model.AddElement(productType);
            var productsSet = new EdmEntitySet(defaultContainer, "Products", productType);

            defaultContainer.AddElement(productsSet);

            var productsProperty = personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "Products",
                Target             = productType,
                TargetMultiplicity = EdmMultiplicity.Many
            });

            peopleSet.AddNavigationTarget(productsProperty, productsSet);

            IEnumerable <EdmError> errors;

            model.Validate(out errors);

            return(model);
        }
예제 #20
0
        /// <summary>
        /// Creates a test model shared among parameter reader/writer tests.
        /// </summary>
        /// <returns>Returns a model with operation imports.</returns>
        public static IEdmModel BuildModelWithFunctionImport()
        {
            EdmCoreModel       coreModel            = EdmCoreModel.Instance;
            EdmModel           model                = new EdmModel();
            const string       defaultNamespaceName = DefaultNamespaceName;
            EdmEntityContainer container            = new EdmEntityContainer(defaultNamespaceName, "TestContainer");

            model.AddElement(container);

            EdmComplexType complexType = new EdmComplexType(defaultNamespaceName, "ComplexType");

            complexType.AddProperty(new EdmStructuralProperty(complexType, "PrimitiveProperty", coreModel.GetString(false)));
            complexType.AddProperty(new EdmStructuralProperty(complexType, "ComplexProperty", complexType.ToTypeReference(false)));
            model.AddElement(complexType);

            EdmEnumType enumType = new EdmEnumType(defaultNamespaceName, "EnumType");

            model.AddElement(enumType);

            EdmEntityType entityType = new EdmEntityType(defaultNamespaceName, "EntityType");

            entityType.AddKeys(new IEdmStructuralProperty[] { new EdmStructuralProperty(entityType, "ID", coreModel.GetInt32(false)) });
            entityType.AddProperty(new EdmStructuralProperty(entityType, "ComplexProperty", complexType.ToTypeReference()));

            container.AddFunctionAndFunctionImport(model, "FunctionImport_Primitive", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/).Function.AsEdmFunction().AddParameter("primitive", coreModel.GetString(false));
            container.AddFunctionAndFunctionImport(model, "FunctionImport_NullablePrimitive", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/).Function.AsEdmFunction().AddParameter("nullablePrimitive", coreModel.GetString(true));
            EdmCollectionType stringCollectionType = new EdmCollectionType(coreModel.GetString(true));

            container.AddFunctionAndFunctionImport(model, "FunctionImport_PrimitiveCollection", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/).Function.AsEdmFunction().AddParameter("primitiveCollection", stringCollectionType.ToTypeReference(false));
            container.AddFunctionAndFunctionImport(model, "FunctionImport_Complex", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/).Function.AsEdmFunction().AddParameter("complex", complexType.ToTypeReference(true));
            EdmCollectionType complexCollectionType = new EdmCollectionType(complexType.ToTypeReference());

            container.AddFunctionAndFunctionImport(model, "FunctionImport_ComplexCollection", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/).Function.AsEdmFunction().AddParameter("complexCollection", complexCollectionType.ToTypeReference());
            container.AddFunctionAndFunctionImport(model, "FunctionImport_Entry", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, true /*bindable*/).Function.AsEdmFunction().AddParameter("entry", entityType.ToTypeReference());
            EdmCollectionType entityCollectionType = new EdmCollectionType(entityType.ToTypeReference());

            container.AddFunctionAndFunctionImport(model, "FunctionImport_Feed", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, true /*bindable*/).Function.AsEdmFunction().AddParameter("feed", entityCollectionType.ToTypeReference());
            container.AddFunctionAndFunctionImport(model, "FunctionImport_Stream", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/).Function.AsEdmFunction().AddParameter("stream", coreModel.GetStream(false));
            container.AddFunctionAndFunctionImport(model, "FunctionImport_Enum", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/).Function.AsEdmFunction().AddParameter("enum", enumType.ToTypeReference());

            var functionImport_PrimitiveTwoParameters = container.AddFunctionAndFunctionImport(model, "FunctionImport_PrimitiveTwoParameters", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/);
            var function_PrimitiveTwoParameters       = functionImport_PrimitiveTwoParameters.Function.AsEdmFunction();

            function_PrimitiveTwoParameters.AddParameter("p1", coreModel.GetInt32(false));
            function_PrimitiveTwoParameters.AddParameter("p2", coreModel.GetString(false));

            container.AddFunctionAndFunctionImport(model, "FunctionImport_Int", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/).Function.AsEdmFunction().AddParameter("p1", coreModel.GetInt32(false));
            container.AddFunctionAndFunctionImport(model, "FunctionImport_Double", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/).Function.AsEdmFunction().AddParameter("p1", coreModel.GetDouble(false));
            EdmCollectionType int32CollectionType = new EdmCollectionType(coreModel.GetInt32(false));

            container.AddActionAndActionImport(model, "FunctionImport_NonNullablePrimitiveCollection", null /*returnType*/, null /*entitySet*/, false /*bindable*/).Action.AsEdmAction().AddParameter("p1", int32CollectionType.ToTypeReference(false));

            EdmComplexType complexType2 = new EdmComplexType(defaultNamespaceName, "ComplexTypeWithNullableProperties");

            complexType2.AddProperty(new EdmStructuralProperty(complexType2, "StringProperty", coreModel.GetString(true)));
            complexType2.AddProperty(new EdmStructuralProperty(complexType2, "IntegerProperty", coreModel.GetInt32(true)));
            model.AddElement(complexType2);

            EdmEnumType enumType1 = new EdmEnumType(defaultNamespaceName, "enumType1");

            enumType1.AddMember(new EdmEnumMember(enumType1, "enumType1_value1", new EdmEnumMemberValue(6)));
            model.AddElement(enumType1);

            var functionImport_MultipleNullableParameters = container.AddFunctionAndFunctionImport(model, "FunctionImport_MultipleNullableParameters", coreModel.GetString(false) /*returnType*/, null /*entitySet*/, false /*composable*/, false /*bindable*/);
            var function_MultipleNullableParameters       = functionImport_MultipleNullableParameters.Function.AsEdmFunction();

            function_MultipleNullableParameters.AddParameter("p1", coreModel.GetBinary(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p2", coreModel.GetBoolean(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p3", coreModel.GetByte(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p5", coreModel.GetDateTimeOffset(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p6", coreModel.GetDecimal(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p7", coreModel.GetDouble(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p8", coreModel.GetGuid(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p9", coreModel.GetInt16(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p10", coreModel.GetInt32(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p11", coreModel.GetInt64(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p12", coreModel.GetSByte(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p13", coreModel.GetSingle(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p14", coreModel.GetSpatial(EdmPrimitiveTypeKind.GeographyPoint, true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p15", coreModel.GetString(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p16", complexType2.ToTypeReference(true /*isNullable*/));
            function_MultipleNullableParameters.AddParameter("p17", enumType1.ToTypeReference(true /*isNullable*/));

            return(model);
        }
        private IEdmModel CreateEdmModel()
        {
            var model = new EdmModel();

            EdmComplexType simpleComplexType = new EdmComplexType(DefaultNamespaceName, "SimplexComplexType");

            simpleComplexType.AddProperty(new EdmStructuralProperty(simpleComplexType, "Name", StringTypeRef));
            model.AddElement(simpleComplexType);

            EdmComplexType simpleComplexType2 = new EdmComplexType(DefaultNamespaceName, "SimplexComplexType2");

            simpleComplexType2.AddProperty(new EdmStructuralProperty(simpleComplexType2, "Value", Int32NullableTypeRef));
            model.AddElement(simpleComplexType2);

            EdmComplexType nestedComplexType = new EdmComplexType(DefaultNamespaceName, "NestedComplexType");

            nestedComplexType.AddProperty(new EdmStructuralProperty(nestedComplexType, "InnerComplexProperty", new EdmComplexTypeReference(simpleComplexType2, isNullable: false)));
            model.AddElement(nestedComplexType);

            EdmComplexType ratingComplexType = new EdmComplexType(DefaultNamespaceName, "RatingComplexType");

            ratingComplexType.AddProperty(new EdmStructuralProperty(ratingComplexType, "Rating", Int32NullableTypeRef));
            model.AddElement(ratingComplexType);

            EdmEntityType entityType = new EdmEntityType(DefaultNamespaceName, "EntityType");

            EdmEntityType expandedEntryType = new EdmEntityType(DefaultNamespaceName, "ExpandedEntryType");

            expandedEntryType.AddKeys(expandedEntryType.AddStructuralProperty("Id", Int32TypeRef));
            expandedEntryType.AddStructuralProperty("ExpandedEntryName", StringTypeRef);
            expandedEntryType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ExpandedEntry_DeferredNavigation", Target = entityType, TargetMultiplicity = EdmMultiplicity.One
            });
            expandedEntryType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ExpandedEntry_ExpandedFeed", Target = entityType, TargetMultiplicity = EdmMultiplicity.Many
            });
            model.AddElement(expandedEntryType);

            entityType.AddKeys(entityType.AddStructuralProperty("Id", Int32TypeRef));
            entityType.AddStructuralProperty("StringProperty", StringTypeRef);
            entityType.AddStructuralProperty("NumberProperty", Int32TypeRef);
            entityType.AddStructuralProperty("SimpleComplexProperty", new EdmComplexTypeReference(simpleComplexType, isNullable: false));
            entityType.AddStructuralProperty("DeepComplexProperty", new EdmComplexTypeReference(nestedComplexType, isNullable: false));
            entityType.AddStructuralProperty("PrimitiveCollection", EdmCoreModel.GetCollection(StringTypeRef));
            entityType.AddStructuralProperty("ComplexCollection", EdmCoreModel.GetCollection(new EdmComplexTypeReference(ratingComplexType, isNullable: false)));
            entityType.AddStructuralProperty("NamedStream", EdmPrimitiveTypeKind.Stream, false);
            entityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "DeferredNavigation", Target = entityType, TargetMultiplicity = EdmMultiplicity.One
            });
            entityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "AssociationLink", Target = entityType, TargetMultiplicity = EdmMultiplicity.One
            });
            entityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ExpandedEntry", Target = expandedEntryType, TargetMultiplicity = EdmMultiplicity.One
            });
            entityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "ExpandedFeed", Target = entityType, TargetMultiplicity = EdmMultiplicity.Many
            });
            model.AddElement(entityType);

            EdmEntityType wrappingEntityType = new EdmEntityType(DefaultNamespaceName, "WrappingEntityType");

            wrappingEntityType.AddKeys(wrappingEntityType.AddStructuralProperty("Wrapping_ID", Int32TypeRef));
            wrappingEntityType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo {
                Name = "Wrapping_ExpandedEntry", Target = entityType, TargetMultiplicity = EdmMultiplicity.One
            });
            model.AddElement(wrappingEntityType);

            var container = new EdmEntityContainer(DefaultNamespaceName, "DefaultContainer");

            model.AddElement(container);

            container.AddEntitySet("EntitySet", entityType);
            container.AddEntitySet("WrappingEntitySet", wrappingEntityType);

            return(model);
        }
예제 #22
0
        public static IEdmModel CreateServiceEdmModel(string ns)
        {
            EdmModel model            = new EdmModel();
            var      defaultContainer = new EdmEntityContainer(ns, "PerfInMemoryContainer");

            model.AddElement(defaultContainer);

            var personType       = new EdmEntityType(ns, "Person");
            var personIdProperty = new EdmStructuralProperty(personType, "PersonID", EdmCoreModel.Instance.GetInt32(false));

            personType.AddProperty(personIdProperty);
            personType.AddKeys(new IEdmStructuralProperty[] { personIdProperty });
            personType.AddProperty(new EdmStructuralProperty(personType, "FirstName", EdmCoreModel.Instance.GetString(false)));
            personType.AddProperty(new EdmStructuralProperty(personType, "LastName", EdmCoreModel.Instance.GetString(false)));
            personType.AddProperty(new EdmStructuralProperty(personType, "MiddleName", EdmCoreModel.Instance.GetString(true)));
            personType.AddProperty(new EdmStructuralProperty(personType, "Age", EdmCoreModel.Instance.GetInt32(false)));
            model.AddElement(personType);

            var simplePersonSet = new EdmEntitySet(defaultContainer, "SimplePeopleSet", personType);

            defaultContainer.AddElement(simplePersonSet);

            var largetPersonSet = new EdmEntitySet(defaultContainer, "LargePeopleSet", personType);

            defaultContainer.AddElement(largetPersonSet);

            var addressType = new EdmComplexType(ns, "Address");

            addressType.AddProperty(new EdmStructuralProperty(addressType, "Street", EdmCoreModel.Instance.GetString(false)));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "City", EdmCoreModel.Instance.GetString(false)));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "PostalCode", EdmCoreModel.Instance.GetString(false)));
            model.AddElement(addressType);

            var companyType = new EdmEntityType(ns, "Company");
            var companyId   = new EdmStructuralProperty(companyType, "CompanyID", EdmCoreModel.Instance.GetInt32(false));

            companyType.AddProperty(companyId);
            companyType.AddKeys(companyId);
            companyType.AddProperty(new EdmStructuralProperty(companyType, "Name", EdmCoreModel.Instance.GetString(true)));
            companyType.AddProperty(new EdmStructuralProperty(companyType, "Address", new EdmComplexTypeReference(addressType, true)));
            companyType.AddProperty(new EdmStructuralProperty(companyType, "Revenue", EdmCoreModel.Instance.GetInt32(false)));

            model.AddElement(companyType);

            var companySet = new EdmEntitySet(defaultContainer, "CompanySet", companyType);

            defaultContainer.AddElement(companySet);

            var companyEmployeeNavigation = companyType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "Employees",
                Target             = personType,
                TargetMultiplicity = EdmMultiplicity.Many
            });

            companySet.AddNavigationTarget(companyEmployeeNavigation, largetPersonSet);

            // ResetDataSource
            var resetDataSourceAction = new EdmAction(ns, "ResetDataSource", null, false, null);

            model.AddElement(resetDataSourceAction);
            defaultContainer.AddActionImport(resetDataSourceAction);

            return(model);
        }
예제 #23
0
        public static IEdmModel CreateODataServiceModel(string ns)
        {
            EdmModel model            = new EdmModel();
            var      defaultContainer = new EdmEntityContainer(ns, "OperationService");

            model.AddElement(defaultContainer);

            #region ComplexType

            var addressType = new EdmComplexType(ns, "Address");
            addressType.AddProperty(new EdmStructuralProperty(addressType, "Street", EdmCoreModel.Instance.GetString(false)));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "City", EdmCoreModel.Instance.GetString(false)));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "PostalCode", EdmCoreModel.Instance.GetString(false)));
            model.AddElement(addressType);

            var homeAddressType = new EdmComplexType(ns, "HomeAddress", addressType, false);
            homeAddressType.AddProperty(new EdmStructuralProperty(homeAddressType, "FamilyName", EdmCoreModel.Instance.GetString(true)));
            model.AddElement(homeAddressType);

            var companyAddressType = new EdmComplexType(ns, "CompanyAddress", addressType, false);
            companyAddressType.AddProperty(new EdmStructuralProperty(companyAddressType, "CompanyName", EdmCoreModel.Instance.GetString(false)));
            model.AddElement(companyAddressType);

            #endregion

            #region EnumType

            var customerLevelType = new EdmEnumType(ns, "CustomerLevel");
            customerLevelType.AddMember("Common", new EdmIntegerConstant(0));
            customerLevelType.AddMember("Silver", new EdmIntegerConstant(1));
            customerLevelType.AddMember("Gold", new EdmIntegerConstant(2));
            model.AddElement(customerLevelType);

            #endregion

            #region EntityType

            var customerType       = new EdmEntityType(ns, "Customer");
            var customerIdProperty = new EdmStructuralProperty(customerType, "ID", EdmCoreModel.Instance.GetInt32(false));
            customerType.AddProperty(customerIdProperty);
            customerType.AddKeys(customerIdProperty);
            customerType.AddStructuralProperty("FirstName", EdmCoreModel.Instance.GetString(false));
            customerType.AddStructuralProperty("LastName", EdmCoreModel.Instance.GetString(false));
            customerType.AddStructuralProperty("Address", new EdmComplexTypeReference(addressType, true));
            customerType.AddStructuralProperty("Emails", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(true))));
            customerType.AddStructuralProperty("Level", new EdmEnumTypeReference(customerLevelType, false));
            model.AddElement(customerType);

            var orderType       = new EdmEntityType(ns, "Order");
            var orderIdProperty = new EdmStructuralProperty(orderType, "ID", EdmCoreModel.Instance.GetInt32(false));
            orderType.AddProperty(orderIdProperty);
            orderType.AddKeys(orderIdProperty);
            orderType.AddStructuralProperty("OrderDate", EdmCoreModel.Instance.GetDateTimeOffset(false));
            orderType.AddStructuralProperty("Notes", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(true))));
            model.AddElement(orderType);

            var ordersNavigation = customerType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "Orders",
                Target             = orderType,
                TargetMultiplicity = EdmMultiplicity.Many
            });

            var customerForOrderNavigation = orderType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "Customer",
                Target             = customerType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            #endregion

            #region EntitySet

            var customerSet = new EdmEntitySet(defaultContainer, "Customers", customerType);
            defaultContainer.AddElement(customerSet);

            var orderSet = new EdmEntitySet(defaultContainer, "Orders", orderType);
            defaultContainer.AddElement(orderSet);

            customerSet.AddNavigationTarget(ordersNavigation, orderSet);
            orderSet.AddNavigationTarget(customerForOrderNavigation, customerSet);

            #endregion

            #region Operations

            //UnBound Action: ResetDataSource
            var resetDataSourceAction = new EdmAction(ns, "ResetDataSource", null, false, null);
            model.AddElement(resetDataSourceAction);
            defaultContainer.AddActionImport(resetDataSourceAction);

            var customerTypeReference           = new EdmEntityTypeReference(customerType, true);
            var customerCollectionTypeReference =
                new EdmCollectionTypeReference(new EdmCollectionType(customerTypeReference));
            var addressTypeReference          = new EdmComplexTypeReference(addressType, true);
            var addressCollectionTypeRefernce =
                new EdmCollectionTypeReference(new EdmCollectionType(addressTypeReference));
            var stringTypeReference           = EdmCoreModel.Instance.GetString(true);
            var stringCollectionTypeReference =
                new EdmCollectionTypeReference(new EdmCollectionType(stringTypeReference));
            var orderTypeReference           = new EdmEntityTypeReference(orderType, true);
            var orderCollectionTypeReference =
                new EdmCollectionTypeReference(new EdmCollectionType(orderTypeReference));

            //Bound Function : Bound to Collection Of Entity, Parameter is Complex Value, Return Entity.
            var getCustomerForAddressFunction = new EdmFunction(ns, "GetCustomerForAddress", customerTypeReference, true, new EdmPathExpression("customers"), true);
            getCustomerForAddressFunction.AddParameter("customers", customerCollectionTypeReference);
            getCustomerForAddressFunction.AddParameter("address", addressTypeReference);
            model.AddElement(getCustomerForAddressFunction);

            //Bound Function : Bound to Collection Of Entity, Parameter is Collection of Complex Value, Return Collection Of Entity.
            var getCustomersForAddressesFunction = new EdmFunction(ns, "GetCustomersForAddresses", customerCollectionTypeReference, true, new EdmPathExpression("customers"), true);
            getCustomersForAddressesFunction.AddParameter("customers", customerCollectionTypeReference);
            getCustomersForAddressesFunction.AddParameter("addresses", addressCollectionTypeRefernce);
            model.AddElement(getCustomersForAddressesFunction);

            //Bound Function : Bound to Collection Of Entity, Parameter is Collection of Complex Value, Return Entity
            var getCustomerForAddressesFunction = new EdmFunction(ns, "GetCustomerForAddresses", customerTypeReference, true, new EdmPathExpression("customers"), true);
            getCustomerForAddressesFunction.AddParameter("customers", customerCollectionTypeReference);
            getCustomerForAddressesFunction.AddParameter("addresses", addressCollectionTypeRefernce);
            model.AddElement(getCustomerForAddressesFunction);

            //Bound Function : Bound to Entity, Return Complex Value
            var getCustomerAddressFunction = new EdmFunction(ns, "GetCustomerAddress", addressTypeReference, true, null, true);
            getCustomerAddressFunction.AddParameter("customer", customerTypeReference);
            model.AddElement(getCustomerAddressFunction);

            //Bound Function : Bound to Entity, Parameter is Complex Value, Return Entity
            var verifyCustomerAddressFunction = new EdmFunction(ns, "VerifyCustomerAddress", customerTypeReference, true, new EdmPathExpression("customer"), true);
            verifyCustomerAddressFunction.AddParameter("customer", customerTypeReference);
            verifyCustomerAddressFunction.AddParameter("addresses", addressTypeReference);
            model.AddElement(verifyCustomerAddressFunction);

            //Bound Function : Bound to Entity, Parameter is Entity, Return Entity
            var verifyCustomerByOrderFunction = new EdmFunction(ns, "VerifyCustomerByOrder", customerTypeReference, true, new EdmPathExpression("customer"), true);
            verifyCustomerByOrderFunction.AddParameter("customer", customerTypeReference);
            verifyCustomerByOrderFunction.AddParameter("order", orderTypeReference);
            model.AddElement(verifyCustomerByOrderFunction);

            //Bound Function : Bound to Entity, Parameter is Collection of String, Return Collection of Entity
            var getOrdersFromCustomerByNotesFunction = new EdmFunction(ns, "GetOrdersFromCustomerByNotes", orderCollectionTypeReference, true, new EdmPathExpression("customer/Orders"), true);
            getOrdersFromCustomerByNotesFunction.AddParameter("customer", customerTypeReference);
            getOrdersFromCustomerByNotesFunction.AddParameter("notes", stringCollectionTypeReference);
            model.AddElement(getOrdersFromCustomerByNotesFunction);

            //Bound Function : Bound to Collection Of Entity, Parameter is String, Return Collection of Entity
            var getOrdersByNoteFunction = new EdmFunction(ns, "GetOrdersByNote", orderCollectionTypeReference, true, new EdmPathExpression("orders"), true);
            getOrdersByNoteFunction.AddParameter("orders", orderCollectionTypeReference);
            getOrdersByNoteFunction.AddParameter("note", stringTypeReference);
            model.AddElement(getOrdersByNoteFunction);

            //Bound Function : Bound to Collection Of Entity, Parameter is Collection of String, Return Entity
            var getOrderByNoteFunction = new EdmFunction(ns, "GetOrderByNote", orderTypeReference, true, new EdmPathExpression("orders"), true);
            getOrderByNoteFunction.AddParameter("orders", orderCollectionTypeReference);
            getOrderByNoteFunction.AddParameter("notes", stringCollectionTypeReference);
            model.AddElement(getOrderByNoteFunction);

            //Bound Function : Bound to Collection Of Entity, Parameter is Collection of Entity, Return Collection Of Entity
            var getCustomersByOrdersFunction = new EdmFunction(ns, "GetCustomersByOrders", customerCollectionTypeReference, true, new EdmPathExpression("customers"), true);
            getCustomersByOrdersFunction.AddParameter("customers", customerCollectionTypeReference);
            getCustomersByOrdersFunction.AddParameter("orders", orderCollectionTypeReference);
            model.AddElement(getCustomersByOrdersFunction);

            //Bound Function : Bound to Collection Of Entity, Parameter is Entity, Return Entity
            var getCustomerByOrderFunction = new EdmFunction(ns, "GetCustomerByOrder", customerTypeReference, true, new EdmPathExpression("customers"), true);
            getCustomerByOrderFunction.AddParameter("customers", customerCollectionTypeReference);
            getCustomerByOrderFunction.AddParameter("order", orderTypeReference);
            model.AddElement(getCustomerByOrderFunction);

            // Function Import: Parameter is Collection of Entity, Return Collection Of Entity
            var getCustomersByOrdersUnboundFunction = new EdmFunction(ns, "GetCustomersByOrders", customerCollectionTypeReference, false, null, true);
            getCustomersByOrdersUnboundFunction.AddParameter("orders", orderCollectionTypeReference);
            model.AddElement(getCustomersByOrdersUnboundFunction);
            defaultContainer.AddFunctionImport(getCustomersByOrdersUnboundFunction.Name, getCustomersByOrdersUnboundFunction, new EdmEntitySetReferenceExpression(customerSet));

            // Function Import: Parameter is Collection of Entity, Return Entity
            var getCustomerByOrderUnboundFunction = new EdmFunction(ns, "GetCustomerByOrder", customerTypeReference, false, null, true);
            getCustomerByOrderUnboundFunction.AddParameter("order", orderTypeReference);
            model.AddElement(getCustomerByOrderUnboundFunction);
            defaultContainer.AddFunctionImport(getCustomerByOrderUnboundFunction.Name, getCustomerByOrderUnboundFunction, new EdmEntitySetReferenceExpression(customerSet));

            // Function Import: Bound to Entity, Return Complex Value
            var getCustomerAddressUnboundFunction = new EdmFunction(ns, "GetCustomerAddress", addressTypeReference, false, null, true);
            getCustomerAddressUnboundFunction.AddParameter("customer", customerTypeReference);
            model.AddElement(getCustomerAddressUnboundFunction);
            defaultContainer.AddFunctionImport(getCustomerAddressUnboundFunction.Name, getCustomerAddressUnboundFunction);

            #endregion

            IEnumerable <EdmError> errors;
            model.Validate(out errors);
            if (errors.Any())
            {
                throw new SystemException("The model is not valid, please correct it");
            }

            return(model);
        }
        public ODataJsonLightInheritComplexCollectionWriterTests()
        {
            collectionStartWithoutSerializationInfo = new ODataResourceSet();

            collectionStartWithSerializationInfo = new ODataResourceSet();
            collectionStartWithSerializationInfo.SetSerializationInfo(new ODataResourceSerializationInfo {
                ExpectedTypeName = "ns.Address"
            });

            address = new ODataResource
            {
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "Street", Value = "1 Microsoft Way"
                    },
                    new ODataProperty {
                        Name = "Zipcode", Value = 98052
                    },
                    new ODataProperty {
                        Name = "State", Value = new ODataEnumValue("WA", "ns.StateEnum")
                    }
                }
            };
            items = new[] { address };

            derivedAddress = new ODataResource
            {
                Properties = new[]
                {
                    new ODataProperty {
                        Name = "Street", Value = "1 Microsoft Way"
                    },
                    new ODataProperty {
                        Name = "Zipcode", Value = 98052
                    },
                    new ODataProperty {
                        Name = "State", Value = new ODataEnumValue("WA", "ns.StateEnum")
                    },
                    new ODataProperty {
                        Name = "City", Value = "Shanghai"
                    }
                },
                TypeName = "ns.DerivedAddress"
            };

            derivedItems = new[] { derivedAddress };

            EdmComplexType addressType = new EdmComplexType("ns", "Address");

            addressType.AddProperty(new EdmStructuralProperty(addressType, "Street", EdmCoreModel.Instance.GetString(isNullable: true)));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "Zipcode", EdmCoreModel.Instance.GetInt32(isNullable: true)));
            var stateEnumType = new EdmEnumType("ns", "StateEnum", isFlags: true);

            stateEnumType.AddMember("IL", new EdmEnumMemberValue(1));
            stateEnumType.AddMember("WA", new EdmEnumMemberValue(2));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "State", new EdmEnumTypeReference(stateEnumType, true)));

            EdmComplexType derivedAddressType = new EdmComplexType("ns", "DerivedAddress", addressType, false);

            derivedAddressType.AddProperty(new EdmStructuralProperty(derivedAddressType, "City", EdmCoreModel.Instance.GetString(isNullable: true)));

            addressTypeReference        = new EdmComplexTypeReference(addressType, isNullable: false);
            derivedAddressTypeReference = new EdmComplexTypeReference(derivedAddressType, isNullable: false);
        }
예제 #25
0
        public static IEdmModel CreateModel(string ns)
        {
            EdmModel model            = new EdmModel();
            var      defaultContainer = new EdmEntityContainer(ns, "DefaultContainer");

            model.AddElement(defaultContainer);

            var orderDetail = new EdmComplexType(ns, "orderDetail");

            orderDetail.AddProperty(new EdmStructuralProperty(orderDetail, "amount", EdmCoreModel.Instance.GetDecimal(false)));
            orderDetail.AddProperty(new EdmStructuralProperty(orderDetail, "quantity", EdmCoreModel.Instance.GetInt32(false)));
            model.AddElement(orderDetail);

            var billingType = new EdmEnumType(ns, "billingType", EdmPrimitiveTypeKind.Int64, false);

            billingType.AddMember("fund", new EdmEnumMemberValue(0));
            billingType.AddMember("refund", new EdmEnumMemberValue(1));
            billingType.AddMember("chargeback", new EdmEnumMemberValue(2));
            model.AddElement(billingType);

            var category = new EdmEnumType(ns, "category", true);

            category.AddMember("toy", new EdmEnumMemberValue(1));
            category.AddMember("food", new EdmEnumMemberValue(2));
            category.AddMember("cloth", new EdmEnumMemberValue(4));
            category.AddMember("drink", new EdmEnumMemberValue(8));
            category.AddMember("computer", new EdmEnumMemberValue(16));
            model.AddElement(category);

            var namedObject = new EdmEntityType(ns, "namedObject");
            var idProperty  = new EdmStructuralProperty(namedObject, "id", EdmCoreModel.Instance.GetInt32(false));

            namedObject.AddProperty(idProperty);
            namedObject.AddKeys(idProperty);
            namedObject.AddProperty(new EdmStructuralProperty(namedObject, "name", EdmCoreModel.Instance.GetString(false)));
            model.AddElement(namedObject);

            var order = new EdmEntityType(ns, "order", namedObject);

            order.AddProperty(new EdmStructuralProperty(order, "description", EdmCoreModel.Instance.GetString(false)));
            order.AddProperty(new EdmStructuralProperty(order, "detail", new EdmComplexTypeReference(orderDetail, true)));
            order.AddProperty(new EdmStructuralProperty(order, "lineItems",
                                                        new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(false)))));
            order.AddProperty(new EdmStructuralProperty(order, "categories", new EdmEnumTypeReference(category, false)));
            model.AddElement(order);

            var billingRecord = new EdmEntityType(ns, "billingRecord", namedObject);

            billingRecord.AddProperty(new EdmStructuralProperty(billingRecord, "amount", EdmCoreModel.Instance.GetDecimal(false)));
            billingRecord.AddProperty(new EdmStructuralProperty(billingRecord, "type", new EdmEnumTypeReference(billingType, false)));
            model.AddElement(billingRecord);

            var store = new EdmEntityType(ns, "store", namedObject);

            store.AddProperty(new EdmStructuralProperty(store, "address", EdmCoreModel.Instance.GetString(false)));
            model.AddElement(store);

            var orders = new EdmEntitySet(defaultContainer, "orders", order);

            defaultContainer.AddElement(orders);

            var billingRecords = new EdmEntitySet(defaultContainer, "billingRecords", billingRecord);

            defaultContainer.AddElement(billingRecords);

            var myStore = new EdmSingleton(defaultContainer, "myStore", store);

            defaultContainer.AddElement(myStore);

            var order2billingRecord = order.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name               = "billingRecords",
                Target             = billingRecord,
                TargetMultiplicity = EdmMultiplicity.Many
            });

            ((EdmEntitySet)orders).AddNavigationTarget(order2billingRecord, billingRecords);

            IEnumerable <EdmError> errors = null;

            model.Validate(out errors);

            return(model);
        }
        public static IEdmModel CreateModel(string ns)
        {
            EdmModel model            = new EdmModel();
            var      defaultContainer = new EdmEntityContainer(ns, "DefaultContainer");

            model.AddElement(defaultContainer);

            var nameType = new EdmTypeDefinition(ns, "Name", EdmPrimitiveTypeKind.String);

            model.AddElement(nameType);

            var addressType = new EdmComplexType(ns, "Address");

            addressType.AddProperty(new EdmStructuralProperty(addressType, "Road", new EdmTypeDefinitionReference(nameType, false)));
            addressType.AddProperty(new EdmStructuralProperty(addressType, "City", EdmCoreModel.Instance.GetString(false)));
            model.AddElement(addressType);

            var personType       = new EdmEntityType(ns, "Person");
            var personIdProperty = new EdmStructuralProperty(personType, "PersonId", EdmCoreModel.Instance.GetInt32(false));

            personType.AddProperty(personIdProperty);
            personType.AddKeys(new IEdmStructuralProperty[] { personIdProperty });
            personType.AddProperty(new EdmStructuralProperty(personType, "FirstName", new EdmTypeDefinitionReference(nameType, false)));
            personType.AddProperty(new EdmStructuralProperty(personType, "LastName", new EdmTypeDefinitionReference(nameType, false)));
            personType.AddProperty(new EdmStructuralProperty(personType, "Address", new EdmComplexTypeReference(addressType, true)));
            personType.AddProperty(new EdmStructuralProperty(personType, "Descriptions", new EdmCollectionTypeReference(new EdmCollectionType(new EdmTypeDefinitionReference(nameType, false)))));

            model.AddElement(personType);
            var peopleSet = new EdmEntitySet(defaultContainer, "People", personType);

            defaultContainer.AddElement(peopleSet);

            var numberComboType = new EdmComplexType(ns, "NumberCombo");

            numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Small", model.GetUInt16(ns, false)));
            numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Middle", model.GetUInt32(ns, false)));
            numberComboType.AddProperty(new EdmStructuralProperty(numberComboType, "Large", model.GetUInt64(ns, false)));
            model.AddElement(numberComboType);

            var productType       = new EdmEntityType(ns, "Product");
            var productIdProperty = new EdmStructuralProperty(productType, "ProductId", model.GetUInt16(ns, false));

            productType.AddProperty(productIdProperty);
            productType.AddKeys(new IEdmStructuralProperty[] { productIdProperty });
            productType.AddProperty(new EdmStructuralProperty(productType, "Quantity", model.GetUInt32(ns, false)));
            productType.AddProperty(new EdmStructuralProperty(productType, "NullableUInt32", model.GetUInt32(ns, true)));
            productType.AddProperty(new EdmStructuralProperty(productType, "LifeTimeInSeconds", model.GetUInt64(ns, false)));
            productType.AddProperty(new EdmStructuralProperty(productType, "TheCombo", new EdmComplexTypeReference(numberComboType, true)));
            productType.AddProperty(new EdmStructuralProperty(productType, "LargeNumbers", new EdmCollectionTypeReference(new EdmCollectionType(model.GetUInt64(ns, false)))));

            model.AddElement(productType);
            var productsSet = new EdmEntitySet(defaultContainer, "Products", productType);

            defaultContainer.AddElement(productsSet);

            //Bound Function: bound to entity, return defined type
            var getFullNameFunction = new EdmFunction(ns, "GetFullName", new EdmTypeDefinitionReference(nameType, false), true, null, false);

            getFullNameFunction.AddParameter("person", new EdmEntityTypeReference(personType, false));
            getFullNameFunction.AddParameter("nickname", new EdmTypeDefinitionReference(nameType, false));
            model.AddElement(getFullNameFunction);

            //Bound Action: bound to entity, return UInt64
            var extendLifeTimeAction = new EdmAction(ns, "ExtendLifeTime", model.GetUInt64(ns, false), true, null);

            extendLifeTimeAction.AddParameter("product", new EdmEntityTypeReference(productType, false));
            extendLifeTimeAction.AddParameter("seconds", model.GetUInt32(ns, false));
            model.AddElement(extendLifeTimeAction);

            //UnBound Action: ResetDataSource
            var resetDataSourceAction = new EdmAction(ns, "ResetDataSource", null, false, null);

            model.AddElement(resetDataSourceAction);
            defaultContainer.AddActionImport(resetDataSourceAction);

            IEnumerable <EdmError> errors = null;

            model.Validate(out errors);

            return(model);
        }
예제 #27
0
        public static IEdmModel CreateTripPinServiceModel(string ns)
        {
            EdmModel model            = new EdmModel();
            var      defaultContainer = new EdmEntityContainer(ns, "DefaultContainer");

            model.AddElement(defaultContainer);

            var genderType = new EdmEnumType(ns, "PersonGender", isFlags: false);

            genderType.AddMember("Male", new EdmIntegerConstant(0));
            genderType.AddMember("Female", new EdmIntegerConstant(1));
            genderType.AddMember("Unknown", new EdmIntegerConstant(2));
            model.AddElement(genderType);

            var cityType = new EdmComplexType(ns, "City");

            cityType.AddProperty(new EdmStructuralProperty(cityType, "CountryRegion", EdmCoreModel.Instance.GetString(false)));
            cityType.AddProperty(new EdmStructuralProperty(cityType, "Name", EdmCoreModel.Instance.GetString(false)));
            cityType.AddProperty(new EdmStructuralProperty(cityType, "Region", EdmCoreModel.Instance.GetString(false)));
            model.AddElement(cityType);

            var locationType = new EdmComplexType(ns, "Location", null, false, true);

            locationType.AddProperty(new EdmStructuralProperty(locationType, "Address", EdmCoreModel.Instance.GetString(false)));
            locationType.AddProperty(new EdmStructuralProperty(locationType, "City", new EdmComplexTypeReference(cityType, false)));
            model.AddElement(locationType);

            var eventLocationType = new EdmComplexType(ns, "EventLocation", locationType, false, true);

            eventLocationType.AddProperty(new EdmStructuralProperty(eventLocationType, "BuildingInfo", EdmCoreModel.Instance.GetString(true)));
            model.AddElement(eventLocationType);

            var airportLocationType = new EdmComplexType(ns, "AirportLocation", locationType, false, true);

            airportLocationType.AddProperty(new EdmStructuralProperty(airportLocationType, "Loc", EdmCoreModel.Instance.GetSpatial(EdmPrimitiveTypeKind.GeographyPoint, false)));
            model.AddElement(airportLocationType);

            var photoType       = new EdmEntityType(ns, "Photo", null, false, false, true);
            var photoIdProperty = new EdmStructuralProperty(photoType, "Id", EdmCoreModel.Instance.GetInt64(false));

            photoType.AddProperty(photoIdProperty);
            photoType.AddKeys(photoIdProperty);
            photoType.AddProperty(new EdmStructuralProperty(photoType, "Name", EdmCoreModel.Instance.GetString(true)));
            model.AddElement(photoType);

            var photoSet = new EdmEntitySet(defaultContainer, "Photos", photoType);

            defaultContainer.AddElement(photoSet);

            var personType       = new EdmEntityType(ns, "Person", null, false, /* isOpen */ true);
            var personIdProperty = new EdmStructuralProperty(personType, "UserName", EdmCoreModel.Instance.GetString(false));

            personType.AddProperty(personIdProperty);
            personType.AddKeys(personIdProperty);
            personType.AddProperty(new EdmStructuralProperty(personType, "FirstName", EdmCoreModel.Instance.GetString(false)));
            personType.AddProperty(new EdmStructuralProperty(personType, "LastName", EdmCoreModel.Instance.GetString(false)));
            personType.AddProperty(new EdmStructuralProperty(personType, "Emails", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(true)))));
            personType.AddProperty(new EdmStructuralProperty(personType, "AddressInfo", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(locationType, true)))));
            personType.AddProperty(new EdmStructuralProperty(personType, "Gender", new EdmEnumTypeReference(genderType, true)));
            personType.AddProperty(new EdmStructuralProperty(personType, "Concurrency", EdmCoreModel.Instance.GetInt64(false)));
            model.AddElement(personType);

            var personSet = new EdmEntitySet(defaultContainer, "People", personType);

            defaultContainer.AddElement(personSet);

            var airlineType     = new EdmEntityType(ns, "Airline");
            var airlineCodeProp = new EdmStructuralProperty(airlineType, "AirlineCode", EdmCoreModel.Instance.GetString(false));

            airlineType.AddKeys(airlineCodeProp);
            airlineType.AddProperty(airlineCodeProp);
            airlineType.AddProperty(new EdmStructuralProperty(airlineType, "Name", EdmCoreModel.Instance.GetString(false)));

            model.AddElement(airlineType);
            var airlineSet = new EdmEntitySet(defaultContainer, "Airlines", airlineType);

            defaultContainer.AddElement(airlineSet);

            var airportType   = new EdmEntityType(ns, "Airport");
            var airportIdType = new EdmStructuralProperty(airportType, "IcaoCode", EdmCoreModel.Instance.GetString(false));

            airportType.AddProperty(airportIdType);
            airportType.AddKeys(airportIdType);
            airportType.AddProperty(new EdmStructuralProperty(airportType, "Name", EdmCoreModel.Instance.GetString(false)));
            airportType.AddProperty(new EdmStructuralProperty(airportType, "IataCode", EdmCoreModel.Instance.GetString(false)));
            airportType.AddProperty(new EdmStructuralProperty(airportType, "Location", new EdmComplexTypeReference(airportLocationType, false)));
            model.AddElement(airportType);

            var airportSet = new EdmEntitySet(defaultContainer, "Airports", airportType);

            defaultContainer.AddElement(airportSet);

            var planItemType   = new EdmEntityType(ns, "PlanItem");
            var planItemIdType = new EdmStructuralProperty(planItemType, "PlanItemId", EdmCoreModel.Instance.GetInt32(false));

            planItemType.AddProperty(planItemIdType);
            planItemType.AddKeys(planItemIdType);
            planItemType.AddProperty(new EdmStructuralProperty(planItemType, "ConfirmationCode", EdmCoreModel.Instance.GetString(true)));
            planItemType.AddProperty(new EdmStructuralProperty(planItemType, "StartsAt", EdmCoreModel.Instance.GetDateTimeOffset(true)));
            planItemType.AddProperty(new EdmStructuralProperty(planItemType, "EndsAt", EdmCoreModel.Instance.GetDateTimeOffset(true)));
            planItemType.AddProperty(new EdmStructuralProperty(planItemType, "Duration", EdmCoreModel.Instance.GetDuration(true)));
            model.AddElement(planItemType);

            var publicTransportationType = new EdmEntityType(ns, "PublicTransportation", planItemType);

            publicTransportationType.AddProperty(new EdmStructuralProperty(publicTransportationType, "SeatNumber", EdmCoreModel.Instance.GetString(true)));
            model.AddElement(publicTransportationType);

            var flightType       = new EdmEntityType(ns, "Flight", publicTransportationType);
            var flightNumberType = new EdmStructuralProperty(flightType, "FlightNumber", EdmCoreModel.Instance.GetString(false));

            flightType.AddProperty(flightNumberType);
            model.AddElement(flightType);

            var eventType = new EdmEntityType(ns, "Event", planItemType, false, true);

            eventType.AddProperty(new EdmStructuralProperty(eventType, "Description", EdmCoreModel.Instance.GetString(true)));
            eventType.AddProperty(new EdmStructuralProperty(eventType, "OccursAt", new EdmComplexTypeReference(eventLocationType, false)));
            model.AddElement(eventType);

            var tripType   = new EdmEntityType(ns, "Trip");
            var tripIdType = new EdmStructuralProperty(tripType, "TripId", EdmCoreModel.Instance.GetInt32(false));

            tripType.AddProperty(tripIdType);
            tripType.AddKeys(tripIdType);
            tripType.AddProperty(new EdmStructuralProperty(tripType, "ShareId", EdmCoreModel.Instance.GetGuid(true)));
            tripType.AddProperty(new EdmStructuralProperty(tripType, "Description", EdmCoreModel.Instance.GetString(true)));
            tripType.AddProperty(new EdmStructuralProperty(tripType, "Name", EdmCoreModel.Instance.GetString(false)));
            tripType.AddProperty(new EdmStructuralProperty(tripType, "Budget", EdmCoreModel.Instance.GetSingle(false)));
            tripType.AddProperty(new EdmStructuralProperty(tripType, "StartsAt", EdmCoreModel.Instance.GetDateTimeOffset(false)));
            tripType.AddProperty(new EdmStructuralProperty(tripType, "EndsAt", EdmCoreModel.Instance.GetDateTimeOffset(false)));
            tripType.AddProperty(new EdmStructuralProperty(tripType, "Tags", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(false)))));
            model.AddElement(tripType);

            var friendsnNavigation = personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "Friends",
                Target             = personType,
                TargetMultiplicity = EdmMultiplicity.Many
            });

            var personTripNavigation = personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "Trips",
                Target             = tripType,
                TargetMultiplicity = EdmMultiplicity.Many,
                ContainsTarget     = true
            });

            var personPhotoNavigation = personType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "Photo",
                Target             = photoType,
                TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
            });

            var tripPhotosNavigation = tripType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "Photos",
                Target             = photoType,
                TargetMultiplicity = EdmMultiplicity.Many,
            });

            var tripItemNavigation = tripType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "PlanItems",
                Target             = planItemType,
                ContainsTarget     = true,
                TargetMultiplicity = EdmMultiplicity.Many
            });

            var flightFromAirportNavigation = flightType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "From",
                Target             = airportType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            var flightToAirportNavigation = flightType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "To",
                Target             = airportType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            var flightAirlineNavigation = flightType.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo()
            {
                Name               = "Airline",
                Target             = airlineType,
                TargetMultiplicity = EdmMultiplicity.One
            });

            var me = new EdmSingleton(defaultContainer, "Me", personType);

            defaultContainer.AddElement(me);

            personSet.AddNavigationTarget(friendsnNavigation, personSet);
            me.AddNavigationTarget(friendsnNavigation, personSet);

            personSet.AddNavigationTarget(flightAirlineNavigation, airlineSet);
            me.AddNavigationTarget(flightAirlineNavigation, airlineSet);

            personSet.AddNavigationTarget(flightFromAirportNavigation, airportSet);
            me.AddNavigationTarget(flightFromAirportNavigation, airportSet);

            personSet.AddNavigationTarget(flightToAirportNavigation, airportSet);
            me.AddNavigationTarget(flightToAirportNavigation, airportSet);

            personSet.AddNavigationTarget(personPhotoNavigation, photoSet);
            me.AddNavigationTarget(personPhotoNavigation, photoSet);

            personSet.AddNavigationTarget(tripPhotosNavigation, photoSet);
            me.AddNavigationTarget(tripPhotosNavigation, photoSet);

            var getFavoriteAirlineFunction = new EdmFunction(ns, "GetFavoriteAirline",
                                                             new EdmEntityTypeReference(airlineType, false), true,
                                                             new EdmPathExpression("person/Trips/PlanItems/Microsoft.OData.SampleService.Models.TripPin.Flight/Airline"), true);

            getFavoriteAirlineFunction.AddParameter("person", new EdmEntityTypeReference(personType, false));
            model.AddElement(getFavoriteAirlineFunction);

            var getInvolvedPeopleFunction = new EdmFunction(ns, "GetInvolvedPeople",
                                                            new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(personType, false))), true, null, true);

            getInvolvedPeopleFunction.AddParameter("trip", new EdmEntityTypeReference(tripType, false));
            model.AddElement(getInvolvedPeopleFunction);

            var getFriendsTripsFunction = new EdmFunction(ns, "GetFriendsTrips",
                                                          new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(tripType, false))),
                                                          true, new EdmPathExpression("person/Friends/Trips"), true);

            getFriendsTripsFunction.AddParameter("person", new EdmEntityTypeReference(personType, false));
            getFriendsTripsFunction.AddParameter("userName", EdmCoreModel.Instance.GetString(false));
            model.AddElement(getFriendsTripsFunction);

            var getNearestAirport = new EdmFunction(ns, "GetNearestAirport",
                                                    new EdmEntityTypeReference(airportType, false),
                                                    false, null, true);

            getNearestAirport.AddParameter("lat", EdmCoreModel.Instance.GetDouble(false));
            getNearestAirport.AddParameter("lon", EdmCoreModel.Instance.GetDouble(false));
            model.AddElement(getNearestAirport);
            var getNearestAirportFunctionImport = (IEdmFunctionImport)defaultContainer.AddFunctionImport("GetNearestAirport", getNearestAirport, new EdmEntitySetReferenceExpression(airportSet), true);

            var resetDataSourceAction = new EdmAction(ns, "ResetDataSource", null, false, null);

            model.AddElement(resetDataSourceAction);
            defaultContainer.AddActionImport(resetDataSourceAction);

            var shareTripAction = new EdmAction(ns, "ShareTrip", null, true, null);

            shareTripAction.AddParameter("person", new EdmEntityTypeReference(personType, false));
            shareTripAction.AddParameter("userName", EdmCoreModel.Instance.GetString(false));
            shareTripAction.AddParameter("tripId", EdmCoreModel.Instance.GetInt32(false));
            model.AddElement(shareTripAction);

            model.SetDescriptionAnnotation(defaultContainer, "TripPin service is a sample service for OData V4.");
            model.SetOptimisticConcurrencyAnnotation(personSet, personType.StructuralProperties().Where(p => p.Name == "Concurrency"));
            // TODO: currently singleton does not support ETag feature
            // model.SetOptimisticConcurrencyAnnotation(me, personType.StructuralProperties().Where(p => p.Name == "Concurrency"));
            model.SetResourcePathCoreAnnotation(personSet, "People");
            model.SetResourcePathCoreAnnotation(me, "Me");
            model.SetResourcePathCoreAnnotation(airlineSet, "Airlines");
            model.SetResourcePathCoreAnnotation(airportSet, "Airports");
            model.SetResourcePathCoreAnnotation(photoSet, "Photos");
            model.SetResourcePathCoreAnnotation(getNearestAirportFunctionImport, "Microsoft.OData.SampleService.Models.TripPin.GetNearestAirport");
            model.SetDereferenceableIDsCoreAnnotation(defaultContainer, true);
            model.SetConventionalIDsCoreAnnotation(defaultContainer, true);
            model.SetPermissionsCoreAnnotation(personType.FindProperty("UserName"), CorePermission.Read);
            model.SetPermissionsCoreAnnotation(airlineType.FindProperty("AirlineCode"), CorePermission.Read);
            model.SetPermissionsCoreAnnotation(airportType.FindProperty("IcaoCode"), CorePermission.Read);
            model.SetPermissionsCoreAnnotation(planItemType.FindProperty("PlanItemId"), CorePermission.Read);
            model.SetPermissionsCoreAnnotation(tripType.FindProperty("TripId"), CorePermission.Read);
            model.SetPermissionsCoreAnnotation(photoType.FindProperty("Id"), CorePermission.Read);
            model.SetImmutableCoreAnnotation(airportType.FindProperty("IataCode"), true);
            model.SetComputedCoreAnnotation(personType.FindProperty("Concurrency"), true);
            model.SetAcceptableMediaTypesCoreAnnotation(photoType, new[] { "image/jpeg" });
            model.SetConformanceLevelCapabilitiesAnnotation(defaultContainer, CapabilitiesConformanceLevelType.Advanced);
            model.SetSupportedFormatsCapabilitiesAnnotation(defaultContainer, new[] { "application/json;odata.metadata=full;IEEE754Compatible=false;odata.streaming=true", "application/json;odata.metadata=minimal;IEEE754Compatible=false;odata.streaming=true", "application/json;odata.metadata=none;IEEE754Compatible=false;odata.streaming=true" });
            model.SetAsynchronousRequestsSupportedCapabilitiesAnnotation(defaultContainer, true);
            model.SetBatchContinueOnErrorSupportedCapabilitiesAnnotation(defaultContainer, false);
            model.SetNavigationRestrictionsCapabilitiesAnnotation(personSet, CapabilitiesNavigationType.None, new[] { new Tuple <IEdmNavigationProperty, CapabilitiesNavigationType>(friendsnNavigation, CapabilitiesNavigationType.Recursive) });
            model.SetFilterFunctionsCapabilitiesAnnotation(defaultContainer, new[] { "contains", "endswith", "startswith", "length", "indexof", "substring", "tolower", "toupper", "trim", "concat", "year", "month", "day", "hour", "minute", "second", "round", "floor", "ceiling", "cast", "isof" });
            model.SetSearchRestrictionsCapabilitiesAnnotation(personSet, true, CapabilitiesSearchExpressions.None);
            model.SetSearchRestrictionsCapabilitiesAnnotation(airlineSet, true, CapabilitiesSearchExpressions.None);
            model.SetSearchRestrictionsCapabilitiesAnnotation(airportSet, true, CapabilitiesSearchExpressions.None);
            model.SetSearchRestrictionsCapabilitiesAnnotation(photoSet, true, CapabilitiesSearchExpressions.None);
            model.SetInsertRestrictionsCapabilitiesAnnotation(personSet, true, new[] { personTripNavigation, friendsnNavigation });
            model.SetInsertRestrictionsCapabilitiesAnnotation(airlineSet, true, null);
            model.SetInsertRestrictionsCapabilitiesAnnotation(airportSet, false, null);
            model.SetInsertRestrictionsCapabilitiesAnnotation(photoSet, true, null);
            // TODO: model.SetUpdateRestrictionsCapabilitiesAnnotation();
            model.SetDeleteRestrictionsCapabilitiesAnnotation(airportSet, false, null);
            model.SetISOCurrencyMeasuresAnnotation(tripType.FindProperty("Budget"), "USD");
            model.SetScaleMeasuresAnnotation(tripType.FindProperty("Budget"), 2);

            return(model);
        }