Example #1
0
        public void CanCreateFunctionWithNonbindingParametersAsNullable()
        {
            // Arrange & Act
            ODataModelBuilder     builder  = new ODataModelBuilder();
            FunctionConfiguration function = builder.Function("MyFunction");

            function.Parameter <string>("p0");
            function.Parameter <string>("p1").Nullable = false;
            function.Parameter <int>("p2").Nullable    = true;
            function.Parameter <int>("p3");
            function.Parameter <Address>("p4");
            function.Parameter <Address>("p5").Nullable = false;

            function.CollectionParameter <ZipCode>("p6");
            function.CollectionParameter <ZipCode>("p7").Nullable = false;

            Dictionary <string, ParameterConfiguration> parameters = function.Parameters.ToDictionary(e => e.Name, e => e);

            // Assert
            Assert.True(parameters["p0"].Nullable);
            Assert.False(parameters["p1"].Nullable);

            Assert.True(parameters["p2"].Nullable);
            Assert.False(parameters["p3"].Nullable);

            Assert.True(parameters["p4"].Nullable);
            Assert.False(parameters["p5"].Nullable);

            Assert.True(parameters["p6"].Nullable);
            Assert.False(parameters["p7"].Nullable);
        }
Example #2
0
        public void CanCreateFunctionWithNonbindingParameters()
        {
            // Arrange & Act
            ODataModelBuilder     builder  = new ODataModelBuilder();
            FunctionConfiguration function = builder.Function("MyFunction");

            function.Parameter <string>("p0");
            function.Parameter <int>("p1");
            function.Parameter <Address>("p2");
            function.CollectionParameter <string>("p3");
            function.CollectionParameter <int>("p4");
            function.CollectionParameter <ZipCode>("p5");
            function.EntityParameter <Customer>("p6");
            function.CollectionEntityParameter <Employee>("p7");
            ParameterConfiguration[]   parameters   = function.Parameters.ToArray();
            ComplexTypeConfiguration[] complexTypes = builder.StructuralTypes.OfType <ComplexTypeConfiguration>().ToArray();
            EntityTypeConfiguration[]  entityTypes  = builder.StructuralTypes.OfType <EntityTypeConfiguration>().ToArray();

            // Assert
            Assert.Equal(2, complexTypes.Length);
            Assert.Equal(typeof(Address).FullName, complexTypes[0].FullName);
            Assert.Equal(typeof(ZipCode).FullName, complexTypes[1].FullName);

            Assert.Equal(2, entityTypes.Length);
            Assert.Equal(typeof(Customer).FullName, entityTypes[0].FullName);
            Assert.Equal(typeof(Employee).FullName, entityTypes[1].FullName);

            Assert.Equal(8, parameters.Length);
            Assert.Equal("p0", parameters[0].Name);
            Assert.Equal("Edm.String", parameters[0].TypeConfiguration.FullName);
            Assert.Equal("p1", parameters[1].Name);
            Assert.Equal("Edm.Int32", parameters[1].TypeConfiguration.FullName);
            Assert.Equal("p2", parameters[2].Name);
            Assert.Equal(typeof(Address).FullName, parameters[2].TypeConfiguration.FullName);
            Assert.Equal("p3", parameters[3].Name);
            Assert.Equal("Collection(Edm.String)", parameters[3].TypeConfiguration.FullName);
            Assert.Equal("p4", parameters[4].Name);
            Assert.Equal("Collection(Edm.Int32)", parameters[4].TypeConfiguration.FullName);
            Assert.Equal("p5", parameters[5].Name);
            Assert.Equal(string.Format("Collection({0})", typeof(ZipCode).FullName), parameters[5].TypeConfiguration.FullName);

            Assert.Equal("p6", parameters[6].Name);
            Assert.Equal(typeof(Customer).FullName, parameters[6].TypeConfiguration.FullName);

            Assert.Equal("p7", parameters[7].Name);
            Assert.Equal(string.Format("Collection({0})", typeof(Employee).FullName), parameters[7].TypeConfiguration.FullName);
        }
Example #3
0
        public void CanCreateEdmModel_ForBindableFunction_WithSupportedParameterType()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <Customer> customer = builder.EntityType <Customer>();

            customer.HasKey(c => c.CustomerId);
            customer.Property(c => c.Name);

            // Act
            FunctionConfiguration functionBuilder = customer.Function("FunctionName");

            functionBuilder.Parameter <int>("primitive");
            functionBuilder.CollectionParameter <int>("collectionPrimitive");

            functionBuilder.Parameter <bool?>("nullablePrimitive");
            functionBuilder.CollectionParameter <bool?>("nullableCollectionPrimitive");

            functionBuilder.Parameter <Color>("enum");
            functionBuilder.CollectionParameter <Color>("collectionEnum");

            functionBuilder.Parameter <Color?>("nullableEnum");
            functionBuilder.CollectionParameter <Color?>("nullableCollectionEnum");

            functionBuilder.Parameter <Address>("complex");
            functionBuilder.CollectionParameter <Address>("collectionComplex");

            functionBuilder.EntityParameter <Customer>("entity");
            functionBuilder.CollectionEntityParameter <Customer>("collectionEntity");

            functionBuilder.Returns <bool>();

            IEdmModel model = builder.GetEdmModel();

            // Assert
            Assert.Single(model.SchemaElements.OfType <IEdmFunction>());
            IEdmFunction function = Assert.Single(model.SchemaElements.OfType <IEdmFunction>());

            Assert.False(function.IsComposable);
            Assert.True(function.IsBound);
            Assert.Equal("FunctionName", function.Name);
            Assert.NotNull(function.ReturnType);

            Assert.Equal(13, function.Parameters.Count());

            function.AssertHasParameter(model, BindingParameterConfiguration.DefaultBindingParameterName, typeof(Customer), true);

            function.AssertHasParameter(model, parameterName: "primitive", parameterType: typeof(int), isNullable: false);
            function.AssertHasParameter(model, parameterName: "collectionPrimitive", parameterType: typeof(IList <int>), isNullable: false);

            function.AssertHasParameter(model, parameterName: "nullablePrimitive", parameterType: typeof(bool?), isNullable: true);
            function.AssertHasParameter(model, parameterName: "nullableCollectionPrimitive", parameterType: typeof(IList <bool?>), isNullable: true);

            function.AssertHasParameter(model, parameterName: "enum", parameterType: typeof(Color), isNullable: false);
            function.AssertHasParameter(model, parameterName: "collectionEnum", parameterType: typeof(IList <Color>), isNullable: false);

            function.AssertHasParameter(model, parameterName: "nullableEnum", parameterType: typeof(Color?), isNullable: true);
            function.AssertHasParameter(model, parameterName: "nullableCollectionEnum", parameterType: typeof(IList <Color?>), isNullable: true);

            function.AssertHasParameter(model, parameterName: "complex", parameterType: typeof(Address), isNullable: true);
            function.AssertHasParameter(model, parameterName: "collectionComplex", parameterType: typeof(IList <Address>), isNullable: true);

            function.AssertHasParameter(model, parameterName: "entity", parameterType: typeof(Customer), isNullable: true);
            function.AssertHasParameter(model, parameterName: "collectionEntity", parameterType: typeof(IList <Customer>), isNullable: true);
        }
        public static IEdmModel GetEdmModel(ODataConventionModelBuilder builder)
        {
            builder.EntitySet <ConventionCustomer>("ConventionCustomers");
            builder.EntitySet <ConventionOrder>("ConventionOrders");

            EnumTypeConfiguration <ConventionGender> enumType = builder.EnumType <ConventionGender>();

            enumType.Member(ConventionGender.Female);
            enumType.Member(ConventionGender.Male);
            #region functions

            FunctionConfiguration getAllCustomers = builder.Function("GetAllConventionCustomers");
            getAllCustomers.ReturnsCollectionFromEntitySet <ConventionCustomer>("ConventionCustomers");
            getAllCustomers.IsComposable = true;

            // Return all the customers whose name contains CustomerName
            FunctionConfiguration getAllCustomersOverload = builder.Function("GetAllConventionCustomers");
            getAllCustomersOverload.ReturnsCollectionFromEntitySet <ConventionCustomer>("ConventionCustomers");
            getAllCustomersOverload.Parameter <string>("CustomerName");
            getAllCustomersOverload.IsComposable = true;

            FunctionConfiguration getCustomersById = builder.Function("GetConventionCustomerById");
            getCustomersById.Parameter <int>("CustomerId");
            getCustomersById.ReturnsFromEntitySet <ConventionCustomer>("ConventionCustomers");
            getCustomersById.IsComposable = true;

            FunctionConfiguration getOrder = builder.Function("GetConventionOrderByCustomerIdAndOrderName");
            getOrder.Parameter <int>("CustomerId");
            getOrder.Parameter <string>("OrderName");
            getOrder.ReturnsFromEntitySet <ConventionOrder>("ConventionOrders");

            FunctionConfiguration getCustomerNameById = builder.Function("GetConventionCustomerNameById");
            getCustomerNameById.Parameter <int>("CustomerId");
            getCustomerNameById.Returns <string>();

            FunctionConfiguration getDefinedGenders = builder.Function("GetDefinedGenders");
            getDefinedGenders.ReturnsCollection <ConventionGender>()
            .IsComposable = true;

            FunctionConfiguration function = builder.Function("AdvancedFunction").Returns <bool>();
            function.CollectionParameter <int>("nums");
            function.CollectionParameter <ConventionGender>("genders");
            function.Parameter <ConventionAddress>("location");
            function.CollectionParameter <ConventionAddress>("addresses");
            function.EntityParameter <ConventionCustomer>("customer");
            function.CollectionEntityParameter <ConventionCustomer>("customers");

            #endregion

            #region actions

            ActionConfiguration resetDataSource = builder.Action("ResetDataSource");

            // bug: error message:  non-binding parameter type must be either Primitive, Complex, Collection of Primitive or a Collection of Complex.

            /*
             * ActionConfiguration createCustomer = builder.Action("CreateCustomer");
             * createCustomer.Parameter<ConventionCustomer>("Customer");
             * createCustomer.ReturnsFromEntitySet<ConventionCustomer>("ConventionCustomers");
             */

            ActionConfiguration udpateAddress = builder.Action("UpdateAddress");
            udpateAddress.Parameter <ConventionAddress>("Address");
            udpateAddress.Parameter <int>("ID");
            udpateAddress.ReturnsCollectionFromEntitySet <ConventionCustomer>("ConventionCustomers");

            ActionConfiguration action = builder.Action("AdvancedAction");
            action.CollectionParameter <int>("nums");
            action.CollectionParameter <ConventionGender>("genders");
            action.Parameter <ConventionAddress>("location");
            action.CollectionParameter <ConventionAddress>("addresses");
            action.EntityParameter <ConventionCustomer>("customer");
            action.CollectionEntityParameter <ConventionCustomer>("customers");

            #endregion

            var schemaNamespace = typeof(ConventionCustomer).Namespace;

            builder.Namespace = schemaNamespace;

            var edmModel  = builder.GetEdmModel();
            var container = edmModel.EntityContainer as EdmEntityContainer;

            #region function imports

            var entitySet = container.FindEntitySet("ConventionCustomers");
            var getCustomersByIdOfEdmFunction = edmModel.FindDeclaredOperations(schemaNamespace + ".GetConventionCustomerById").First() as EdmFunction;
            container.AddFunctionImport("GetConventionCustomerByIdImport", getCustomersByIdOfEdmFunction, new EdmPathExpression(entitySet.Name));

            var functionsOfGetAllConventionCustomers   = edmModel.FindDeclaredOperations(schemaNamespace + ".GetAllConventionCustomers");
            var getAllConventionCustomersOfEdmFunction = functionsOfGetAllConventionCustomers.FirstOrDefault(f => f.Parameters.Count() == 0) as EdmFunction;
            container.AddFunctionImport("GetAllConventionCustomersImport", getAllConventionCustomersOfEdmFunction, new EdmPathExpression(entitySet.Name));

            // TODO delete this overload after bug 1640 is fixed: It can not find the correct overload function if the the function is exposed as a function import.
            var getAllConventionCustomersOverloadOfEdmFunction = functionsOfGetAllConventionCustomers.FirstOrDefault(f => f.Parameters.Count() > 0) as EdmFunction;
            container.AddFunctionImport("GetAllConventionCustomersImport", getAllConventionCustomersOverloadOfEdmFunction, new EdmPathExpression(entitySet.Name));

            var entitySet1 = container.FindEntitySet("ConventionOrders");
            var GetConventionOrderByCustomerIdAndOrderNameOfEdmFunction = edmModel.FindDeclaredOperations(schemaNamespace + ".GetConventionOrderByCustomerIdAndOrderName").First() as EdmFunction;
            container.AddFunctionImport("GetConventionOrderByCustomerIdAndOrderNameImport", GetConventionOrderByCustomerIdAndOrderNameOfEdmFunction, new EdmPathExpression(entitySet1.Name));

            var getConventionCustomerNameByIdOfEdmFunction = edmModel.FindDeclaredOperations(schemaNamespace + ".GetConventionCustomerNameById").First() as EdmFunction;
            container.AddFunctionImport("GetConventionCustomerNameByIdImport", getConventionCustomerNameByIdOfEdmFunction, null);

            #endregion

            #region action imports

            var resetDataSourceOfEdmAction = edmModel.FindDeclaredOperations(schemaNamespace + ".ResetDataSource").FirstOrDefault() as EdmAction;
            container.AddActionImport("ResetDataSourceImport", resetDataSourceOfEdmAction);

            // TODO: it is a potential issue that entity can not be used as an un-bound parameter.

            /*
             * var createCustomerOfEdmAction = edmModel.FindDeclaredOperations(schemaNamespace + ".CreateCustomer").FirstOrDefault() as EdmAction;
             * container.AddActionImport("CreateCustomerImport", createCustomerOfEdmAction);
             */
            var updateAddressOfEdmAction = edmModel.FindDeclaredOperations(schemaNamespace + ".UpdateAddress").FirstOrDefault() as EdmAction;
            container.AddActionImport("UpdateAddressImport", updateAddressOfEdmAction, new EdmPathExpression(entitySet.Name));

            #endregion

            return(edmModel);
        }