示例#1
0
        public static IEdmModel GetEdmModel(WebRouteConfiguration configuration)
        {
            ODataConventionModelBuilder        builder = configuration.CreateConventionModelBuilder();
            EntitySetConfiguration <Employee>  entitySetConfiguration            = builder.EntitySet <Employee>("Employees");
            EntityTypeConfiguration <Manager>  entityTypeConfigurationOfManager  = builder.EntityType <Manager>();
            EntityTypeConfiguration <Employee> entityTypeConfigurationOfEmployee = builder.EntityType <Employee>();

            #region functions

            // Function bound to a collection of base EntityType.
            entityTypeConfigurationOfEmployee.Collection.Function("GetCount")
            .Returns <int>();

            // Overload
            entityTypeConfigurationOfEmployee.Collection.Function("GetCount")
            .Returns <int>()
            .Parameter <string>("Name");

            // Overload with one optional parameter
            var salaryRangeCount = entityTypeConfigurationOfEmployee.Collection.Function("GetWholeSalary")
                                   .Returns <int>();
            salaryRangeCount.Parameter <double>("minSalary");
            salaryRangeCount.Parameter <double>("maxSalary").Optional();
            salaryRangeCount.Parameter <double>("aveSalary").HasDefaultValue("8.9");

            // Function bound to a collection of derived EntityType.
            entityTypeConfigurationOfManager.Collection.Function("GetCount")
            .Returns <int>();

            // Function bound to an base EntityType
            entityTypeConfigurationOfEmployee.Function("GetEmailsCount")
            .Returns <int>();

            entityTypeConfigurationOfEmployee.Function("GetOptionalAddresses")
            .ReturnsCollection <Address>()
            .IsComposable = true;

            entityTypeConfigurationOfEmployee.Function("GetEmails")
            .ReturnsCollection <string>()
            .IsComposable = false;

            // Function bound to a derived EntityType
            entityTypeConfigurationOfManager.Function("GetEmailsCount")
            .Returns <int>();

            // Function with primitive and collection of primitive parameters
            var function = entityTypeConfigurationOfEmployee.Collection.Function("PrimitiveFunction").Returns <string>();
            function.Parameter <int>("param");
            function.Parameter <double?>("price");          // nullable
            function.Parameter <string>("name");            // nullable
            function.CollectionParameter <string>("names"); // collection with nullable element

            // Function with Enum and collection of Enum parameters
            function = entityTypeConfigurationOfEmployee.Collection.Function("EnumFunction").Returns <string>();
            function.Parameter <Color>("bkColor");
            function.Parameter <Color?>("frColor");         // nullable
            function.CollectionParameter <Color>("colors"); // collection with non-nullable element

            // Function with complex and collection of complex parameters
            function = entityTypeConfigurationOfEmployee.Collection.Function("ComplexFunction").ReturnsCollection <Address>();
            function.Parameter <Address>("address").Nullable = false;
            function.Parameter <Address>("location");            // nullable
            function.CollectionParameter <Address>("addresses"); // collection with nullable element

            // Function with entity and collection of entity parameters
            function = entityTypeConfigurationOfEmployee.Collection.Function("EntityFunction").Returns <string>();
            function.EntityParameter <Employee>("person").Nullable = false;
            function.EntityParameter <Employee>("guard");           // nullable
            function.CollectionEntityParameter <Employee>("staff"); // collection with nullable element

            #endregion

            #region actions

            // Action bound to a collection of base EntityType
            entityTypeConfigurationOfEmployee.Collection.Action("IncreaseSalary")
            .ReturnsCollectionFromEntitySet(entitySetConfiguration)
            .Parameter <string>("Name");

            // Action bound to a collection of derived EntityType
            entityTypeConfigurationOfManager.Collection.Action("IncreaseSalary")
            .ReturnsCollectionFromEntitySet(entitySetConfiguration)
            .Parameter <string>("Name");

            // Action bound to a base EntityType
            entityTypeConfigurationOfEmployee.Action("IncreaseSalary")
            .Returns <int>();

            // Action bound to a derived EntityType
            entityTypeConfigurationOfManager.Action("IncreaseSalary")
            .Returns <int>();

            // Action with optional parameters
            var action = entityTypeConfigurationOfManager.Action("IncreaseWholeSalary")
                         .Returns <int>();
            action.Parameter <double>("minSalary");
            action.Parameter <double>("maxSalary").Optional();
            action.Parameter <double>("aveSalary").HasDefaultValue("8.9");

            // Action with primitive and collection of primitive parameters
            action = entityTypeConfigurationOfEmployee.Collection.Action("PrimitiveAction");
            action.Parameter <int>("param");
            action.Parameter <double?>("price");          // nullable
            action.Parameter <string>("name");            // nullable
            action.CollectionParameter <string>("names"); // collection with nullable element

            // Action with Enum and collection of Enum parameters
            action = entityTypeConfigurationOfEmployee.Collection.Action("EnumAction");
            action.Parameter <Color>("bkColor");
            action.Parameter <Color?>("frColor");         // nullable
            action.CollectionParameter <Color>("colors"); // collection with non-nullable element

            // Action with complex and collection of complex parameters
            action = entityTypeConfigurationOfEmployee.Collection.Action("ComplexAction");
            action.Parameter <Address>("address").Nullable = false;
            action.Parameter <Address>("location");            // nullable
            action.CollectionParameter <Address>("addresses"); // collection with nullable element

            // Action with entity and collection of entity parameters
            action = entityTypeConfigurationOfEmployee.Collection.Action("EntityAction");
            action.EntityParameter <Employee>("person").Nullable = false;
            action.EntityParameter <Employee>("guard");           // nullable
            action.CollectionEntityParameter <Employee>("staff"); // collection with nullable element
            #endregion

            builder.Action("ResetDataSource");

            builder.EnumType <Color>().Namespace         = "NS";
            builder.ComplexType <Address>().Namespace    = "NS";
            builder.ComplexType <SubAddress>().Namespace = "NS";
            builder.EntityType <Employee>().Namespace    = "NS";
            builder.EntityType <Manager>().Namespace     = "NS";

            return(builder.GetEdmModel());
        }
示例#2
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 void CanBuildOperationBoundToCollectionCacheForIEdmModel()
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <Customer> customer = builder.EntitySet <Customer>("Customers").EntityType;

            customer.HasKey(c => c.ID);
            customer.Property(c => c.Name);
            customer.ComplexProperty(c => c.Address);

            EntityTypeConfiguration <Movie> movie = builder.EntitySet <Movie>("Movies").EntityType;

            movie.HasKey(m => m.ID);
            movie.HasKey(m => m.Name);
            EntityTypeConfiguration <Blockbuster> blockBuster = builder.EntityType <Blockbuster>().DerivesFrom <Movie>();
            EntityTypeConfiguration movieConfiguration        = builder.StructuralTypes.OfType <EntityTypeConfiguration>().Single(t => t.Name == "Movie");

            // build actions that are bindable to the collection of entity
            customer.Collection.Action("CollectionCustomerActionInCache1");
            customer.Collection.Action("CollectionCustomerActionInCache2");
            movie.Collection.Action("CollectionMovieActionInCache3");

            ActionConfiguration         movieActionIncache4     = builder.Action("CollectionMovieActionInCache4");
            CollectionTypeConfiguration collectionConfiguration = new CollectionTypeConfiguration(movieConfiguration, typeof(Movie));

            movieActionIncache4.SetBindingParameter("bindingParameter", collectionConfiguration);

            blockBuster.Collection.Action("CollectionBlockbusterActionInCache5");

            // build functions that are bindable to the collection of entity
            customer.Collection.Function("CollectionCustomerFunctionInCache1").Returns <int>();
            customer.Collection.Function("CollectionCustomerFunctionInCache2").Returns <int>();
            movie.Collection.Function("CollectionMovieFunctionInCache3").Returns <int>();
            blockBuster.Collection.Function("CollectionBlockbusterFunctionInCache5").Returns <int>();

            // build actions that are either: bindable to an entity, have no parameter, have only complex parameter
            customer.Action("CustomersActionNotInCache1");
            customer.Function("CustomersFunctionNotInCache1").Returns <int>();
            movie.Action("MoviesActionNotInCache2");
            builder.Action("NoParametersNotInCache3");

            ActionConfiguration addressParameterNotInCache4 = builder.Action("AddressParameterNotInCache4");

            addressParameterNotInCache4.Parameter <Address>("address");

            IEdmModel model = builder.GetEdmModel();

            IEdmEntityType customerType    = model.SchemaElements.OfType <IEdmEntityType>().Single(e => e.Name == "Customer");
            IEdmEntityType movieType       = model.SchemaElements.OfType <IEdmEntityType>().Single(e => e.Name == "Movie");
            IEdmEntityType blockBusterType = model.SchemaElements.OfType <IEdmEntityType>().Single(e => e.Name == "Blockbuster");

            // Act
            BindableOperationFinder annotation = new BindableOperationFinder(model);
            var movieOperations       = annotation.FindOperationsBoundToCollection(movieType).ToArray();
            var customerOperations    = annotation.FindOperationsBoundToCollection(customerType).ToArray();
            var blockBusterOperations = annotation.FindOperationsBoundToCollection(blockBusterType).ToArray();

            // Assert
            Assert.Equal(3, movieOperations.Length);
            Assert.Single(movieOperations.Where(a => a.Name == "CollectionMovieActionInCache3"));
            Assert.Single(movieOperations.Where(a => a.Name == "CollectionMovieActionInCache4"));
            Assert.Single(movieOperations.Where(a => a.Name == "CollectionMovieFunctionInCache3"));

            Assert.Equal(4, customerOperations.Length);
            Assert.Single(customerOperations.Where(a => a.Name == "CollectionCustomerActionInCache1"));
            Assert.Single(customerOperations.Where(a => a.Name == "CollectionCustomerActionInCache2"));
            Assert.Single(customerOperations.Where(a => a.Name == "CollectionCustomerFunctionInCache1"));
            Assert.Single(customerOperations.Where(a => a.Name == "CollectionCustomerFunctionInCache2"));

            Assert.Equal(5, blockBusterOperations.Length);
            Assert.Single(blockBusterOperations.Where(a => a.Name == "CollectionBlockbusterActionInCache5"));
            Assert.Single(blockBusterOperations.Where(a => a.Name == "CollectionBlockbusterFunctionInCache5"));
            Assert.Single(blockBusterOperations.Where(a => a.Name == "CollectionMovieActionInCache3"));
            Assert.Single(blockBusterOperations.Where(a => a.Name == "CollectionMovieActionInCache4"));
            Assert.Single(blockBusterOperations.Where(a => a.Name == "CollectionMovieFunctionInCache3"));
        }