예제 #1
0
        public void CreateEnumTypeWithFlags()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            var color = builder.EnumType<Color>();
            color.Member(Color.Red);
            color.Member(Color.Green);
            color.Member(Color.Blue);

            // Act
            var model = builder.GetEdmModel();
            var colorType = model.SchemaElements.OfType<IEdmEnumType>().Single();

            // Assert
            Assert.Equal("Color", colorType.Name);
            Assert.True(colorType.IsFlags);
            Assert.Equal(3, colorType.Members.Count());
            Assert.Equal("Edm.Int32", colorType.UnderlyingType.FullName());

            var redMember = colorType.Members.SingleOrDefault(m => m.Name == "Red");
            var greenMember = colorType.Members.SingleOrDefault(m => m.Name == "Green");
            var blueMember = colorType.Members.SingleOrDefault(m => m.Name == "Blue");

            Assert.NotNull(redMember);
            Assert.NotNull(greenMember);
            Assert.NotNull(blueMember);
        }
예제 #2
0
        public static IEdmModel GetExplicitModel(string singletonName)
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            // Define EntityType of Partner
            var partner = builder.EntityType<Partner>();
            partner.HasKey(p => p.ID);
            partner.Property(p => p.Name);
            var partnerCompany = partner.HasRequired(p => p.Company);

            // Define Enum Type
            var category = builder.EnumType<CompanyCategory>();
            category.Member(CompanyCategory.IT);
            category.Member(CompanyCategory.Communication);
            category.Member(CompanyCategory.Electronics);
            category.Member(CompanyCategory.Others);

            // Define EntityType of Company
            var company = builder.EntityType<Company>();
            company.HasKey(p => p.ID);
            company.Property(p => p.Name);
            company.Property(p => p.Revenue);
            company.EnumProperty(p => p.Category);
            var companyPartners = company.HasMany(p => p.Partners);
            companyPartners.IsNotCountable();

            var companyBranches = company.CollectionProperty(p => p.Branches);

            // Define Complex Type: Office
            var office = builder.ComplexType<Office>();
            office.Property(p => p.City);
            office.Property(p => p.Address);

            // Define Derived Type: SubCompany
            var subCompany = builder.EntityType<SubCompany>();
            subCompany.DerivesFrom<Company>();
            subCompany.Property(p => p.Location);
            subCompany.Property(p => p.Description);
            subCompany.ComplexProperty(p => p.Office);

            builder.Namespace = typeof(Partner).Namespace;

            // Define PartnerSet and Company(singleton)
            EntitySetConfiguration<Partner> partnersConfiguration = builder.EntitySet<Partner>("Partners");
            partnersConfiguration.HasIdLink(c=>c.GenerateSelfLink(false), true);
            partnersConfiguration.HasSingletonBinding(c => c.Company, singletonName);
            Func<EntityInstanceContext<Partner>, IEdmNavigationProperty, Uri> link = (eic, np) => eic.GenerateNavigationPropertyLink(np, false);
            partnersConfiguration.HasNavigationPropertyLink(partnerCompany, link, true);
            partnersConfiguration.EntityType.Collection.Action("ResetDataSource");

            SingletonConfiguration<Company> companyConfiguration = builder.Singleton<Company>(singletonName);
            companyConfiguration.HasIdLink(c => c.GenerateSelfLink(false), true);
            companyConfiguration.HasManyBinding(c => c.Partners, "Partners");
            Func<EntityInstanceContext<Company>, IEdmNavigationProperty, Uri> linkFactory = (eic, np) => eic.GenerateNavigationPropertyLink(np, false);
            companyConfiguration.HasNavigationPropertyLink(companyPartners, linkFactory, true);
            companyConfiguration.EntityType.Action("ResetDataSource");
            companyConfiguration.EntityType.Function("GetPartnersCount").Returns<int>();

            return builder.GetEdmModel();
        }
        public void NonbindingParameterConfigurationSupportsParameterTypeAs(Type type, bool isNullable)
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            builder.EntityType<Customer>();
            builder.ComplexType<Address>();
            builder.EnumType<Color>();

            // Act
            Type underlyingType = TypeHelper.GetUnderlyingTypeOrSelf(type);
            IEdmTypeConfiguration edmTypeConfiguration = builder.GetTypeConfigurationOrNull(type);
            if (underlyingType.IsEnum)
            {
                edmTypeConfiguration = builder.GetTypeConfigurationOrNull(underlyingType);
                if (edmTypeConfiguration != null && isNullable)
                {
                    edmTypeConfiguration = ((EnumTypeConfiguration)edmTypeConfiguration).GetNullableEnumTypeConfiguration();
                }
            }
            NonbindingParameterConfiguration parameter = new NonbindingParameterConfiguration("name",
                edmTypeConfiguration);

            // Assert
            Assert.Equal(isNullable, parameter.OptionalParameter);
        }
예제 #4
0
        public static ODataModelBuilder Add_ByteEnum_EnumType(this ODataModelBuilder builder)
        {
            EnumTypeConfiguration <ByteEnum> byteEnum = builder.EnumType <ByteEnum>();

            byteEnum.Member(ByteEnum.FirstByte);
            byteEnum.Member(ByteEnum.SecondByte);
            byteEnum.Member(ByteEnum.ThirdByte);
            return(builder);
        }
예제 #5
0
        public static ODataModelBuilder Add_LongEnum_EnumType(this ODataModelBuilder builder)
        {
            var longEnum = builder.EnumType <LongEnum>();

            longEnum.Member(LongEnum.FirstLong);
            longEnum.Member(LongEnum.SecondLong);
            longEnum.Member(LongEnum.ThirdLong);
            return(builder);
        }
예제 #6
0
        public static ODataModelBuilder Add_FlagsEnum_EnumType(this ODataModelBuilder builder)
        {
            var flagsEnum = builder.EnumType <FlagsEnum>();

            flagsEnum.Member(FlagsEnum.One);
            flagsEnum.Member(FlagsEnum.Two);
            flagsEnum.Member(FlagsEnum.Four);
            return(builder);
        }
예제 #7
0
        public static ODataModelBuilder Add_SimpleEnum_EnumType(this ODataModelBuilder builder)
        {
            var simpleEnum = builder.EnumType <SimpleEnum>();

            simpleEnum.Member(SimpleEnum.First);
            simpleEnum.Member(SimpleEnum.Second);
            simpleEnum.Member(SimpleEnum.Third);
            return(builder);
        }
예제 #8
0
        public static ODataModelBuilder Add_Color_EnumType(this ODataModelBuilder builder)
        {
            var color = builder.EnumType <Color>();

            color.Member(Color.Red);
            color.Member(Color.Green);
            color.Member(Color.Blue);
            return(builder);
        }
예제 #9
0
        public static ODataModelBuilder Add_SByteEnum_EnumType(this ODataModelBuilder builder)
        {
            EnumTypeConfiguration <SByteEnum> sByteEnum = builder.EnumType <SByteEnum>();

            sByteEnum.Member(SByteEnum.FirstSByte);
            sByteEnum.Member(SByteEnum.SecondSByte);
            sByteEnum.Member(SByteEnum.ThirdSByte);
            return(builder);
        }
예제 #10
0
        public static IEdmModel GetTypedExplicitModel()
        {
            ODataModelBuilder builder = new ODataModelBuilder();
            var accountType = builder.EntityType<Account>();
            accountType.HasKey(c => c.Id);
            accountType.Property(c => c.Name);
            accountType.HasDynamicProperties(c => c.DynamicProperties);

            accountType.ComplexProperty<AccountInfo>(c => c.AccountInfo);
            accountType.ComplexProperty<Address>(a => a.Address);
            accountType.ComplexProperty<Tags>(a => a.Tags);

            var premiumAccountType = builder.EntityType<PremiumAccount>();
            premiumAccountType.Property(p => p.Since);
            premiumAccountType.DerivesFrom<Account>();

            var accountInfoType = builder.ComplexType<AccountInfo>();
            accountInfoType.Property(i => i.NickName);
            accountInfoType.HasDynamicProperties(i => i.DynamicProperties);

            var addressType = builder.ComplexType<Address>();
            addressType.Property(a => a.City);
            addressType.Property(a => a.Street);
            addressType.HasDynamicProperties(a => a.DynamicProperties);

            var globalAddressType = builder.ComplexType<GlobalAddress>();
            globalAddressType.Property(a => a.CountryCode);
            globalAddressType.DerivesFrom<Address>();

            var tagsType = builder.ComplexType<Tags>();
            tagsType.HasDynamicProperties(t => t.DynamicProperties);

            var gender = builder.EnumType<Gender>();
            gender.Member(Gender.Female);
            gender.Member(Gender.Male);

            var employeeType = builder.EntityType<Employee>();
            employeeType.HasKey(e => e.Id);
            employeeType.HasOptional(e => e.Account);
            builder.EntitySet<Employee>("Employees");

            var managerType = builder.EntityType<Manager>();
            managerType.Property(m => m.Heads);
            managerType.HasDynamicProperties(m => m.DynamicProperties);
            managerType.DerivesFrom<Employee>();

            AddBoundActionsAndFunctions(accountType);
            AddUnboundActionsAndFunctions(builder);

            EntitySetConfiguration<Account> accounts = builder.EntitySet<Account>("Accounts");
            builder.Namespace = typeof(Account).Namespace;
            return builder.GetEdmModel();
        }
예제 #11
0
        public static IEdmModel GetExplicitModel()
        {
            ODataModelBuilder builder = new ODataModelBuilder();
            var employee = builder.EntityType<Employee>();
            employee.HasKey(c => c.ID);
            employee.Property(c => c.Name);
            employee.CollectionProperty<Skill>(c => c.SkillSet);
            employee.EnumProperty<Gender>(c => c.Gender);
            employee.EnumProperty<AccessLevel>(c => c.AccessLevel);
            employee.ComplexProperty<FavoriteSports>(c => c.FavoriteSports);

            var skill = builder.EnumType<Skill>();
            skill.Member(Skill.CSharp);
            skill.Member(Skill.Sql);
            skill.Member(Skill.Web);

            var gender = builder.EnumType<Gender>();
            gender.Member(Gender.Female);
            gender.Member(Gender.Male);

            var accessLevel = builder.EnumType<AccessLevel>();
            accessLevel.Member(AccessLevel.Execute);
            accessLevel.Member(AccessLevel.Read);
            accessLevel.Member(AccessLevel.Write);

            var favoriteSports = builder.ComplexType<FavoriteSports>();
            favoriteSports.EnumProperty<Sport>(f => f.LikeMost);
            favoriteSports.CollectionProperty<Sport>(f => f.Like);

            var sport = builder.EnumType<Sport>();
            sport.Member(Sport.Basketball);
            sport.Member(Sport.Pingpong);

            AddBoundActionsAndFunctions(employee);
            AddUnboundActionsAndFunctions(builder);

            EntitySetConfiguration<Employee> employees = builder.EntitySet<Employee>("Employees");
            builder.Namespace = typeof(Employee).Namespace;
            return builder.GetEdmModel();
        }
예제 #12
0
        public static IEdmModel GetEdmModel()
        {
            if (_model == null)
            {
                ODataModelBuilder model = new ODataModelBuilder();

                var color = model.EnumType<Color>();
                color.Member(Color.Red);
                color.Member(Color.Green);
                color.Member(Color.Blue);

                var people = model.EntitySet<FormatterPerson>("People");
                people.HasFeedSelfLink(context => new Uri(context.Url.CreateODataLink(new EntitySetPathSegment(
                    context.EntitySet))));
                people.HasIdLink(context =>
                    {
                        return context.Url.CreateODataLink(
                            new EntitySetPathSegment(context.EntitySet),
                            new KeyValuePathSegment(context.GetPropertyValue("PerId").ToString()));
                    },
                    followsConventions: false);

                var person = people.EntityType;
                person.HasKey(p => p.PerId);
                person.Property(p => p.Age);
                person.Property(p => p.MyGuid);
                person.Property(p => p.Name);
                person.EnumProperty(p => p.FavoriteColor);
                person.ComplexProperty<FormatterOrder>(p => p.Order);

                var order = model.ComplexType<FormatterOrder>();
                order.Property(o => o.OrderAmount);
                order.Property(o => o.OrderName);

                // Add a top level function 
                var getPersons = model.Function("GetPerson");
                getPersons.Parameter<Int32>("PerId");
                getPersons.ReturnsFromEntitySet<FormatterPerson>("People");

                // Add a top level function which is not included in service document
                var getVipPerson = model.Function("GetVipPerson");
                getVipPerson.ReturnsFromEntitySet<FormatterPerson>("People");
                getVipPerson.IncludeInServiceDocument = false;

                _model = model.GetEdmModel();
            }

            return _model;
        }
예제 #13
0
        public void CreateEnumTypeWithoutFlags()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            var simple = builder.EnumType<SimpleEnum>();
            simple.Member(SimpleEnum.First);
            simple.Member(SimpleEnum.Second);
            simple.Member(SimpleEnum.Third);

            // Act
            var model = builder.GetEdmModel();
            var colorType = model.SchemaElements.OfType<IEdmEnumType>().Single();

            // Assert
            Assert.False(colorType.IsFlags);
        }
예제 #14
0
        private static IEdmModel GetEdmModel()
        {
            if (_edmModel == null)
            {
                ODataModelBuilder modelBuilder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();
                modelBuilder.EntityType <DerivedTypeA>().DerivesFrom <BaseType>();
                modelBuilder.EntityType <DerivedTypeB>().DerivesFrom <BaseType>();

                modelBuilder.ComplexType <AComplexType>();
                modelBuilder.EnumType <AEnumType>();

                _edmModel = modelBuilder.GetEdmModel();
            }

            return(_edmModel);
        }
예제 #15
0
        private static IEdmModel GetExplicitEdmModel()
        {
            // Non-conventional model builder
            // http://odata.github.io/WebApi/#02-03-model-builder-nonconvention

            var builder = new ODataModelBuilder();

            // enum type "Color"
            var color = builder.EnumType <Color>();

            color.Member(Color.Red);
            color.Member(Color.Green);
            color.Member(Color.Blue);
            color.Member(Color.Yellow);
            color.Member(Color.Pink);
            color.Member(Color.Purple);

            // complex type "Address"
            var address = builder.ComplexType <Address>();

            address.Property(a => a.City);
            address.Property(a => a.Street);

            // entity type "Customer"
            var customer = builder.EntityType <Customer>().HasKey(c => c.CustomerId);

            customer.Property(c => c.Name);
            customer.Property(c => c.Token);
            // customer.Property(c => c.Email).IsNotNavigable(); // you can call Fluent API
            customer.Property(c => c.Email);
            customer.CollectionProperty(c => c.Addresses);
            customer.CollectionProperty(c => c.FavoriateColors);
            customer.HasMany(c => c.Orders);

            // entity type "Order"
            var order = builder.EntityType <Order>().HasKey(o => o.OrderId);

            order.Property(o => o.Price);

            // entity sets
            builder.EntitySet <Customer>("Customers").HasManyBinding(c => c.Orders, "Orders");
            builder.EntitySet <Order>("Orders");

            return(builder.GetEdmModel());
        }
        public void NonbindingParameterConfigurationSupportsParameterCollectionTypeAs(Type type, bool isNullable)
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            builder.EntityType<Customer>();
            builder.ComplexType<Address>();
            builder.EnumType<Color>();

            Type elementType;
            Assert.True(type.IsCollection(out elementType));

            // Act
            Type underlyingType = TypeHelper.GetUnderlyingTypeOrSelf(elementType);
            IEdmTypeConfiguration elementTypeConfiguration = builder.GetTypeConfigurationOrNull(underlyingType);
            CollectionTypeConfiguration collectionType = new CollectionTypeConfiguration(elementTypeConfiguration,
                typeof(IEnumerable<>).MakeGenericType(elementType));

            NonbindingParameterConfiguration parameter = new NonbindingParameterConfiguration("name", collectionType);

            // Assert
            Assert.Equal(isNullable, parameter.OptionalParameter);
        }
예제 #17
0
        private static IEdmModel GetExplicitEdmModel()
        {
            var builder = new ODataModelBuilder();

            // enum type "Color"
            var color = builder.EnumType<Color>();
            color.Member(Color.Red);
            color.Member(Color.Green);
            color.Member(Color.Blue);
            color.Member(Color.Yellow);
            color.Member(Color.Pink);
            color.Member(Color.Purple);

            // complex type "Address"
            var address = builder.ComplexType<Address>();
            address.Property(a => a.City);
            address.Property(a => a.Street);

            // entity type "Customer"
            var customer = builder.EntityType<Customer>().HasKey(c => c.CustomerId);
            customer.Property(c => c.Name);
            customer.Property(c => c.Token);
            // customer.Property(c => c.Email).IsNotNavigable(); // you can call Fluent API
            customer.Property(c => c.Email);
            customer.CollectionProperty(c => c.Addresses);
            customer.CollectionProperty(c => c.FavoriateColors);
            customer.HasMany(c => c.Orders);

            // entity type "Order"
            var order = builder.EntityType<Order>().HasKey(o => o.OrderId);
            order.Property(o => o.Price);

            // entity sets
            builder.EntitySet<Customer>("Customers").HasManyBinding(c => c.Orders, "Orders");
            builder.EntitySet<Order>("Orders");

            return builder.GetEdmModel();
        }
예제 #18
0
        private static IEdmModel GetEdmModel()
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            // Configure LimitedEntity
            EntitySetConfiguration <LimitedEntity> limitedEntities = builder.EntitySet <LimitedEntity>("LimitedEntities");

            limitedEntities.EntityType.HasKey(p => p.Id);
            limitedEntities.EntityType.ComplexProperty(c => c.ComplexProperty);
            limitedEntities.EntityType.CollectionProperty(c => c.ComplexCollectionProperty).IsNotCountable();
            limitedEntities.EntityType.HasMany(l => l.EntityCollectionProperty).IsNotCountable();
            limitedEntities.EntityType.CollectionProperty(cp => cp.Integers).IsNotCountable();

            // Configure LimitedRelatedEntity
            EntitySetConfiguration <LimitedRelatedEntity> limitedRelatedEntities =
                builder.EntitySet <LimitedRelatedEntity>("LimitedRelatedEntities");

            limitedRelatedEntities.EntityType.HasKey(p => p.Id);
            limitedRelatedEntities.EntityType.CollectionProperty(p => p.ComplexCollectionProperty).IsNotCountable();

            // Configure Complextype
            ComplexTypeConfiguration <LimitedComplex> complexType = builder.ComplexType <LimitedComplex>();

            complexType.CollectionProperty(p => p.Strings).IsNotCountable();
            complexType.Property(p => p.Value);
            complexType.CollectionProperty(p => p.SimpleEnums).IsNotCountable();

            // Configure EnumType
            EnumTypeConfiguration <SimpleEnum> enumType = builder.EnumType <SimpleEnum>();

            enumType.Member(SimpleEnum.First);
            enumType.Member(SimpleEnum.Second);
            enumType.Member(SimpleEnum.Third);
            enumType.Member(SimpleEnum.Fourth);

            return(builder.GetEdmModel());
        }
예제 #19
0
        public void NonbindingParameterConfigurationSupportsParameterCollectionTypeAs(Type type, bool isNullable)
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();

            builder.EntityType <Customer>();
            builder.ComplexType <Address>();
            builder.EnumType <Color>();

            Type elementType;

            Assert.True(TypeHelper.IsCollection(type, out elementType));

            // Act
            Type underlyingType = TypeHelper.GetUnderlyingTypeOrSelf(elementType);
            IEdmTypeConfiguration       elementTypeConfiguration = builder.GetTypeConfigurationOrNull(underlyingType);
            CollectionTypeConfiguration collectionType           = new CollectionTypeConfiguration(elementTypeConfiguration,
                                                                                                   typeof(IEnumerable <>).MakeGenericType(elementType));

            NonbindingParameterConfiguration parameter = new NonbindingParameterConfiguration("name", collectionType);

            // Assert
            Assert.Equal(isNullable, parameter.Nullable);
        }
예제 #20
0
        public void RemoveWrongEnumTypeMemberFromConfigurationShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            builder.EnumType<Color>();
            EnumTypeConfiguration enumTypeConfiguration = builder.EnumTypes.Single();

            // Act & Assert
            Assert.ThrowsArgument(
                () => enumTypeConfiguration.RemoveMember(SimpleEnum.First),
                "member",
                "The property 'First' does not belong to the type 'System.Web.OData.Builder.TestModels.Color'.");
        }
예제 #21
0
        public void DollarMetadata_Works_WithEntityTypeWithEnumKeys()
        {
            // Arrange
            const string expectMetadata =
                "      <EntityType Name=\"EnumModel\">\r\n" +
                "        <Key>\r\n" +
                "          <PropertyRef Name=\"Simple\" />\r\n" +
                "        </Key>\r\n" +
                "        <Property Name=\"Simple\" Type=\"NS.SimpleEnum\" Nullable=\"false\" />\r\n" +
                "      </EntityType>\r\n" +
                "      <EnumType Name=\"SimpleEnum\" />";

            ODataModelBuilder builder = new ODataModelBuilder();
            builder.EntityType<EnumModel>().HasKey(e => e.Simple).Namespace = "NS";
            builder.EnumType<SimpleEnum>().Namespace = "NS";
            IEdmModel model = builder.GetEdmModel();

            var config = new[] { typeof(MetadataController) }.GetHttpConfiguration();
            config.MapODataServiceRoute(model);
            HttpServer server = new HttpServer(config);
            HttpClient client = new HttpClient(server);

            // Act
            var response = client.GetAsync("http://localhost/$metadata").Result;

            // Assert
            Assert.True(response.IsSuccessStatusCode);
            Assert.Equal("application/xml", response.Content.Headers.ContentType.MediaType);

            string payload = response.Content.ReadAsStringAsync().Result;
            Assert.Contains(expectMetadata, payload);
        }
예제 #22
0
        public void PassNullToEnumTypeConfigurationNameSetterShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            builder.EnumType<Color>();
            EnumTypeConfiguration enumTypeConfiguration = builder.EnumTypes.Single();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => enumTypeConfiguration.Name = null,
                "value");
        }
예제 #23
0
        public void PassNullToEnumTypeConfigurationRemoveMemberShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            builder.EnumType<Color>();
            EnumTypeConfiguration enumTypeConfiguration = builder.EnumTypes.Single();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => enumTypeConfiguration.RemoveMember(null),
                "member");
        }
예제 #24
0
        public void PassNullMemberParameterToEnumMemberConfigurationConstructorShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            builder.EnumType<Color>();
            EnumTypeConfiguration declaringType = builder.EnumTypes.Single();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => new EnumMemberConfiguration(null, declaringType),
                "member");
        }
예제 #25
0
        public void PassNullToEnumMemberConfigurationNameSetterShouldThrowException()
        {
            // Arrange
            Enum member = Color.Red;
            var builder = new ODataModelBuilder();
            builder.EnumType<Color>();
            EnumTypeConfiguration declaringType = builder.EnumTypes.Single();
            var enumMemberConfiguration = new EnumMemberConfiguration(member, declaringType);

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => enumMemberConfiguration.Name = null,
                "value");
        }
예제 #26
0
        public void PamameterOfEnumPropertyIsNotEnumShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            var color = builder.EnumType<Color>();
            color.Member(Color.Red);
            color.Member(Color.Green);
            color.Member(Color.Blue);
            var entityTypeConfiguration = builder.ComplexType<EntityTypeWithEnumTypePropertyTestModel>();

            // Act & Assert
            Assert.ThrowsArgument(
                () => entityTypeConfiguration.EnumProperty(e => e.ID),
                "propertyInfo",
                "The property 'ID' on type 'System.Web.OData.Builder.EntityTypeWithEnumTypePropertyTestModel' must be an Enum property.");
        }
예제 #27
0
        public static IEdmModel GetTypedExplicitModel()
        {
            ODataModelBuilder builder = new ODataModelBuilder();
            var accountType           = builder.EntityType <Account>();

            accountType.HasKey(c => c.Id);
            accountType.Property(c => c.Name);
            accountType.HasDynamicProperties(c => c.DynamicProperties);

            accountType.ComplexProperty <AccountInfo>(c => c.AccountInfo);
            accountType.ComplexProperty <Address>(a => a.Address);
            accountType.ComplexProperty <Tags>(a => a.Tags);

            var premiumAccountType = builder.EntityType <PremiumAccount>();

            premiumAccountType.Property(p => p.Since);
            premiumAccountType.DerivesFrom <Account>();

            var accountInfoType = builder.ComplexType <AccountInfo>();

            accountInfoType.Property(i => i.NickName);
            accountInfoType.HasDynamicProperties(i => i.DynamicProperties);

            var addressType = builder.ComplexType <Address>();

            addressType.Property(a => a.City);
            addressType.Property(a => a.Street);
            addressType.HasDynamicProperties(a => a.DynamicProperties);

            var globalAddressType = builder.ComplexType <GlobalAddress>();

            globalAddressType.Property(a => a.CountryCode);
            globalAddressType.DerivesFrom <Address>();

            var tagsType = builder.ComplexType <Tags>();

            tagsType.HasDynamicProperties(t => t.DynamicProperties);

            var gender = builder.EnumType <Gender>();

            gender.Member(Gender.Female);
            gender.Member(Gender.Male);

            var employeeType = builder.EntityType <Employee>();

            employeeType.HasKey(e => e.Id);
            employeeType.HasOptional(e => e.Account);
            builder.EntitySet <Employee>("Employees");

            var managerType = builder.EntityType <Manager>();

            managerType.Property(m => m.Heads);
            managerType.HasDynamicProperties(m => m.DynamicProperties);
            managerType.DerivesFrom <Employee>();

            AddBoundActionsAndFunctions(accountType);
            AddUnboundActionsAndFunctions(builder);

            EntitySetConfiguration <Account> accounts = builder.EntitySet <Account>("Accounts");

            builder.Namespace = typeof(Account).Namespace;
            return(builder.GetEdmModel());
        }
예제 #28
0
        public static IEdmModel GetExplicitModel(string singletonName)
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            // Define EntityType of Partner
            var partner = builder.EntityType <Partner>();

            partner.HasKey(p => p.ID);
            partner.Property(p => p.Name);
            var partnerCompany = partner.HasRequired(p => p.Company);

            // Define Enum Type
            var category = builder.EnumType <CompanyCategory>();

            category.Member(CompanyCategory.IT);
            category.Member(CompanyCategory.Communication);
            category.Member(CompanyCategory.Electronics);
            category.Member(CompanyCategory.Others);

            // Define EntityType of Company
            var company = builder.EntityType <Company>();

            company.HasKey(p => p.ID);
            company.Property(p => p.Name);
            company.Property(p => p.Revenue);
            company.EnumProperty(p => p.Category);
            var companyPartners = company.HasMany(p => p.Partners);

            companyPartners.IsNotCountable();

            var companyBranches = company.CollectionProperty(p => p.Branches);

            // Define Complex Type: Office
            var office = builder.ComplexType <Office>();

            office.Property(p => p.City);
            office.Property(p => p.Address);

            // Define Derived Type: SubCompany
            var subCompany = builder.EntityType <SubCompany>();

            subCompany.DerivesFrom <Company>();
            subCompany.Property(p => p.Location);
            subCompany.Property(p => p.Description);
            subCompany.ComplexProperty(p => p.Office);

            builder.Namespace = typeof(Partner).Namespace;

            // Define PartnerSet and Company(singleton)
            EntitySetConfiguration <Partner> partnersConfiguration = builder.EntitySet <Partner>("Partners");

            //partnersConfiguration.HasIdLink(c=>c.GenerateSelfLink(false), true);
            //partnersConfiguration.HasSingletonBinding(c => c.Company, singletonName);
            //Func<ResourceContext<Partner>, IEdmNavigationProperty, Uri> link = (eic, np) => eic.GenerateNavigationPropertyLink(np, false);
            //partnersConfiguration.HasNavigationPropertyLink(partnerCompany, link, true);
            partnersConfiguration.EntityType.Collection.Action("ResetDataSource");

            SingletonConfiguration <Company> companyConfiguration = builder.Singleton <Company>("Umbrella");

            //companyConfiguration.HasIdLink(c => c.GenerateSelfLink(false), true);
            //companyConfiguration.HasManyBinding(c => c.Partners, "Partners");
            //Func<ResourceContext<Company>, IEdmNavigationProperty, Uri> linkFactory = (eic, np) => eic.GenerateNavigationPropertyLink(np, false);
            //companyConfiguration.HasNavigationPropertyLink(companyPartners, linkFactory, true);
            companyConfiguration.EntityType.Action("ResetDataSource");
            companyConfiguration.EntityType.Function("GetPartnersCount").Returns <int>();

            SingletonConfiguration <Company> monstersIncConfiguration = builder.Singleton <Company>("MonstersInc");

            monstersIncConfiguration.EntityType.Function("GetPartnersCount").Returns <int>();

            return(builder.GetEdmModel());
        }
예제 #29
0
        public void TypeParameterOfEnumTypeIsNotEnumShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();

            // Act & Assert
            Assert.ThrowsArgument(
                () => builder.EnumType<ComplexTypeWithEnumTypePropertyTestModel>(),
                "type",
                "The type 'System.Web.OData.Builder.ComplexTypeWithEnumTypePropertyTestModel' cannot be configured as an enum type.");
        }
예제 #30
0
        public void AddAndRemoveEnumMemberFromEnumType()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            var color = builder.EnumType<Color>();

            // Act & Assert
            Assert.Equal(0, color.Members.Count());

            color.Member(Color.Red);
            color.Member(Color.Green);
            Assert.Equal(2, color.Members.Count());

            color.RemoveMember(Color.Red);
            Assert.Equal(1, color.Members.Count());
        }
        private static IEdmModel GetExplicitEdmModel()
        {
            var modelBuilder = new ODataModelBuilder();

            var enumContry = modelBuilder.EnumType<Country>();
            enumContry.Member(Country.Canada);
            enumContry.Member(Country.China);
            enumContry.Member(Country.India);
            enumContry.Member(Country.Japen);
            enumContry.Member(Country.USA);

            var products = modelBuilder.EntitySet<Product>("Products");
            products.HasEditLink(entityContext =>
                {
                    object id;
                    entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                    return new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName,
                        new
                        {
                            odataPath = entityContext.Url.CreateODataLink(
                                new EntitySetPathSegment(entityContext.NavigationSource.Name),
                                new KeyValuePathSegment(id.ToString()))
                        }));
                }, true);

            var suppliers = modelBuilder.EntitySet<Supplier>("Suppliers");
            suppliers.HasEditLink(entityContext =>
                {
                    object id;
                    entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                    return new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName,
                        new
                        {
                            odataPath = entityContext.Url.CreateODataLink(
                                new EntitySetPathSegment(entityContext.NavigationSource.Name),
                                new KeyValuePathSegment(id.ToString()))
                        }));
                }, true);

            var families = modelBuilder.EntitySet<ProductFamily>("ProductFamilies");
            families.HasEditLink(entityContext =>
                {
                    object id;
                    entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                    return new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName, 
                        new
                        {
                            odataPath = entityContext.Url.CreateODataLink(
                                new EntitySetPathSegment(entityContext.NavigationSource.Name),
                                new KeyValuePathSegment(id.ToString()))
                        }));
                }, true);

            var product = products.EntityType;

            product.HasKey(p => p.ID);
            product.Property(p => p.Name);
            product.Property(p => p.ReleaseDate);
            product.Property(p => p.SupportedUntil);

            var address = modelBuilder.ComplexType<Address>();
            address.Property(a => a.City);
            address.Property(a => a.Country);
            address.Property(a => a.State);
            address.Property(a => a.Street);
            address.Property(a => a.ZipCode);

            var supplier = suppliers.EntityType;
            supplier.HasKey(s => s.ID);
            supplier.Property(s => s.Name);
            supplier.CollectionProperty(s => s.Addresses);
            supplier.CollectionProperty(s => s.Tags);
            supplier.EnumProperty(s => s.Country);

            var productFamily = families.EntityType;
            productFamily.HasKey(pf => pf.ID);
            productFamily.Property(pf => pf.Name);
            productFamily.Property(pf => pf.Description);

            // Create relationships and bindings in one go
            products.HasRequiredBinding(p => p.Family, families);
            families.HasManyBinding(pf => pf.Products, products);
            families.HasOptionalBinding(pf => pf.Supplier, suppliers);
            suppliers.HasManyBinding(s => s.ProductFamilies, families);

            // Create navigation Link builders
            products.HasNavigationPropertiesLink(
                product.NavigationProperties,
                (entityContext, navigationProperty) =>
                {
                    object id;
                    entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                    return new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName,
                new
                {
                    odataPath = entityContext.Url.CreateODataLink(
                        new EntitySetPathSegment(entityContext.NavigationSource.Name),
                        new KeyValuePathSegment(id.ToString()),
                        new NavigationPathSegment(navigationProperty))
                }));
                }, true);

            families.HasNavigationPropertiesLink(
                productFamily.NavigationProperties,
                (entityContext, navigationProperty) =>
                {
                    object id;
                    entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                    return new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName,
                new
                {
                    odataPath = entityContext.Url.CreateODataLink(
                        new EntitySetPathSegment(entityContext.NavigationSource.Name),
                        new KeyValuePathSegment(id.ToString()),
                        new NavigationPathSegment(navigationProperty))
                }));
                }, true);

            suppliers.HasNavigationPropertiesLink(
                supplier.NavigationProperties,
                (entityContext, navigationProperty) =>
                {
                    object id;
                    entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                    return new Uri(entityContext.Url.Link(
                ODataTestConstants.DefaultRouteName,
                new
                {
                    odataPath = entityContext.Url.CreateODataLink(
                        new EntitySetPathSegment(entityContext.NavigationSource.Name),
                        new KeyValuePathSegment(id.ToString()),
                        new NavigationPathSegment(navigationProperty))
                }));
                }, true);

            return modelBuilder.GetEdmModel();
        }
예제 #32
0
        private static IEdmModel GetExplicitEdmModel()
        {
            var modelBuilder = new ODataModelBuilder();

            var enumContry = modelBuilder.EnumType <CountryOrRegion>();

            enumContry.Member(CountryOrRegion.Canada);
            enumContry.Member(CountryOrRegion.China);
            enumContry.Member(CountryOrRegion.India);
            enumContry.Member(CountryOrRegion.Japen);
            enumContry.Member(CountryOrRegion.USA);

            var products            = modelBuilder.EntitySet <Product>("Products");
            var toiletPaperSupplier = modelBuilder.EntityType <ToiletPaperSupplier>();

            products.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var mainSupplier = modelBuilder.Singleton <Supplier>("MainSupplier");

            var suppliers = modelBuilder.EntitySet <Supplier>("Suppliers").HasDerivedTypeConstraint <ToiletPaperSupplier>();

            mainSupplier.HasDerivedTypeConstraints(typeof(ToiletPaperSupplier));

            suppliers.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var families = modelBuilder.EntitySet <ProductFamily>("ProductFamilies");

            families.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var product = products.EntityType;

            product.HasKey(p => p.ID);
            product.Property(p => p.Name);
            product.Property(p => p.ReleaseDate);
            product.Property(p => p.SupportedUntil);

            var address = modelBuilder.ComplexType <Address>();

            address.Property(a => a.City);
            address.Property(a => a.CountryOrRegion);
            address.Property(a => a.State);
            address.Property(a => a.Street);
            address.Property(a => a.ZipCode);

            var supplier = suppliers.EntityType;

            supplier.HasKey(s => s.ID);
            supplier.Property(s => s.Name);
            supplier.CollectionProperty(s => s.Addresses);
            supplier.CollectionProperty(s => s.Tags);
            supplier.EnumProperty(s => s.CountryOrRegion);
            supplier.ComplexProperty(s => s.MainAddress).HasDerivedTypeConstraints(typeof(Address));

            var productFamily = families.EntityType;

            productFamily.HasKey(pf => pf.ID);
            productFamily.Property(pf => pf.Name);
            productFamily.Property(pf => pf.Description);

            // Create relationships and bindings in one go
            products.HasRequiredBinding(p => p.Family, families);
            families.HasManyBinding(pf => pf.Products, products);
            families.HasOptionalBinding(pf => pf.Supplier, suppliers).NavigationProperty.HasDerivedTypeConstraint <ToiletPaperSupplier>();
            suppliers.HasManyBinding(s => s.ProductFamilies, families);

            // Create navigation Link builders
            products.HasNavigationPropertiesLink(
                product.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                      new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            families.HasNavigationPropertiesLink(
                productFamily.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(ODataTestConstants.DefaultRouteName,
                                                                 new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                      new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            suppliers.HasNavigationPropertiesLink(
                supplier.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.GetUrlHelper().Link(
                                   ODataTestConstants.DefaultRouteName,
                                   new
                {
                    odataPath = ResourceContextHelper.CreateODataLink(entityContext,
                                                                      new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                                                                      new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                                                                      new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            var function = supplier.Function("GetAddress").Returns <Address>().HasDerivedTypeConstraintForReturnType <Address>();

            function.ReturnTypeConstraints.Location = Microsoft.OData.Edm.Csdl.EdmVocabularyAnnotationSerializationLocation.OutOfLine;
            function.Parameter <int>("value");

            var action = modelBuilder.Action("GetAddress").Returns <Address>().HasDerivedTypeConstraintsForReturnType(typeof(Address));

            action.Parameter <Supplier>("supplier").HasDerivedTypeConstraint <ToiletPaperSupplier>();
            action.ReturnTypeConstraints.Location = Microsoft.OData.Edm.Csdl.EdmVocabularyAnnotationSerializationLocation.OutOfLine;

            return(modelBuilder.GetEdmModel());
        }
예제 #33
0
        public static IEdmModel GetEdmModel2()
        {
            var builder = new ODataModelBuilder();

            // enum type
            var color = builder.EnumType <Color>();

            color.Member(Color.Red);
            color.Member(Color.Blue);
            color.Member(Color.Green);

            // complex type
            // var address = builder.ComplexType<Address>().Abstract();
            var address = builder.ComplexType <Address>();

            address.Property(a => a.Country);
            address.Property(a => a.City);
            // address.HasDynamicProperties(a => a.Dynamics);

            var subAddress = builder.ComplexType <SubAddress>().DerivesFrom <Address>();

            subAddress.Property(s => s.Street);

            // entity type
            // var customer = builder.EntityType<Customer>().Abstract();
            var customer = builder.EntityType <Customer>();

            customer.HasKey(c => c.CustomerId);
            customer.ComplexProperty(c => c.Location);
            customer.HasMany(c => c.Orders);
            // customer.HasDynamicProperties(c => c.Dynamics);

            var order = builder.EntityType <Order>();

            order.HasKey(o => o.OrderId);
            order.Property(o => o.Token);

            // entity set
            builder.EntitySet <Customer>("Customers");
            builder.EntitySet <Order>("Orders");

            // function
            var function = customer.Function("BoundFunction").Returns <string>();

            function.Parameter <int>("value");
            function.Parameter <Address>("address");

            function = builder.Function("UnBoundFunction").Returns <int>();
            function.Parameter <Color>("color");
            function.EntityParameter <Order>("order");

            // action
            var action = customer.Collection.Action("BoundAction");

            action.Parameter <int>("value");
            action.CollectionParameter <Address>("addresses");

            action = builder.Action("UnBoundAction").Returns <int>();
            action.Parameter <Color>("color");
            action.CollectionEntityParameter <Order>("orders");

            return(builder.GetEdmModel());
        }
예제 #34
0
        public void ValueOfEnumMemberCannotBeConvertedToLongShouldThrowException()
        {
            // Arrange
            var builder = new ODataModelBuilder();
            var color = builder.EnumType<ValueOutOfRangeEnum>();
            color.Member(ValueOutOfRangeEnum.Member);

            // Act & Assert
            Assert.ThrowsArgument(
                () => builder.GetServiceModel(),
                "value",
                "The value of enum member 'Member' cannot be converted to a long type.");
        }
예제 #35
0
        private static IEdmModel GetExplicitEdmModel()
        {
            var modelBuilder = new ODataModelBuilder();

            var enumContry = modelBuilder.EnumType <Country>();

            enumContry.Member(Country.Canada);
            enumContry.Member(Country.China);
            enumContry.Member(Country.India);
            enumContry.Member(Country.Japen);
            enumContry.Member(Country.USA);

            var products = modelBuilder.EntitySet <Product>("Products");

            products.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName,
                                                      new
                {
                    odataPath = entityContext.Url.CreateODataLink(
                        new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                        new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var suppliers = modelBuilder.EntitySet <Supplier>("Suppliers");

            suppliers.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName,
                                                      new
                {
                    odataPath = entityContext.Url.CreateODataLink(
                        new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                        new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var families = modelBuilder.EntitySet <ProductFamily>("ProductFamilies");

            families.HasEditLink(entityContext =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName,
                                                      new
                {
                    odataPath = entityContext.Url.CreateODataLink(
                        new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                        new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null))
                })));
            }, true);

            var product = products.EntityType;

            product.HasKey(p => p.ID);
            product.Property(p => p.Name);
            product.Property(p => p.ReleaseDate);
            product.Property(p => p.SupportedUntil);

            var address = modelBuilder.ComplexType <Address>();

            address.Property(a => a.City);
            address.Property(a => a.Country);
            address.Property(a => a.State);
            address.Property(a => a.Street);
            address.Property(a => a.ZipCode);

            var supplier = suppliers.EntityType;

            supplier.HasKey(s => s.ID);
            supplier.Property(s => s.Name);
            supplier.CollectionProperty(s => s.Addresses);
            supplier.CollectionProperty(s => s.Tags);
            supplier.EnumProperty(s => s.Country);

            var productFamily = families.EntityType;

            productFamily.HasKey(pf => pf.ID);
            productFamily.Property(pf => pf.Name);
            productFamily.Property(pf => pf.Description);

            // Create relationships and bindings in one go
            products.HasRequiredBinding(p => p.Family, families);
            families.HasManyBinding(pf => pf.Products, products);
            families.HasOptionalBinding(pf => pf.Supplier, suppliers);
            suppliers.HasManyBinding(s => s.ProductFamilies, families);

            // Create navigation Link builders
            products.HasNavigationPropertiesLink(
                product.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName,
                                                      new
                {
                    odataPath = entityContext.Url.CreateODataLink(
                        new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                        new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                        new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            families.HasNavigationPropertiesLink(
                productFamily.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.Url.Link(ODataTestConstants.DefaultRouteName,
                                                      new
                {
                    odataPath = entityContext.Url.CreateODataLink(
                        new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                        new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                        new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            suppliers.HasNavigationPropertiesLink(
                supplier.NavigationProperties,
                (entityContext, navigationProperty) =>
            {
                object id;
                entityContext.EdmObject.TryGetPropertyValue("ID", out id);
                return(new Uri(entityContext.Url.Link(
                                   ODataTestConstants.DefaultRouteName,
                                   new
                {
                    odataPath = entityContext.Url.CreateODataLink(
                        new EntitySetSegment(entityContext.NavigationSource as IEdmEntitySet),
                        new KeySegment(new[] { new KeyValuePair <string, object>("ID", id) }, entityContext.StructuredType as IEdmEntityType, null),
                        new NavigationPropertySegment(navigationProperty, null))
                })));
            }, true);

            return(modelBuilder.GetEdmModel());
        }
        private static IEdmModel GetEdmModel()
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            // Configure LimitedEntity
            EntitySetConfiguration<LimitedEntity> limitedEntities = builder.EntitySet<LimitedEntity>("LimitedEntities");
            limitedEntities.EntityType.HasKey(p => p.Id);
            limitedEntities.EntityType.ComplexProperty(c => c.ComplexProperty);
            limitedEntities.EntityType.CollectionProperty(c => c.ComplexCollectionProperty).IsNotCountable();
            limitedEntities.EntityType.HasMany(l => l.EntityCollectionProperty).IsNotCountable();
            limitedEntities.EntityType.CollectionProperty(cp => cp.Integers).IsNotCountable();

            // Configure LimitedRelatedEntity
            EntitySetConfiguration<LimitedRelatedEntity> limitedRelatedEntities =
                builder.EntitySet<LimitedRelatedEntity>("LimitedRelatedEntities");
            limitedRelatedEntities.EntityType.HasKey(p => p.Id);
            limitedRelatedEntities.EntityType.CollectionProperty(p => p.ComplexCollectionProperty).IsNotCountable();

            // Configure Complextype
            ComplexTypeConfiguration<LimitedComplex> complexType = builder.ComplexType<LimitedComplex>();
            complexType.CollectionProperty(p => p.Strings).IsNotCountable();
            complexType.Property(p => p.Value);
            complexType.CollectionProperty(p => p.SimpleEnums).IsNotCountable();

            // Configure EnumType
            EnumTypeConfiguration<SimpleEnum> enumType = builder.EnumType<SimpleEnum>();
            enumType.Member(SimpleEnum.First);
            enumType.Member(SimpleEnum.Second);
            enumType.Member(SimpleEnum.Third);
            enumType.Member(SimpleEnum.Fourth);

            return builder.GetEdmModel();
        }
예제 #37
0
        public static IEdmModel GetEdmModel()
        {
            if (_model == null)
            {
                ODataModelBuilder model = new ODataModelBuilder();

                var color = model.EnumType<Color>();
                color.Member(Color.Red);
                color.Member(Color.Green);
                color.Member(Color.Blue);

                var people = model.EntitySet<FormatterPerson>("People");
                people.HasFeedSelfLink(context => new Uri(context.Url.CreateODataLink(new EntitySetPathSegment(
                    context.EntitySetBase))));
                people.HasIdLink(context =>
                    {
                        return new Uri(context.Url.CreateODataLink(
                            new EntitySetPathSegment(context.NavigationSource as IEdmEntitySet),
                            new KeyValuePathSegment(context.GetPropertyValue("PerId").ToString())));
                    },
                    followsConventions: false);

                var person = people.EntityType;
                person.HasKey(p => p.PerId);
                person.Property(p => p.Age);
                person.Property(p => p.MyGuid);
                person.Property(p => p.Name);
                person.EnumProperty(p => p.FavoriteColor);
                person.ComplexProperty<FormatterOrder>(p => p.Order);

                var order = model.ComplexType<FormatterOrder>();
                order.Property(o => o.OrderAmount);
                order.Property(o => o.OrderName);

                // Add a top level function without parameter and the "IncludeInServiceDocument = true"
                var getPerson = model.Function("GetPerson");
                getPerson.ReturnsFromEntitySet<FormatterPerson>("People");
                getPerson.IncludeInServiceDocument = true;

                // Add a top level function without parameter and the "IncludeInServiceDocument = false"
                var getAddress = model.Function("GetAddress");
                getAddress.Returns<string>();
                getAddress.IncludeInServiceDocument = false;

                // Add an overload top level function with parameters and the "IncludeInServiceDocument = true"
                getPerson = model.Function("GetPerson");
                getPerson.Parameter<int>("PerId");
                getPerson.ReturnsFromEntitySet<FormatterPerson>("People");
                getPerson.IncludeInServiceDocument = true;

                // Add an overload top level function with parameters and the "IncludeInServiceDocument = false"
                getAddress = model.Function("GetAddress");
                getAddress.Parameter<int>("AddressId");
                getAddress.Returns<string>();
                getAddress.IncludeInServiceDocument = false;

                // Add an overload top level function
                var getVipPerson = model.Function("GetVipPerson");
                getVipPerson.Parameter<string>("name");
                getVipPerson.ReturnsFromEntitySet<FormatterPerson>("People");
                getVipPerson.IncludeInServiceDocument = true;

                // Add a top level function which is included in service document
                getVipPerson = model.Function("GetVipPerson");
                getVipPerson.ReturnsFromEntitySet<FormatterPerson>("People");
                getVipPerson.IncludeInServiceDocument = true;

                // Add an overload top level function
                getVipPerson = model.Function("GetVipPerson");
                getVipPerson.Parameter<int>("PerId");
                getVipPerson.Parameter<string>("name");
                getVipPerson.ReturnsFromEntitySet<FormatterPerson>("People");
                getVipPerson.IncludeInServiceDocument = true;

                // Add a top level function with parameters and without any overload
                var getSalary = model.Function("GetSalary");
                getSalary.Parameter<int>("PerId");
                getSalary.Parameter<string>("month");
                getSalary.Returns<int>();
                getSalary.IncludeInServiceDocument = true;

                // Add Singleton
                var president = model.Singleton<FormatterPerson>("President");
                president.HasIdLink(context =>
                    {
                        return new Uri(context.Url.CreateODataLink(new SingletonPathSegment((IEdmSingleton)context.NavigationSource)));
                    },
                    followsConventions: false);

                _model = model.GetEdmModel();
            }

            return _model;
        }
예제 #38
0
        public static IEdmModel GetEdmModel()
        {
            if (_model == null)
            {
                ODataModelBuilder model = new ODataModelBuilder();

                var color = model.EnumType <Color>();
                color.Member(Color.Red);
                color.Member(Color.Green);
                color.Member(Color.Blue);

                var people = model.EntitySet <FormatterPerson>("People");

                people.HasFeedSelfLink(context => new Uri(context.InternalUrlHelper.CreateODataLink(new EntitySetSegment(
                                                                                                        context.EntitySetBase as IEdmEntitySet))));
                people.HasIdLink(context =>
                {
                    var keys = new[] { new KeyValuePair <string, object>("PerId", context.GetPropertyValue("PerId")) };
                    return(new Uri(context.InternalUrlHelper.CreateODataLink(
                                       new EntitySetSegment(context.NavigationSource as IEdmEntitySet),
                                       new KeySegment(keys, context.StructuredType as IEdmEntityType, context.NavigationSource))));
                },
                                 followsConventions: false);

                var person = people.EntityType;
                person.HasKey(p => p.PerId);
                person.Property(p => p.Age);
                person.Property(p => p.MyGuid);
                person.Property(p => p.Name);
                person.EnumProperty(p => p.FavoriteColor);
                person.ComplexProperty <FormatterOrder>(p => p.Order);

                var order = model.ComplexType <FormatterOrder>();
                order.Property(o => o.OrderAmount);
                order.Property(o => o.OrderName);

                // Add a top level function without parameter and the "IncludeInServiceDocument = true"
                var getPerson = model.Function("GetPerson");
                getPerson.ReturnsFromEntitySet <FormatterPerson>("People");
                getPerson.IncludeInServiceDocument = true;

                // Add a top level function without parameter and the "IncludeInServiceDocument = false"
                var getAddress = model.Function("GetAddress");
                getAddress.Returns <string>();
                getAddress.IncludeInServiceDocument = false;

                // Add an overload top level function with parameters and the "IncludeInServiceDocument = true"
                getPerson = model.Function("GetPerson");
                getPerson.Parameter <int>("PerId");
                getPerson.ReturnsFromEntitySet <FormatterPerson>("People");
                getPerson.IncludeInServiceDocument = true;

                // Add an overload top level function with parameters and the "IncludeInServiceDocument = false"
                getAddress = model.Function("GetAddress");
                getAddress.Parameter <int>("AddressId");
                getAddress.Returns <string>();
                getAddress.IncludeInServiceDocument = false;

                // Add an overload top level function
                var getVipPerson = model.Function("GetVipPerson");
                getVipPerson.Parameter <string>("name");
                getVipPerson.ReturnsFromEntitySet <FormatterPerson>("People");
                getVipPerson.IncludeInServiceDocument = true;

                // Add a top level function which is included in service document
                getVipPerson = model.Function("GetVipPerson");
                getVipPerson.ReturnsFromEntitySet <FormatterPerson>("People");
                getVipPerson.IncludeInServiceDocument = true;

                // Add an overload top level function
                getVipPerson = model.Function("GetVipPerson");
                getVipPerson.Parameter <int>("PerId");
                getVipPerson.Parameter <string>("name");
                getVipPerson.ReturnsFromEntitySet <FormatterPerson>("People");
                getVipPerson.IncludeInServiceDocument = true;

                // Add a top level function with parameters and without any overload
                var getSalary = model.Function("GetSalary");
                getSalary.Parameter <int>("PerId");
                getSalary.Parameter <string>("month");
                getSalary.Returns <int>();
                getSalary.IncludeInServiceDocument = true;

                // Add a function to test namespace configuration
                var getNSFunction = model.Function("GetNS");
                getNSFunction.Returns <int>();
                getNSFunction.Namespace = "CustomizeNamepace";

                // Add Singleton
                var president = model.Singleton <FormatterPerson>("President");
                president.HasIdLink(context =>
                {
                    return(new Uri(context.InternalUrlHelper.CreateODataLink(new SingletonSegment((IEdmSingleton)context.NavigationSource))));
                },
                                    followsConventions: false);

                _model = model.GetEdmModel();
            }

            return(_model);
        }