コード例 #1
0
        public void Apply(OperationConfiguration configuration, ODataModelBuilder model)
        {
            FunctionConfiguration function = configuration as FunctionConfiguration;

            if (function == null || !function.IsBindable)
            {
                return;
            }

            // You only need to create links for bindable functions that bind to a single entity.
            if (function.BindingParameter.TypeConfiguration.Kind == EdmTypeKind.Entity && function.GetFunctionLink() == null)
            {
                string bindingParamterType = function.BindingParameter.TypeConfiguration.FullName;

                function.HasFunctionLink(entityContext =>
                                         entityContext.GenerateFunctionLink(bindingParamterType, function.FullyQualifiedName, function.Parameters.Select(p => p.Name)),
                                         followsConventions: true);
            }
            else if (function.BindingParameter.TypeConfiguration.Kind == EdmTypeKind.Collection && function.GetFeedFunctionLink() == null)
            {
                if (((CollectionTypeConfiguration)function.BindingParameter.TypeConfiguration).ElementType.Kind ==
                    EdmTypeKind.Entity)
                {
                    string bindingParamterType = function.BindingParameter.TypeConfiguration.FullName;
                    function.HasFeedFunctionLink(
                        feedContext =>
                        feedContext.GenerateFunctionLink(bindingParamterType, function.FullyQualifiedName, function.Parameters.Select(p => p.Name)),
                        followsConventions: true);
                }
            }
        }
コード例 #2
0
        public void CanManuallyConfigureFunctionLinkFactory()
        {
            // Arrange
            string            uriTemplate = "http://server/service/Customers({0})/Reward";
            Uri               expectedUri = new Uri(string.Format(uriTemplate, 1));
            ODataModelBuilder builder     = new ODataModelBuilder();
            EntityTypeConfiguration <Customer> customer = builder.EntitySet <Customer>("Customers").EntityType;

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

            // Act
            FunctionConfiguration reward = customer.Function("Reward");

            reward.HasFunctionLink(ctx => new Uri(string.Format(uriTemplate, ctx.GetPropertyValue("CustomerId"))),
                                   followsConventions: false);
            reward.Returns <bool>();
            IEdmModel              model             = builder.GetEdmModel();
            IEdmEntityType         customerType      = model.SchemaElements.OfType <IEdmEntityType>().SingleOrDefault();
            ODataSerializerContext serializerContext = new ODataSerializerContext {
                Model = model
            };

            ResourceContext context = new ResourceContext(serializerContext, customerType.AsReference(), new Customer {
                CustomerId = 1
            });
            IEdmFunction         rewardFunction      = Assert.Single(model.SchemaElements.OfType <IEdmFunction>()); // Guard
            OperationLinkBuilder functionLinkBuilder = model.GetAnnotationValue <OperationLinkBuilder>(rewardFunction);

            //Assert
            Assert.Equal(expectedUri, reward.GetFunctionLink()(context));
            Assert.NotNull(functionLinkBuilder);
            Assert.Equal(expectedUri, functionLinkBuilder.BuildLink(context));
        }
コード例 #3
0
        public void FunctionLink_PreservesFollowsConventions(bool value)
        {
            // Arrange
            ODataModelBuilder     builder       = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();
            FunctionConfiguration configuration = new FunctionConfiguration(builder, "IgnoreFunction");

            configuration.Returns <int>();
            Mock <IEdmTypeConfiguration> bindingParameterTypeMock = new Mock <IEdmTypeConfiguration>();

            bindingParameterTypeMock.Setup(o => o.Kind).Returns(EdmTypeKind.Entity);
            Type entityType = typeof(object);

            bindingParameterTypeMock.Setup(o => o.ClrType).Returns(entityType);
            configuration.SetBindingParameter("IgnoreParameter", bindingParameterTypeMock.Object);
            configuration.HasFunctionLink((a) => { throw new NotImplementedException(); }, followsConventions: value);
            builder.AddOperation(configuration);
            builder.AddEntityType(entityType);

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            var function = Assert.Single(model.SchemaElements.OfType <IEdmFunction>());
            OperationLinkBuilder functionLinkBuilder = model.GetOperationLinkBuilder(function);

            Assert.NotNull(functionLinkBuilder);
            Assert.Equal(value, functionLinkBuilder.FollowsConventions);
        }
コード例 #4
0
        public void HasFunctionLink_ThrowsException_OnNonBindableActions()
        {
            // Arrange
            ODataModelBuilder     builder  = new ODataModelBuilder();
            FunctionConfiguration function = builder.Function("NoBindableFunction");

            // Act & Assert
            ExceptionAssert.Throws <InvalidOperationException>(
                () => function.HasFunctionLink(ctx => new Uri("http://any"), followsConventions: false),
                "To register a function link factory, functions must be bindable to a single entity. " +
                "Function 'NoBindableFunction' does not meet this requirement.");
        }
コード例 #5
0
        public void Apply(ProcedureConfiguration configuration, ODataModelBuilder model)
        {
            FunctionConfiguration function = configuration as FunctionConfiguration;

            // You only need to create links for bindable functions that bind to a single entity.
            if (function != null && function.IsBindable && function.BindingParameter.TypeConfiguration.Kind == EdmTypeKind.Entity && function.GetFunctionLink() == null)
            {
                string bindingParamterType = function.BindingParameter.TypeConfiguration.FullName;

                function.HasFunctionLink(entityContext =>
                                         entityContext.GenerateFunctionLink(bindingParamterType, function.Name, function.Parameters.Select(p => p.Name)),
                                         followsConventions: true);
            }
        }
コード例 #6
0
        public void HasFunctionLink_SetsFollowsConventions(bool value)
        {
            // Arrange
            ODataModelBuilder            builder  = new ODataModelBuilder();
            FunctionConfiguration        function = new FunctionConfiguration(builder, "IgnoreFunction");
            Mock <IEdmTypeConfiguration> bindingParameterTypeMock = new Mock <IEdmTypeConfiguration>();

            bindingParameterTypeMock.Setup(o => o.Kind).Returns(EdmTypeKind.Entity);
            bindingParameterTypeMock.Setup(o => o.ClrType).Returns(typeof(int));
            IEdmTypeConfiguration bindingParameterType = bindingParameterTypeMock.Object;

            function.SetBindingParameter("IgnoreParameter", bindingParameterType);

            // Act
            function.HasFunctionLink((a) => { throw new NotImplementedException(); }, followsConventions: value);

            // Assert
            Assert.Equal(value, function.FollowsConventions);
        }
コード例 #7
0
        public void HasFunctionLink_SetsFollowsConventions(bool value)
        {
            // Arrange
            ODataModelBuilder builder = new ODataModelBuilder();
            FunctionConfiguration function = new FunctionConfiguration(builder, "IgnoreFunction");
            Mock<IEdmTypeConfiguration> bindingParameterTypeMock = new Mock<IEdmTypeConfiguration>();
            bindingParameterTypeMock.Setup(o => o.Kind).Returns(EdmTypeKind.Entity);
            IEdmTypeConfiguration bindingParameterType = bindingParameterTypeMock.Object;
            function.SetBindingParameter("IgnoreParameter", bindingParameterType, alwaysBindable: false);

            // Act
            function.HasFunctionLink((a) => { throw new NotImplementedException(); }, followsConventions: value);

            // Assert
            Assert.Equal(value, function.FollowsConventions);
        }
コード例 #8
0
        public void FunctionLink_PreservesFollowsConventions(bool value)
        {
            // Arrange
            ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock<ODataModelBuilder>();
            FunctionConfiguration configuration = new FunctionConfiguration(builder, "IgnoreFunction");
            configuration.Returns<int>();
            Mock<IEdmTypeConfiguration> bindingParameterTypeMock = new Mock<IEdmTypeConfiguration>();
            bindingParameterTypeMock.Setup(o => o.Kind).Returns(EdmTypeKind.Entity);
            Type entityType = typeof(object);
            bindingParameterTypeMock.Setup(o => o.ClrType).Returns(entityType);
            configuration.SetBindingParameter("IgnoreParameter", bindingParameterTypeMock.Object);
            configuration.HasFunctionLink((a) => { throw new NotImplementedException(); }, followsConventions: value);
            builder.AddProcedure(configuration);
            builder.AddEntityType(entityType);

            // Act
            IEdmModel model = builder.GetEdmModel();

            // Assert
            var function = Assert.Single(model.SchemaElements.OfType<IEdmFunction>());
            FunctionLinkBuilder functionLinkBuilder = model.GetFunctionLinkBuilder(function);
            Assert.NotNull(functionLinkBuilder);
            Assert.Equal(value, functionLinkBuilder.FollowsConventions);
        }