Exemplo n.º 1
0
        static ExtensionTestBase()
        {
            var model = new EdmModel();

            Model = model;

            var person   = new EdmEntityType("TestNS", "Person");
            var personId = person.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32);

            PersonNameProp = person.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            PersonPen      = person.AddStructuralProperty("pen", EdmPrimitiveTypeKind.Binary);
            person.AddKeys(personId);
            PersonType = person;

            var address = new EdmComplexType("TestNS", "Address");

            AddrType        = address;
            ZipCodeProperty = address.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            AddrProperty    = person.AddStructuralProperty("Addr", new EdmComplexTypeReference(address, false));

            var pencil   = new EdmEntityType("TestNS", "Pencil");
            var pencilId = pencil.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32);

            PencilId = pencilId;
            var pid = pencil.AddStructuralProperty("Pid", EdmPrimitiveTypeKind.Int32);

            pencil.AddKeys(pencilId);

            var starPencil = new EdmEntityType("TestNS", "StarPencil", pencil);

            model.AddElement(starPencil);
            var starPencilUpper = new EdmEntityType("TestNS", "STARPENCIL");

            model.AddElement(starPencilUpper);
            StarPencil = starPencil;

            var navInfo = new EdmNavigationPropertyInfo()
            {
                Name               = "Pen",
                ContainsTarget     = false,
                Target             = pencil,
                TargetMultiplicity = EdmMultiplicity.One
            };

            PersonNavPen = person.AddUnidirectionalNavigation(navInfo);

            var navInfo2 = new EdmNavigationPropertyInfo()
            {
                Name               = "Pen2",
                ContainsTarget     = true,
                Target             = pencil,
                TargetMultiplicity = EdmMultiplicity.One
            };

            PersonNavPen2 = person.AddUnidirectionalNavigation(navInfo2);

            var container = new EdmEntityContainer("Default", "Con1");
            var personSet = container.AddEntitySet("People", person);

            PencilSet  = container.AddEntitySet("PencilSet", pencil);
            Boss       = container.AddSingleton("Boss", person);
            Bajie      = container.AddSingleton("Bajie", pencil);
            BajieUpper = container.AddSingleton("BAJIE", pencil);
            personSet.AddNavigationTarget(PersonNavPen, PencilSet);
            PeopleSet = personSet;

            var pencilSetUpper = container.AddEntitySet("PENCILSET", pencil);

            PencilSetUpper = pencilSetUpper;

            var         pencilRef   = new EdmEntityTypeReference(pencil, false);
            EdmFunction findPencil1 = new EdmFunction("TestNS", "FindPencil", pencilRef, true, null, false);

            findPencil1.AddParameter("qid", new EdmEntityTypeReference(PersonType, false));
            model.AddElement(findPencil1);
            FindPencil1P = findPencil1;

            EdmFunction findPencil = new EdmFunction("TestNS", "FindPencil", pencilRef, true, null, false);

            findPencil.AddParameter("qid", new EdmEntityTypeReference(PersonType, false));
            findPencil.AddParameter("pid", EdmCoreModel.Instance.GetInt32(false));
            model.AddElement(findPencil);
            FindPencil2P = findPencil;

            EdmFunction findPencils = new EdmFunction("TestNS", "FindPencils", new EdmCollectionTypeReference(new EdmCollectionType(pencilRef)), true, null, false);

            findPencils.AddParameter("qid", new EdmEntityTypeReference(PersonType, false));
            model.AddElement(findPencils);
            FindPencils = findPencils;

            EdmFunction findPencilsCon = new EdmFunction("TestNS", "FindPencilsCon", new EdmCollectionTypeReference(new EdmCollectionType(pencilRef)), true, null, false);

            FindPencilsCon = findPencilsCon;
            findPencilsCon.AddParameter("qid", new EdmEntityTypeReference(PersonType, false));
            model.AddElement(findPencilsCon);

            EdmFunction findPencilsConUpper = new EdmFunction("TestNS", "FindPencilsCON", new EdmCollectionTypeReference(new EdmCollectionType(pencilRef)), true, null, false);

            FindPencilsConUpper = findPencilsConUpper;
            findPencilsConUpper.AddParameter("qid", new EdmEntityTypeReference(PersonType, false));
            model.AddElement(findPencilsConUpper);

            EdmFunction findPencilsConNT = new EdmFunction("TestNT", "FindPencilsCon", new EdmCollectionTypeReference(new EdmCollectionType(pencilRef)), true, null, false);

            FindPencilsConNT = findPencilsConNT;
            findPencilsConNT.AddParameter("qid", new EdmEntityTypeReference(PersonType, false));
            model.AddElement(findPencilsConNT);

            ChangeZip = new EdmAction("TestNS", "ChangeZip", null, true, null);
            ChangeZip.AddParameter("address", new EdmComplexTypeReference(address, false));
            ChangeZip.AddParameter("newZip", EdmCoreModel.Instance.GetString(false));
            model.AddElement(ChangeZip);

            GetZip = new EdmFunction("TestNS", "GetZip", EdmCoreModel.Instance.GetString(false), true, null, true);
            GetZip.AddParameter("address", new EdmComplexTypeReference(address, false));
            model.AddElement(GetZip);

            model.AddElement(person);
            model.AddElement(address);
            model.AddElement(pencil);
            model.AddElement(container);

            var feed = new EdmAction("TestNS", "Feed", null);

            Feed = feed;
            feed.AddParameter("pid", EdmCoreModel.Instance.GetInt32(false));

            FeedImport         = container.AddActionImport("Feed", feed);
            FeedConImport      = container.AddActionImport("FeedCon", feed);
            FeedConUpperImport = container.AddActionImport("FeedCON", feed);

            var pet = new EdmEntityType("TestNS", "Pet");

            PetType = pet;
            model.AddElement(pet);
            var key1 = pet.AddStructuralProperty("key1", EdmCoreModel.Instance.GetInt32(false));
            var key2 = pet.AddStructuralProperty("key2", EdmCoreModel.Instance.GetString(false));

            pet.AddKeys(key1, key2);
            var petSet = container.AddEntitySet("PetSet", pet);

            var petCon = new EdmEntityType("TestNS", "PetCon");

            model.AddElement(petCon);
            var key1Con = pet.AddStructuralProperty("key", EdmCoreModel.Instance.GetInt32(false));
            var key2Con = pet.AddStructuralProperty("KEY", EdmCoreModel.Instance.GetString(false));

            petCon.AddKeys(key1Con, key2Con);
            var petSetCon = container.AddEntitySet("PetSetCon", petCon);

            EdmEnumType colorType = new EdmEnumType("TestNS", "Color");

            Color = colorType;
            colorType.AddMember("Red", new EdmIntegerConstant(1L));
            colorType.AddMember("Blue", new EdmIntegerConstant(2L));
            model.AddElement(colorType);
            var moonType = new EdmEntityType("TestNS", "Moon");

            MoonType = moonType;
            var color = moonType.AddStructuralProperty("color", new EdmEnumTypeReference(colorType, false));

            moonType.AddKeys(color);
            model.AddElement(moonType);
            container.AddEntitySet("MoonSet", moonType);

            var moonType2 = new EdmEntityType("TestNS", "Moon2");

            MoonType2 = moonType2;
            var color2 = moonType2.AddStructuralProperty("color", new EdmEnumTypeReference(colorType, false));
            var id     = moonType2.AddStructuralProperty("id", EdmCoreModel.Instance.GetInt32(false));

            moonType2.AddKeys(color2, id);
            model.AddElement(moonType2);
            container.AddEntitySet("MoonSet2", moonType2);

            EdmFunction findPencilCon = new EdmFunction("TestNS", "FindPencilCon", pencilRef, true, null, false);

            FindPencilCon = findPencilCon;
            findPencilCon.AddParameter("qid", new EdmEntityTypeReference(PersonType, false));
            findPencilCon.AddParameter("pid", EdmCoreModel.Instance.GetInt32(false));
            findPencilCon.AddParameter("PID", EdmCoreModel.Instance.GetInt32(false));
            model.AddElement(findPencilCon);

            EdmFunction getColorCmyk = new EdmFunction("TestNS", "GetColorCmyk", EdmCoreModel.Instance.GetString(false));

            getColorCmyk.AddParameter("co", new EdmEnumTypeReference(colorType, true));
            GetColorCmyk = getColorCmyk;
            model.AddElement(getColorCmyk);
            GetColorCmykImport = container.AddFunctionImport("GetColorCmykImport", getColorCmyk);

            EdmFunction getMixedColor = new EdmFunction("TestNS", "GetMixedColor", EdmCoreModel.Instance.GetString(false));

            getMixedColor.AddParameter("co", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEnumTypeReference(colorType, true))));
            model.AddElement(getMixedColor);
            GetMixedColor = container.AddFunctionImport("GetMixedColorImport", getMixedColor);
        }
Exemplo n.º 2
0
        private static EdmEntityObjectCollection GetCustomers()
        {
            if (_untypedSimpleOpenCustormers != null)
            {
                return(_untypedSimpleOpenCustormers);
            }
            EdmEntityType customerType = new EdmEntityType("NS", "UntypedSimpleOpenCustomer", null, false, true);

            customerType.AddKeys(customerType.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32));
            EdmEntityObject customer = new EdmEntityObject(customerType);

            customer.TrySetPropertyValue("CustomerId", 1);

            //Add Numbers primitive collection property
            customer.TrySetPropertyValue("DeclaredNumbers", new[] { 1, 2 });

            //Add Color, Colors enum(collection) property
            EdmEnumType colorType = new EdmEnumType("NS", "Color");

            colorType.AddMember(new EdmEnumMember(colorType, "Red", new EdmIntegerConstant(0)));

            EdmEnumObject color  = new EdmEnumObject(colorType, "Red");
            EdmEnumObject color2 = new EdmEnumObject(colorType, "0");
            EdmEnumObject color3 = new EdmEnumObject(colorType, "Red");

            customer.TrySetPropertyValue("Color", color);

            List <IEdmEnumObject> colorList = new List <IEdmEnumObject>();

            colorList.Add(color);
            colorList.Add(color2);
            colorList.Add(color3);
            IEdmCollectionTypeReference enumCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(colorType.ToEdmTypeReference(false)));
            EdmEnumObjectCollection     colors             = new EdmEnumObjectCollection(enumCollectionType, colorList);

            customer.TrySetPropertyValue("Colors", colors);
            customer.TrySetPropertyValue("DeclaredColors", colors);

            //Add Addresses complex(collection) property
            EdmComplexType addressType = new EdmComplexType("NS", "Address", null, false, true);

            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);

            EdmComplexObject address = new EdmComplexObject(addressType);

            address.TrySetPropertyValue("Street", "No1");
            EdmComplexObject address2 = new EdmComplexObject(addressType);

            address2.TrySetPropertyValue("Street", "No2");

            List <IEdmComplexObject> addressList = new List <IEdmComplexObject>();

            addressList.Add(address);
            addressList.Add(address2);
            IEdmCollectionTypeReference complexCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(addressType.ToEdmTypeReference(false)));
            EdmComplexObjectCollection  addresses             = new EdmComplexObjectCollection(complexCollectionType, addressList);

            customer.TrySetPropertyValue("DeclaredAddresses", addresses);

            EdmEntityObjectCollection customers = new EdmEntityObjectCollection(new EdmCollectionTypeReference(new EdmCollectionType(customerType.ToEdmTypeReference(false))));

            customers.Add(customer);
            _untypedSimpleOpenCustormers = customers;
            return(_untypedSimpleOpenCustormers);
        }
        internal void WriteEnumTypeElementHeader(EdmEnumType enumType)
        {
            _xmlWriter.WriteStartElement(CsdlConstants.Element_EnumType);
            _xmlWriter.WriteAttributeString(CsdlConstants.Attribute_Name, enumType.Name);
            _xmlWriter.WriteAttributeString(
                CsdlConstants.Attribute_IsFlags, GetLowerCaseStringFromBoolValue(enumType.IsFlags));

            if (enumType.UnderlyingType != null)
            {
                _xmlWriter.WriteAttributeString(
                    CsdlConstants.Attribute_UnderlyingType,
                    GetTypeNameFromPrimitiveTypeKind(enumType.UnderlyingType.PrimitiveTypeKind));
            }
        }
 public void ParseEnumFlags()
 {
     var enumType = new EdmEnumType()
     {
         Name = "Namespace.Color", 
         IsFlags = true,
         Members = new[]
         {
             new EdmEnumMember { Name = "Black", EvaluatedValue = 1},
             new EdmEnumMember { Name = "White", EvaluatedValue = 2},
         }
     };
     var propertyType = new EdmEnumPropertyType() { Type = enumType };
     Assert.Equal(3, _parser.ParseValue("Namespace.Color'Black,White'", propertyType));
 }
Exemplo n.º 5
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 EdmEnumMemberValue(0));
            genderType.AddMember("Female", new EdmEnumMemberValue(1));
            genderType.AddMember("Unknown", new EdmEnumMemberValue(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);

            var path = new EdmPathExpression("Trips/PlanItems/Microsoft.OData.SampleService.Models.TripPin.Flight/Airline");

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

            path = new EdmPathExpression("Trips/PlanItems/Microsoft.OData.SampleService.Models.TripPin.Flight/From");
            personSet.AddNavigationTarget(flightFromAirportNavigation, airportSet, path);
            me.AddNavigationTarget(flightFromAirportNavigation, airportSet, path);

            path = new EdmPathExpression("Trips/PlanItems/Microsoft.OData.SampleService.Models.TripPin.Flight/To");
            personSet.AddNavigationTarget(flightToAirportNavigation, airportSet, path);
            me.AddNavigationTarget(flightToAirportNavigation, airportSet, path);

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

            path = new EdmPathExpression("Trips/Photos");
            personSet.AddNavigationTarget(tripPhotosNavigation, photoSet, path);
            me.AddNavigationTarget(tripPhotosNavigation, photoSet, path);

            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 EdmPathExpression("Airports"), 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);
        }
 protected override void VisitEdmEnumType(EdmEnumType item)
 {
     _schemaWriter.WriteEnumTypeElementHeader(item);
     base.VisitEdmEnumType(item);
     _schemaWriter.WriteEndElement();
 }
Exemplo n.º 7
0
 protected virtual void VisitEnumMembers(EdmEnumType enumType, IEnumerable<EdmEnumTypeMember> members)
 {
     VisitCollection(members, VisitEdmEnumTypeMember);
 }
Exemplo n.º 8
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 EdmIntegerConstant(0));
            billingType.AddMember("refund", new EdmIntegerConstant(1));
            billingType.AddMember("chargeback", new EdmIntegerConstant(2));
            model.AddElement(billingType);

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

            category.AddMember("toy", new EdmIntegerConstant(1));
            category.AddMember("food", new EdmIntegerConstant(2));
            category.AddMember("cloth", new EdmIntegerConstant(4));
            category.AddMember("drink", new EdmIntegerConstant(8));
            category.AddMember("computer", new EdmIntegerConstant(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);
        }
Exemplo n.º 9
0
        public void Init()
        {
            this.serverModel          = new EdmModel();
            this.serverEntityType     = new EdmEntityType("Server.NS", "ServerEntityType");
            this.serverEntityTypeName = "Server.NS.ServerEntityType";
            this.serverModel.AddElement(this.serverEntityType);
            this.serverEntityType.AddKeys(this.serverEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));

            var addressType = new EdmComplexType("Server.NS", "Address");

            addressType.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            this.serverEntityType.AddStructuralProperty("Address", new EdmComplexTypeReference(addressType, true));
            this.serverComplexTypeName = "Server.NS.Address";

            var homeAddressType = new EdmComplexType("Server.NS", "HomeAddress", addressType);

            homeAddressType.AddStructuralProperty("FamilyName", EdmPrimitiveTypeKind.String);
            this.serverComplexTypeName = "Server.NS.HomeAddress";

            var colorType = new EdmEnumType("Server.NS", "Color");

            colorType.AddMember(new EdmEnumMember(colorType, "Red", new EdmIntegerConstant(1)));
            colorType.AddMember(new EdmEnumMember(colorType, "Blue", new EdmIntegerConstant(2)));
            colorType.AddMember(new EdmEnumMember(colorType, "Green", new EdmIntegerConstant(3)));
            this.serverEntityType.AddStructuralProperty("Color", new EdmEnumTypeReference(colorType, false));

            this.serverEntityType.AddStructuralProperty("Nicknames", new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetInt32(false))));
            this.serverEntityType.AddStructuralProperty("OtherAddresses", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(addressType, false))));

            this.clientModel          = new ClientEdmModel(ODataProtocolVersion.V4);
            this.clientTypeAnnotation = this.clientModel.GetClientTypeAnnotation(typeof(TestClientEntityType));

            this.context = new DataServiceContext(new Uri("http://temp/"), ODataProtocolVersion.V4, this.clientModel);
            this.context.Format.UseJson(this.serverModel);

            this.testSubject = new ODataPropertyConverter(new RequestInfo(this.context));

            this.context.Format.UseJson(this.serverModel);
            this.context.ResolveName = t =>
            {
                if (t == typeof(TestClientEntityType))
                {
                    return(this.serverEntityTypeName);
                }

                if (t == typeof(Address))
                {
                    return(this.serverComplexTypeName);
                }

                return(null);
            };
            this.entity = new TestClientEntityType
            {
                Id             = 1,
                Name           = "foo",
                Number         = 1.23f,
                Address        = new Address(),
                OpenAddress    = new Address(),
                Nicknames      = new string[0],
                OtherAddresses = new[] { new Address() },
                Color          = 0
            };
            this.entityWithDerivedComplexProperty = new TestClientEntityType
            {
                Id             = 1,
                Name           = "foo",
                Number         = 1.23f,
                Address        = new HomeAddress(),
                OpenAddress    = new Address(),
                Nicknames      = new string[0],
                OtherAddresses = new[] { new Address() }
            };
            this.entityWithDerivedComplexInCollection = new TestClientEntityType
            {
                Id             = 1,
                Name           = "foo",
                Number         = 1.23f,
                Address        = new HomeAddress(),
                OpenAddress    = new Address(),
                Nicknames      = new string[0],
                OtherAddresses = new[] { new Address(), new HomeAddress() }
            };
        }
        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);

            var orderDetailType = new EdmComplexType(ns, "OrderDetail");
            orderDetailType.AddProperty(new EdmStructuralProperty(orderDetailType, "Quantity", EdmCoreModel.Instance.GetInt32(false)));
            orderDetailType.AddProperty(new EdmStructuralProperty(orderDetailType, "UnitPrice", EdmCoreModel.Instance.GetSingle(false)));
            model.AddElement(orderDetailType);

            var infoFromCustomerType = new EdmComplexType(ns, "InfoFromCustomer");
            infoFromCustomerType.AddProperty(new EdmStructuralProperty(infoFromCustomerType, "CustomerMessage", EdmCoreModel.Instance.GetString(false)));
            model.AddElement(infoFromCustomerType);

            #endregion

            #region EnumType

            var customerLevelType = new EdmEnumType(ns, "CustomerLevel");
            customerLevelType.AddMember("Common", new EdmEnumMemberValue(0));
            customerLevelType.AddMember("Silver", new EdmEnumMemberValue(1));
            customerLevelType.AddMember("Gold", new EdmEnumMemberValue(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))));
            orderType.AddStructuralProperty("OrderDetails", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(orderDetailType, true))));
            orderType.AddStructuralProperty("InfoFromCustomer", new EdmComplexTypeReference(infoFromCustomerType, 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 EdmPathExpression("Customers"));

            // 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 EdmPathExpression("Customers"));

            // 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);
        }
Exemplo n.º 11
0
        private static IEdmModel GetTypelessEdmModel()
        {
            EdmModel model = new EdmModel();

            // Enum type "Color"
            EdmEnumType colorEnum = new EdmEnumType("NS", "Color");

            colorEnum.AddMember(new EdmEnumMember(colorEnum, "Red", new EdmEnumMemberValue(0)));
            colorEnum.AddMember(new EdmEnumMember(colorEnum, "Blue", new EdmEnumMemberValue(1)));
            colorEnum.AddMember(new EdmEnumMember(colorEnum, "Green", new EdmEnumMemberValue(2)));
            model.AddElement(colorEnum);

            // complex type "Address"
            EdmComplexType address = new EdmComplexType("NS", "Address");

            address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

            // derived complex type "SubAddress"
            EdmComplexType subAddress = new EdmComplexType("NS", "SubAddress", address);

            subAddress.AddStructuralProperty("Code", EdmPrimitiveTypeKind.Double);
            model.AddElement(subAddress);

            // entity type "Customer"
            EdmEntityType customer = new EdmEntityType("NS", "Customer");

            customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
            customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            customer.AddStructuralProperty("Location", new EdmComplexTypeReference(address, isNullable: true));
            model.AddElement(customer);

            // derived entity type special customer
            EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer);

            specialCustomer.AddStructuralProperty("Title", EdmPrimitiveTypeKind.Guid);
            model.AddElement(specialCustomer);

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

            model.AddElement(container);
            container.AddEntitySet("FCustomers", customer);

            EdmComplexTypeReference    complexType           = new EdmComplexTypeReference(address, isNullable: true);
            EdmCollectionTypeReference complexCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(complexType));

            EdmEnumTypeReference       enumType           = new EdmEnumTypeReference(colorEnum, isNullable: false);
            EdmCollectionTypeReference enumCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(enumType));

            EdmEntityTypeReference     entityType           = new EdmEntityTypeReference(customer, isNullable: false);
            EdmCollectionTypeReference entityCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(entityType));

            IEdmTypeReference          intType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: true);
            EdmCollectionTypeReference primitiveCollectionType = new EdmCollectionTypeReference(new EdmCollectionType(intType));

            // bound functions
            BoundFunction(model, "IntCollectionFunction", "intValues", primitiveCollectionType, entityType);

            BoundFunction(model, "ComplexFunction", "address", complexType, entityType);

            BoundFunction(model, "ComplexCollectionFunction", "addresses", complexCollectionType, entityType);

            BoundFunction(model, "EnumFunction", "color", enumType, entityType);

            BoundFunction(model, "EnumCollectionFunction", "colors", enumCollectionType, entityType);

            BoundFunction(model, "EntityFunction", "customer", entityType, entityType);

            BoundFunction(model, "CollectionEntityFunction", "customers", entityCollectionType, entityType);

            // unbound functions
            UnboundFunction(container, "UnboundIntCollectionFunction", "intValues", primitiveCollectionType);

            UnboundFunction(container, "UnboundComplexFunction", "address", complexType);

            UnboundFunction(container, "UnboundComplexCollectionFunction", "addresses", complexCollectionType);

            UnboundFunction(container, "UnboundEnumFunction", "color", enumType);

            UnboundFunction(container, "UnboundEnumCollectionFunction", "colors", enumCollectionType);

            UnboundFunction(container, "UnboundEntityFunction", "customer", entityType);

            UnboundFunction(container, "UnboundCollectionEntityFunction", "customers", entityCollectionType);

            // bound to collection
            BoundToCollectionFunction(model, "BoundToCollectionFunction", "p", intType, entityType);

            model.SetAnnotationValue <BindableOperationFinder>(model, new BindableOperationFinder(model));
            return(model);
        }
        public NonPrimitiveTypeRoundtripJsonLightTests()
        {
            this.model = new EdmModel();

            EdmComplexType personalInfo = new EdmComplexType(MyNameSpace, "PersonalInfo");

            personalInfo.AddStructuralProperty("Age", EdmPrimitiveTypeKind.Int16);
            personalInfo.AddStructuralProperty("Email", EdmPrimitiveTypeKind.String);
            personalInfo.AddStructuralProperty("Tel", EdmPrimitiveTypeKind.String);
            personalInfo.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Guid);

            EdmComplexType derivedPersonalInfo = new EdmComplexType(MyNameSpace, "DerivedPersonalInfo", personalInfo);

            derivedPersonalInfo.AddStructuralProperty("Hobby", EdmPrimitiveTypeKind.String);

            EdmComplexType derivedDerivedPersonalInfo = new EdmComplexType(MyNameSpace, "DerivedDerivedPersonalInfo", derivedPersonalInfo);

            derivedDerivedPersonalInfo.AddStructuralProperty("Education", EdmPrimitiveTypeKind.String);

            EdmComplexType subjectInfo = new EdmComplexType(MyNameSpace, "Subject");

            subjectInfo.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            subjectInfo.AddStructuralProperty("Score", EdmPrimitiveTypeKind.Int16);

            EdmComplexType derivedSubjectInfo = new EdmComplexType(MyNameSpace, "DerivedSubject", subjectInfo);

            derivedSubjectInfo.AddStructuralProperty("Teacher", EdmPrimitiveTypeKind.String);

            EdmComplexType derivedDerivedSubjectInfo = new EdmComplexType(MyNameSpace, "DerivedDerivedSubject", derivedSubjectInfo);

            derivedDerivedSubjectInfo.AddStructuralProperty("Classroom", EdmPrimitiveTypeKind.String);

            EdmCollectionTypeReference subjectsCollection = new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(subjectInfo, isNullable: true)));

            studentInfo = new EdmEntityType(MyNameSpace, "Student");
            studentInfo.AddStructuralProperty("Info", new EdmComplexTypeReference(personalInfo, isNullable: false));
            studentInfo.AddProperty(new EdmStructuralProperty(studentInfo, "Subjects", subjectsCollection));

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

            enumFlagsType.AddMember("Red", new EdmIntegerConstant(1));
            enumFlagsType.AddMember("Green", new EdmIntegerConstant(2));
            enumFlagsType.AddMember("Blue", new EdmIntegerConstant(4));
            studentInfo.AddStructuralProperty("ClothesColors", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEnumTypeReference(enumFlagsType, true))));

            EdmCollectionTypeReference hobbiesCollection = new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(isNullable: false)));

            studentInfo.AddProperty(new EdmStructuralProperty(studentInfo, "Hobbies", hobbiesCollection));

            model.AddElement(enumFlagsType);
            model.AddElement(studentInfo);
            model.AddElement(personalInfo);
            model.AddElement(derivedPersonalInfo);
            model.AddElement(derivedDerivedPersonalInfo);
            model.AddElement(subjectInfo);
            model.AddElement(derivedSubjectInfo);
            model.AddElement(derivedDerivedSubjectInfo);

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

            model.AddElement(defaultContainer);

            this.studentSet = new EdmEntitySet(defaultContainer, "MySet", this.studentInfo);
        }
Exemplo n.º 13
0
        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);
        }
        public static IEdmModel CreateEdm()
        {
            var model = new EdmModel();

            // Complex Type
            EdmComplexType address = new EdmComplexType(Namespace, "Address");

            address.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

            EdmComplexType subAddress = new EdmComplexType(Namespace, "SubAddress", address);

            subAddress.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            model.AddElement(subAddress);

            // Enum type
            EdmEnumType color = new EdmEnumType(Namespace, "Color");

            color.AddMember(new EdmEnumMember(color, "None", new EdmEnumMemberValue(0)));
            color.AddMember(new EdmEnumMember(color, "Red", new EdmEnumMemberValue(1)));
            color.AddMember(new EdmEnumMember(color, "Blue", new EdmEnumMemberValue(2)));
            color.AddMember(new EdmEnumMember(color, "Green", new EdmEnumMemberValue(3)));
            model.AddElement(color);

            // Entity type
            EdmEntityType customer = new EdmEntityType(Namespace, "Customer");

            customer.AddKeys(customer.AddStructuralProperty("CustomerId", EdmPrimitiveTypeKind.Int32));
            customer.AddStructuralProperty("Location", new EdmComplexTypeReference(address, isNullable: true));
            model.AddElement(customer);

            EdmEntityType vipCustomer = new EdmEntityType(Namespace, "VipCustomer", customer);

            vipCustomer.AddStructuralProperty("FavoriteColor", new EdmEnumTypeReference(color, isNullable: false));
            model.AddElement(vipCustomer);

            EdmEntityType order = new EdmEntityType(Namespace, "Order");

            order.AddKeys(order.AddStructuralProperty("OrderId", EdmPrimitiveTypeKind.Int32));
            order.AddStructuralProperty("Token", EdmPrimitiveTypeKind.Guid);
            model.AddElement(order);

            EdmEntityContainer container = new EdmEntityContainer(Namespace, "Container");
            EdmEntitySet       customers = container.AddEntitySet("Customers", customer);
            EdmEntitySet       orders    = container.AddEntitySet("Orders", order);

            model.AddElement(container);

            // EdmSingleton mary = container.AddSingleton("Mary", customer);

            // navigation properties
            EdmNavigationProperty ordersNavProp = customer.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name = "Orders",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = order
            });

            customers.AddNavigationTarget(ordersNavProp, orders);

            // function
            IEdmTypeReference stringType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false);
            IEdmTypeReference intType    = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false);

            EdmFunction getFirstName = new EdmFunction(Namespace, "GetFirstName", stringType, isBound: true, entitySetPathExpression: null, isComposable: false);

            getFirstName.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(getFirstName);

            EdmFunction getNumber = new EdmFunction(Namespace, "GetOrderCount", intType, isBound: false, entitySetPathExpression: null, isComposable: false);

            model.AddElement(getNumber);
            container.AddFunctionImport("GetOrderCount", getNumber);

            // action
            EdmAction calculate = new EdmAction(Namespace, "CalculateOrderPrice", returnType: null, isBound: true, entitySetPathExpression: null);

            calculate.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(calculate);

            EdmAction change = new EdmAction(Namespace, "ChangeCustomerById", returnType: null, isBound: false, entitySetPathExpression: null);

            change.AddParameter("Id", intType);
            model.AddElement(change);
            container.AddActionImport("ChangeCustomerById", change);


            return(model);
        }
        public CustomersModelWithInheritance()
        {
            EdmModel model = new EdmModel();

            // Enum type simpleEnum
            EdmEnumType simpleEnum = new EdmEnumType("NS", "SimpleEnum");

            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "First", new EdmIntegerConstant(0)));
            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Second", new EdmIntegerConstant(1)));
            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Third", new EdmIntegerConstant(2)));
            model.AddElement(simpleEnum);

            // complex type address
            EdmComplexType address = new EdmComplexType("NS", "Address");

            address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("Country", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

            // entity type customer
            EdmEntityType customer = new EdmEntityType("NS", "Customer");

            customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            IEdmProperty customerName = customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);

            customer.AddStructuralProperty("SimpleEnum", simpleEnum.ToEdmTypeReference(isNullable: false));
            customer.AddStructuralProperty("Address", new EdmComplexTypeReference(address, isNullable: true));
            IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
                EdmPrimitiveTypeKind.String,
                isNullable: true);

            customer.AddStructuralProperty(
                "City",
                primitiveTypeReference,
                defaultValue: null,
                concurrencyMode: EdmConcurrencyMode.Fixed);
            model.AddElement(customer);

            // derived entity type special customer
            EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer);

            specialCustomer.AddStructuralProperty("SpecialCustomerProperty", EdmPrimitiveTypeKind.Guid);
            specialCustomer.AddStructuralProperty("SpecialAddress", new EdmComplexTypeReference(address, isNullable: true));
            model.AddElement(specialCustomer);

            // entity type order
            EdmEntityType order = new EdmEntityType("NS", "Order");

            order.AddKeys(order.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            order.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32);
            model.AddElement(order);

            // derived entity type special order
            EdmEntityType specialOrder = new EdmEntityType("NS", "SpecialOrder", order);

            specialOrder.AddStructuralProperty("SpecialOrderProperty", EdmPrimitiveTypeKind.Guid);
            model.AddElement(specialOrder);

            // test entity
            EdmEntityType testEntity = new EdmEntityType("System.Web.OData.Query.Expressions", "TestEntity");

            testEntity.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Binary);
            model.AddElement(testEntity);

            // containment
            // my order
            EdmEntityType myOrder = new EdmEntityType("NS", "MyOrder");

            myOrder.AddKeys(myOrder.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            myOrder.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(myOrder);

            // order line
            EdmEntityType orderLine = new EdmEntityType("NS", "OrderLine");

            orderLine.AddKeys(orderLine.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            orderLine.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(orderLine);

            EdmNavigationProperty orderLinesNavProp = myOrder.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name = "OrderLines",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = orderLine,
                ContainsTarget     = true,
            });

            EdmAction tag = new EdmAction("NS", "tag", returnType: null, isBound: true, entitySetPathExpression: null);

            tag.AddParameter("entity", new EdmEntityTypeReference(orderLine, false));
            model.AddElement(tag);

            // entity sets
            EdmEntityContainer container = new EdmEntityContainer("NS", "ModelWithInheritance");

            model.AddElement(container);
            EdmEntitySet customers = container.AddEntitySet("Customers", customer);
            EdmEntitySet orders    = container.AddEntitySet("Orders", order);
            EdmEntitySet myOrders  = container.AddEntitySet("MyOrders", myOrder);

            // singletons
            EdmSingleton vipCustomer = container.AddSingleton("VipCustomer", customer);
            EdmSingleton mary        = container.AddSingleton("Mary", customer);

            // containment
            IEdmContainedEntitySet orderLines = (IEdmContainedEntitySet)myOrders.FindNavigationTarget(orderLinesNavProp);

            // actions
            EdmAction upgrade = new EdmAction("NS", "upgrade", returnType: null, isBound: true, entitySetPathExpression: null);

            upgrade.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(upgrade);

            EdmAction specialUpgrade =
                new EdmAction("NS", "specialUpgrade", returnType: null, isBound: true, entitySetPathExpression: null);

            specialUpgrade.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false));
            model.AddElement(specialUpgrade);

            // functions
            IEdmTypeReference returnType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: false);
            IEdmTypeReference stringType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false);
            IEdmTypeReference intType    = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false);

            EdmFunction IsUpgraded = new EdmFunction(
                "NS",
                "IsUpgraded",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            IsUpgraded.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(IsUpgraded);

            EdmFunction orderByCityAndAmount = new EdmFunction(
                "NS",
                "OrderByCityAndAmount",
                stringType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            orderByCityAndAmount.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            orderByCityAndAmount.AddParameter("city", stringType);
            orderByCityAndAmount.AddParameter("amount", intType);
            model.AddElement(orderByCityAndAmount);

            EdmFunction IsSpecialUpgraded = new EdmFunction(
                "NS",
                "IsSpecialUpgraded",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            IsSpecialUpgraded.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false));
            model.AddElement(IsSpecialUpgraded);

            EdmFunction getSalary = new EdmFunction(
                "NS",
                "GetSalary",
                stringType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            getSalary.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(getSalary);

            getSalary = new EdmFunction(
                "NS",
                "GetSalary",
                stringType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);
            getSalary.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false));
            model.AddElement(getSalary);

            EdmFunction IsAnyUpgraded = new EdmFunction(
                "NS",
                "IsAnyUpgraded",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);
            EdmCollectionType edmCollectionType = new EdmCollectionType(new EdmEntityTypeReference(customer, false));

            IsAnyUpgraded.AddParameter("entityset", new EdmCollectionTypeReference(edmCollectionType));
            model.AddElement(IsAnyUpgraded);

            EdmFunction isCustomerUpgradedWithParam = new EdmFunction(
                "NS",
                "IsUpgradedWithParam",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            isCustomerUpgradedWithParam.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            isCustomerUpgradedWithParam.AddParameter("city", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false));
            model.AddElement(isCustomerUpgradedWithParam);

            EdmFunction isCustomerLocal = new EdmFunction(
                "NS",
                "IsLocal",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            isCustomerLocal.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(isCustomerLocal);

            // navigation properties
            EdmNavigationProperty ordersNavProp = customer.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name = "Orders",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = order
            });

            mary.AddNavigationTarget(ordersNavProp, orders);
            vipCustomer.AddNavigationTarget(ordersNavProp, orders);
            customers.AddNavigationTarget(ordersNavProp, orders);
            orders.AddNavigationTarget(
                order.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name = "Customer",
                TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                Target             = customer
            }),
                customers);

            // navigation properties on derived types.
            EdmNavigationProperty specialOrdersNavProp = specialCustomer.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name = "SpecialOrders",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = order
            });

            vipCustomer.AddNavigationTarget(specialOrdersNavProp, orders);
            customers.AddNavigationTarget(specialOrdersNavProp, orders);
            orders.AddNavigationTarget(
                specialOrder.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name = "SpecialCustomer",
                TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                Target             = customer
            }),
                customers);
            model.SetAnnotationValue <BindableProcedureFinder>(model, new BindableProcedureFinder(model));

            // set properties
            Model                     = model;
            Container                 = container;
            Customer                  = customer;
            Order                     = order;
            Address                   = address;
            SpecialCustomer           = specialCustomer;
            SpecialOrder              = specialOrder;
            Orders                    = orders;
            Customers                 = customers;
            VipCustomer               = vipCustomer;
            Mary                      = mary;
            OrderLine                 = orderLine;
            OrderLines                = orderLines;
            UpgradeCustomer           = upgrade;
            UpgradeSpecialCustomer    = specialUpgrade;
            CustomerName              = customerName;
            IsCustomerUpgraded        = isCustomerUpgradedWithParam;
            IsSpecialCustomerUpgraded = IsSpecialUpgraded;
            Tag = tag;
        }
Exemplo n.º 16
0
        public static IEdmModel GetEdmModel()
        {
            EdmModel model = new EdmModel();

            // Enum type "Color"
            EdmEnumType colorEnum = new EdmEnumType("NS", "Color");

            colorEnum.AddMember(new EdmEnumMember(colorEnum, "Red", new EdmEnumMemberValue(0)));
            colorEnum.AddMember(new EdmEnumMember(colorEnum, "Blue", new EdmEnumMemberValue(1)));
            colorEnum.AddMember(new EdmEnumMember(colorEnum, "Green", new EdmEnumMemberValue(2)));
            model.AddElement(colorEnum);

            // complex type "Address"
            EdmComplexType address = new EdmComplexType("NS", "Address");

            address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

            // derived complex type "SubAddress"
            EdmComplexType subAddress = new EdmComplexType("NS", "SubAddress", address);

            subAddress.AddStructuralProperty("Code", EdmPrimitiveTypeKind.Double);
            model.AddElement(subAddress);

            // entity type "Customer"
            EdmEntityType customer = new EdmEntityType("NS", "Customer");

            customer.AddKeys(customer.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32));
            customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(customer);

            // derived entity type special customer
            EdmEntityType subCustomer = new EdmEntityType("NS", "SubCustomer", customer);

            subCustomer.AddStructuralProperty("Price", EdmPrimitiveTypeKind.Double);
            model.AddElement(subCustomer);

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

            model.AddElement(container);
            container.AddEntitySet("Customers", customer);

            IEdmTypeReference       intType     = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: true);
            EdmEnumTypeReference    enumType    = new EdmEnumTypeReference(colorEnum, isNullable: true);
            EdmComplexTypeReference complexType = new EdmComplexTypeReference(address, isNullable: true);
            EdmEntityTypeReference  entityType  = new EdmEntityTypeReference(customer, isNullable: true);

            // functions
            BuildFunction(model, "PrimitiveFunction", entityType, "param", intType);
            BuildFunction(model, "EnumFunction", entityType, "color", enumType);
            BuildFunction(model, "ComplexFunction", entityType, "address", complexType);
            BuildFunction(model, "EntityFunction", entityType, "customer", entityType);

            // actions
            BuildAction(model, "PrimitiveAction", entityType, "param", intType);
            BuildAction(model, "EnumAction", entityType, "color", enumType);
            BuildAction(model, "ComplexAction", entityType, "address", complexType);
            BuildAction(model, "EntityAction", entityType, "customer", entityType);
            return(model);
        }
Exemplo n.º 17
0
        public void EnumTypeReferenceFullNameTest()
        {
            var enumType = new EdmEnumType("n", "enumtype");

            enumType.FullTypeName().Should().Be("n.enumtype");
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
0
 protected virtual void VisitEdmEnumType(EdmEnumType item)
 {
     VisitEdmNamedMetadataItem(item);
     if (item != null)
     {
         if (item.HasMembers)
         {
             VisitEnumMembers(item, item.Members);
         }
     }
 }
Exemplo n.º 20
0
        public ODataDynamicPropertyTests()
        {
            this.serviceEdmModel = new EdmModel();

            // Enum type
            this.genreEnumTypeName = "ServiceNS.Genre";
            this.genreEnumType     = new EdmEnumType("ServiceNS", "Genre");
            this.genreEnumType.AddMember(new EdmEnumMember(this.genreEnumType, "Thriller", new EdmEnumMemberValue(1)));
            this.genreEnumType.AddMember(new EdmEnumMember(this.genreEnumType, "SciFi", new EdmEnumMemberValue(2)));
            this.genreEnumType.AddMember(new EdmEnumMember(this.genreEnumType, "Epic", new EdmEnumMemberValue(3)));
            this.serviceEdmModel.AddElement(this.genreEnumType);

            // Complex type
            this.addressComplexTypeName = "ServiceNS.Address";
            this.addressComplexType     = new EdmComplexType("ServiceNS", "Address", null /* baseType */, false /* isAbstract */, true /* isOpen */);
            this.addressComplexType.AddStructuralProperty("AddressLine", EdmPrimitiveTypeKind.String);
            this.addressComplexType.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            this.serviceEdmModel.AddElement(this.addressComplexType);

            // Nested complex type
            this.nextOfKinComplexTypeName = "ServiceNS.NextOfKin";
            this.nextOfKinComplexType     = new EdmComplexType("ServiceNS", "NextOfKin");
            this.nextOfKinComplexType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            this.nextOfKinComplexType.AddStructuralProperty("HomeAddress", new EdmComplexTypeReference(this.addressComplexType, true));
            this.serviceEdmModel.AddElement(this.nextOfKinComplexType);

            // Director entity type
            this.directorEntitySetName  = "Directors";
            this.directorEntityTypeName = "ServiceNS.Director";
            this.directorEntityType     = new EdmEntityType("ServiceNS", "Director", null /* baseType */, false /* isAbstract */, true /* isOpen */); // Open type
            this.directorEntityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            this.directorEntityType.AddKeys(this.directorEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false));
            this.serviceEdmModel.AddElement(directorEntityType);

            // Editor entity type
            this.editorEntitySetName  = "Editors";
            this.editorEntityTypeName = "ServiceNS.Editor";
            this.editorEntityType     = new EdmEntityType("ServiceNS", "Editor", null /* baseType */, false /* isAbstract */, true /* isOpen */); // Open type
            this.editorEntityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            this.editorEntityType.AddKeys(this.editorEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false));
            this.serviceEdmModel.AddElement(editorEntityType);

            // Producer entity type
            this.producerEntitySetName  = "Producers";
            this.producerEntityTypeName = "ServiceNS.Producer";
            this.producerEntityType     = new EdmEntityType("ServiceNS", "Producer"); // Not an open type
            this.producerEntityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            this.producerEntityType.AddKeys(this.producerEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false));
            this.serviceEdmModel.AddElement(producerEntityType);

            // Actor entity type
            this.actorEntitySetName  = "Actors";
            this.actorEntityTypeName = "ServiceNS.Actor";
            this.actorEntityType     = new EdmEntityType("ServiceNS", "Actor", null /* baseType */, false /* isAbstract */, true /* isOpen */); // Open type
            this.actorEntityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            this.actorEntityType.AddKeys(this.actorEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false));
            this.serviceEdmModel.AddElement(actorEntityType);

            // Award entity type
            this.awardEntitySetName  = "Awards";
            this.awardEntityTypeName = "ServiceNS.Award";
            this.awardEntityType     = new EdmEntityType("ServiceNS", "Award"); // Not an open type
            this.awardEntityType.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            this.awardEntityType.AddKeys(this.awardEntityType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32, false));
            this.serviceEdmModel.AddElement(awardEntityType);

            // Entity container
            EdmEntityContainer container = new EdmEntityContainer("ServiceNS", "Container");

            container.AddEntitySet(this.directorEntitySetName, this.directorEntityType, true);
            container.AddEntitySet(this.editorEntitySetName, this.editorEntityType, true);
            container.AddEntitySet(this.producerEntitySetName, this.producerEntityType, true);
            container.AddEntitySet(this.actorEntitySetName, this.actorEntityType, true);
            container.AddEntitySet(this.awardEntitySetName, this.awardEntityType, true);
            this.serviceEdmModel.AddElement(container);

            this.clientEdmModel = new ClientEdmModel(ODataProtocolVersion.V4);

            this.context = new DataServiceContext(new Uri("http://tempuri.org/"), ODataProtocolVersion.V4, this.clientEdmModel);
            this.context.UndeclaredPropertyBehavior = UndeclaredPropertyBehavior.Support;
            this.context.Format.UseJson(this.serviceEdmModel);

            var serverTypeNames = new[]
            {
                this.genreEnumTypeName,
                this.addressComplexTypeName,
                this.nextOfKinComplexTypeName,
                this.directorEntityTypeName,
                this.editorEntityTypeName,
                this.producerEntityTypeName,
                this.actorEntityTypeName,
                this.awardEntityTypeName
            };

            this.context.ResolveName = type => {
                // Lazy approach to resolving server type names - alternative would be multiple if/else blocks
                return(serverTypeNames.FirstOrDefault(d => d.EndsWith(type.Name, StringComparison.Ordinal)));
            };

            var serverTypeNameToClientTypeMapping = new Dictionary <string, Type>
            {
                { this.genreEnumTypeName, typeof(Genre) },
                { this.addressComplexTypeName, typeof(Address) },
                { this.nextOfKinComplexTypeName, typeof(NextOfKin) },
                { this.directorEntityTypeName, typeof(Director) },
                { this.producerEntityTypeName, typeof(Producer) },
                { this.editorEntityTypeName, typeof(Editor) },
                { this.actorEntityTypeName, typeof(Actor) },
                { this.awardEntityTypeName, typeof(Award) }
            };

            this.context.ResolveType = name => {
                if (!serverTypeNameToClientTypeMapping.ContainsKey(name))
                {
                    return(null);
                }
                // Lazy approach to resolving client types - alternative would be multiple if/else blocks
                return(serverTypeNameToClientTypeMapping[name]);
            };

            this.typeNameToEntitySetMapping = new Dictionary <string, string>
            {
                { typeof(Director).Name, this.directorEntitySetName },
                { typeof(Editor).Name, this.editorEntitySetName },
                { typeof(Producer).Name, this.producerEntitySetName },
                { typeof(Actor).Name, this.actorEntitySetName }
            };
        }
 public void ParseEnumLong()
 {
     var enumType = new EdmEnumType()
     {
         Name = "Namespace.Color",
         UnderlyingType = "Edm.Int64",
         Members = new[]
         {
             new EdmEnumMember { Name = "Black", EvaluatedValue = 0},
             new EdmEnumMember { Name = "White", EvaluatedValue = 1},
         }
     };
     var propertyType = new EdmEnumPropertyType() { Type = enumType };
     Assert.Equal(1L, _parser.ParseValue("Namespace.Color'White'", propertyType));
 }
        public CustomersModelWithInheritance()
        {
            EdmModel model = new EdmModel();

            // Enum type simpleEnum
            EdmEnumType simpleEnum = new EdmEnumType("NS", "SimpleEnum");

            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "First", new EdmEnumMemberValue(0)));
            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Second", new EdmEnumMemberValue(1)));
            simpleEnum.AddMember(new EdmEnumMember(simpleEnum, "Third", new EdmEnumMemberValue(2)));
            model.AddElement(simpleEnum);

            // complex type address
            EdmComplexType address = new EdmComplexType("NS", "Address");

            address.AddStructuralProperty("Street", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("State", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("ZipCode", EdmPrimitiveTypeKind.String);
            address.AddStructuralProperty("CountryOrRegion", EdmPrimitiveTypeKind.String);
            model.AddElement(address);

            // open complex type "Account"
            EdmComplexType account = new EdmComplexType("NS", "Account", null, false, true);

            account.AddStructuralProperty("Bank", EdmPrimitiveTypeKind.String);
            account.AddStructuralProperty("CardNum", EdmPrimitiveTypeKind.Int64);
            account.AddStructuralProperty("BankAddress", new EdmComplexTypeReference(address, isNullable: true));
            model.AddElement(account);

            EdmComplexType specialAccount = new EdmComplexType("NS", "SpecialAccount", account, false, true);

            specialAccount.AddStructuralProperty("SpecialCard", EdmPrimitiveTypeKind.String);
            model.AddElement(specialAccount);

            // entity type customer
            EdmEntityType customer = new EdmEntityType("NS", "Customer");

            customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            IEdmProperty customerName = customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);

            customer.AddStructuralProperty("SimpleEnum", simpleEnum.ToEdmTypeReference(isNullable: false));
            customer.AddStructuralProperty("Address", new EdmComplexTypeReference(address, isNullable: true));
            customer.AddStructuralProperty("Account", new EdmComplexTypeReference(account, isNullable: true));
            customer.AddStructuralProperty("OtherAccounts", new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(account, true))));
            IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
                EdmPrimitiveTypeKind.String,
                isNullable: true);
            var city = customer.AddStructuralProperty(
                "City",
                primitiveTypeReference,
                defaultValue: null);

            model.AddElement(customer);

            // derived entity type special customer
            EdmEntityType specialCustomer = new EdmEntityType("NS", "SpecialCustomer", customer);

            specialCustomer.AddStructuralProperty("SpecialCustomerProperty", EdmPrimitiveTypeKind.Guid);
            specialCustomer.AddStructuralProperty("SpecialAddress", new EdmComplexTypeReference(address, isNullable: true));
            model.AddElement(specialCustomer);

            // entity type order (open entity type)
            EdmEntityType order = new EdmEntityType("NS", "Order", null, false, true);

            // EdmEntityType order = new EdmEntityType("NS", "Order");
            order.AddKeys(order.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            order.AddStructuralProperty("City", EdmPrimitiveTypeKind.String);
            order.AddStructuralProperty("Amount", EdmPrimitiveTypeKind.Int32);
            model.AddElement(order);

            // derived entity type special order
            EdmEntityType specialOrder = new EdmEntityType("NS", "SpecialOrder", order, false, true);

            specialOrder.AddStructuralProperty("SpecialOrderProperty", EdmPrimitiveTypeKind.Guid);
            model.AddElement(specialOrder);

            // test entity
            EdmEntityType testEntity = new EdmEntityType("Microsoft.AspNet.OData.Test.Query.Expressions", "TestEntity");

            testEntity.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Binary);
            model.AddElement(testEntity);

            // containment
            // my order
            EdmEntityType myOrder = new EdmEntityType("NS", "MyOrder");

            myOrder.AddKeys(myOrder.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            myOrder.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(myOrder);

            // order line
            EdmEntityType orderLine = new EdmEntityType("NS", "OrderLine");

            orderLine.AddKeys(orderLine.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
            orderLine.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
            model.AddElement(orderLine);

            EdmNavigationProperty orderLinesNavProp = myOrder.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name = "OrderLines",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = orderLine,
                ContainsTarget     = true,
            });

            EdmNavigationProperty nonContainedOrderLinesNavProp = myOrder.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name = "NonContainedOrderLines",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = orderLine,
                ContainsTarget     = false,
            });

            EdmAction tag = new EdmAction("NS", "tag", returnType: null, isBound: true, entitySetPathExpression: null);

            tag.AddParameter("entity", new EdmEntityTypeReference(orderLine, false));
            model.AddElement(tag);

            // entity sets
            EdmEntityContainer container = new EdmEntityContainer("NS", "ModelWithInheritance");

            model.AddElement(container);
            EdmEntitySet customers = container.AddEntitySet("Customers", customer);
            EdmEntitySet orders    = container.AddEntitySet("Orders", order);
            EdmEntitySet myOrders  = container.AddEntitySet("MyOrders", myOrder);

            // singletons
            EdmSingleton vipCustomer = container.AddSingleton("VipCustomer", customer);
            EdmSingleton mary        = container.AddSingleton("Mary", customer);
            EdmSingleton rootOrder   = container.AddSingleton("RootOrder", order);

            // annotations
            model.SetOptimisticConcurrencyAnnotation(customers, new[] { city });

            // containment
            IEdmContainedEntitySet orderLines = (IEdmContainedEntitySet)myOrders.FindNavigationTarget(orderLinesNavProp);

            // no-containment
            IEdmNavigationSource nonContainedOrderLines = myOrders.FindNavigationTarget(nonContainedOrderLinesNavProp);

            IEdmTypeReference returnType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Boolean, isNullable: false);
            IEdmTypeReference stringType = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false);
            IEdmTypeReference intType    = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int32, isNullable: false);

            // actions
            EdmAction upgrade = new EdmAction("NS", "upgrade", returnType: null, isBound: true, entitySetPathExpression: null);

            upgrade.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(upgrade);

            EdmAction specialUpgrade =
                new EdmAction("NS", "specialUpgrade", returnType: null, isBound: true, entitySetPathExpression: null);

            specialUpgrade.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false));
            model.AddElement(specialUpgrade);

            // actions bound to collection
            EdmAction upgradeAll = new EdmAction("NS", "UpgradeAll", returnType: null, isBound: true, entitySetPathExpression: null);

            upgradeAll.AddParameter("entityset",
                                    new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(customer, false))));
            model.AddElement(upgradeAll);

            EdmAction upgradeSpecialAll = new EdmAction("NS", "UpgradeSpecialAll", returnType: null, isBound: true, entitySetPathExpression: null);

            upgradeSpecialAll.AddParameter("entityset",
                                           new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(specialCustomer, false))));
            model.AddElement(upgradeSpecialAll);

            // action with optional parameters
            EdmAction updateSalaray = new EdmAction("NS", "UpdateSalaray", returnType: null, isBound: true, entitySetPathExpression: null);

            updateSalaray.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            updateSalaray.AddParameter("minSalaray", intType);
            updateSalaray.AddOptionalParameter("maxSalaray", intType);
            updateSalaray.AddOptionalParameter("aveSalaray", intType, "129");

            // functions
            EdmFunction IsUpgraded = new EdmFunction(
                "NS",
                "IsUpgraded",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            IsUpgraded.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(IsUpgraded);

            EdmFunction orderByCityAndAmount = new EdmFunction(
                "NS",
                "OrderByCityAndAmount",
                stringType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            orderByCityAndAmount.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            orderByCityAndAmount.AddParameter("city", stringType);
            orderByCityAndAmount.AddParameter("amount", intType);
            model.AddElement(orderByCityAndAmount);

            EdmFunction getOrders = new EdmFunction(
                "NS",
                "GetOrders",
                EdmCoreModel.GetCollection(order.ToEdmTypeReference(false)),
                isBound: true,
                entitySetPathExpression: null,
                isComposable: true);

            getOrders.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            getOrders.AddParameter("parameter", intType);
            model.AddElement(getOrders);

            EdmFunction IsSpecialUpgraded = new EdmFunction(
                "NS",
                "IsSpecialUpgraded",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            IsSpecialUpgraded.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false));
            model.AddElement(IsSpecialUpgraded);

            EdmFunction getSalary = new EdmFunction(
                "NS",
                "GetSalary",
                stringType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            getSalary.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(getSalary);

            getSalary = new EdmFunction(
                "NS",
                "GetSalary",
                stringType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);
            getSalary.AddParameter("entity", new EdmEntityTypeReference(specialCustomer, false));
            model.AddElement(getSalary);

            EdmFunction IsAnyUpgraded = new EdmFunction(
                "NS",
                "IsAnyUpgraded",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);
            EdmCollectionType edmCollectionType = new EdmCollectionType(new EdmEntityTypeReference(customer, false));

            IsAnyUpgraded.AddParameter("entityset", new EdmCollectionTypeReference(edmCollectionType));
            model.AddElement(IsAnyUpgraded);

            EdmFunction isCustomerUpgradedWithParam = new EdmFunction(
                "NS",
                "IsUpgradedWithParam",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            isCustomerUpgradedWithParam.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            isCustomerUpgradedWithParam.AddParameter("city", EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false));
            model.AddElement(isCustomerUpgradedWithParam);

            EdmFunction isCustomerLocal = new EdmFunction(
                "NS",
                "IsLocal",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            isCustomerLocal.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            model.AddElement(isCustomerLocal);

            EdmFunction entityFunction = new EdmFunction(
                "NS",
                "GetCustomer",
                returnType,
                isBound: true,
                entitySetPathExpression: null,
                isComposable: false);

            entityFunction.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            entityFunction.AddParameter("customer", new EdmEntityTypeReference(customer, false));
            model.AddElement(entityFunction);

            EdmFunction getOrder = new EdmFunction(
                "NS",
                "GetOrder",
                order.ToEdmTypeReference(false),
                isBound: true,
                entitySetPathExpression: null,
                isComposable: true); // Composable

            getOrder.AddParameter("entity", new EdmEntityTypeReference(customer, false));
            getOrder.AddParameter("orderId", intType);
            model.AddElement(getOrder);

            // functions bound to collection
            EdmFunction isAllUpgraded = new EdmFunction("NS", "IsAllUpgraded", returnType, isBound: true,
                                                        entitySetPathExpression: null, isComposable: false);

            isAllUpgraded.AddParameter("entityset",
                                       new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(customer, false))));
            isAllUpgraded.AddParameter("param", intType);
            model.AddElement(isAllUpgraded);

            EdmFunction isSpecialAllUpgraded = new EdmFunction("NS", "IsSpecialAllUpgraded", returnType, isBound: true,
                                                               entitySetPathExpression: null, isComposable: false);

            isSpecialAllUpgraded.AddParameter("entityset",
                                              new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(specialCustomer, false))));
            isSpecialAllUpgraded.AddParameter("param", intType);
            model.AddElement(isSpecialAllUpgraded);

            // function with optional parameters
            EdmFunction getSalaray = new EdmFunction("NS", "GetWholeSalary", intType, isBound: true, entitySetPathExpression: null, isComposable: false);

            getSalaray.AddParameter("entityset", new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(customer, false))));
            getSalaray.AddParameter("minSalary", intType);
            getSalaray.AddOptionalParameter("maxSalary", intType);
            getSalaray.AddOptionalParameter("aveSalary", intType, "129");
            model.AddElement(getSalaray);

            // navigation properties
            EdmNavigationProperty ordersNavProp = customer.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name = "Orders",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = order
            });

            mary.AddNavigationTarget(ordersNavProp, orders);
            vipCustomer.AddNavigationTarget(ordersNavProp, orders);
            customers.AddNavigationTarget(ordersNavProp, orders);
            orders.AddNavigationTarget(
                order.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name = "Customer",
                TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                Target             = customer
            }),
                customers);

            // navigation properties on derived types.
            EdmNavigationProperty specialOrdersNavProp = specialCustomer.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                Name = "SpecialOrders",
                TargetMultiplicity = EdmMultiplicity.Many,
                Target             = order
            });

            vipCustomer.AddNavigationTarget(specialOrdersNavProp, orders);
            customers.AddNavigationTarget(specialOrdersNavProp, orders);
            orders.AddNavigationTarget(
                specialOrder.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                Name = "SpecialCustomer",
                TargetMultiplicity = EdmMultiplicity.ZeroOrOne,
                Target             = customer
            }),
                customers);
            model.SetAnnotationValue <BindableOperationFinder>(model, new BindableOperationFinder(model));

            // set properties
            Model                     = model;
            Container                 = container;
            Customer                  = customer;
            Order                     = order;
            Address                   = address;
            Account                   = account;
            SpecialCustomer           = specialCustomer;
            SpecialOrder              = specialOrder;
            Orders                    = orders;
            Customers                 = customers;
            VipCustomer               = vipCustomer;
            Mary                      = mary;
            RootOrder                 = rootOrder;
            OrderLine                 = orderLine;
            OrderLines                = orderLines;
            NonContainedOrderLines    = nonContainedOrderLines;
            UpgradeCustomer           = upgrade;
            UpgradeSpecialCustomer    = specialUpgrade;
            CustomerName              = customerName;
            IsCustomerUpgraded        = isCustomerUpgradedWithParam;
            IsSpecialCustomerUpgraded = IsSpecialUpgraded;
            Tag = tag;
        }
 public void ParseEnumInvalid()
 {
     var enumType = new EdmEnumType()
     {
         Name = "Namespace.Color",
         Members = new[]
         {
             new EdmEnumMember { Name = "Black", EvaluatedValue = 0},
             new EdmEnumMember { Name = "White", EvaluatedValue = 1},
         }
     };
     var propertyType = new EdmEnumPropertyType() { Type = enumType };
     Assert.Throws<FormatException>(() => _parser.ParseValue("Namespace.Color'None'", propertyType));
 }
        public ODataJsonLightReaderEnumIntegrationTests()
        {
            EdmModel tmpModel = new EdmModel();

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

            enumType.AddMember(red);
            enumType.AddMember("Green", new EdmEnumMemberValue(2));
            enumType.AddMember("Blue", new EdmEnumMemberValue(3));
            tmpModel.AddElement(enumType);

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

            enumFlagsType.AddMember("Red", new EdmEnumMemberValue(1));
            enumFlagsType.AddMember("Green", new EdmEnumMemberValue(2));
            enumFlagsType.AddMember("Blue", new EdmEnumMemberValue(4));
            tmpModel.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)));
            tmpModel.AddElement(myComplexType);
            this.entityType.AddProperty(new EdmStructuralProperty(this.entityType, "MyComplexType", new EdmComplexTypeReference(myComplexType, true)));

            // enum in derived complex type
            EdmComplexType myDerivedComplexType = new EdmComplexType("NS", "MyDerivedComplexType", myComplexType, false);

            myDerivedComplexType.AddProperty(new EdmStructuralProperty(myDerivedComplexType, "MyDerivedColorFlags", new EdmEnumTypeReference(enumFlagsType, false)));
            tmpModel.AddElement(myDerivedComplexType);

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

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

            tmpModel.AddElement(this.entityType);

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

            this.entitySet = new EdmEntitySet(defaultContainer, "MySet", this.entityType);
            defaultContainer.AddEntitySet(this.entitySet.Name, this.entityType);
            tmpModel.AddElement(defaultContainer);

            this.userModel = TestUtils.WrapReferencedModelsToMainModel("NS", "DefaultContainer", tmpModel);
        }