Exemplo n.º 1
0
        /// TODO GitHubIssue#51 : Support model lazy loading
        /// <summary>
        /// Maps the API routes to the RestierController.
        /// </summary>
        /// <typeparam name="TApi">The user API.</typeparam>
        /// <param name="config">The <see cref="HttpConfiguration"/> instance.</param>
        /// <param name="routeName">The name of the route.</param>
        /// <param name="routePrefix">The prefix of the route.</param>
        /// <param name="apiFactory">The callback to create API instances.</param>
        /// <param name="batchHandler">The handler for batch requests.</param>
        /// <returns>The task object containing the resulted <see cref="ODataRoute"/> instance.</returns>
        public static Task <ODataRoute> MapRestierRoute <TApi>(
            this HttpConfiguration config,
            string routeName,
            string routePrefix,
            Func <ApiBase> apiFactory,
            RestierBatchHandler batchHandler = null)
            where TApi : ApiBase
        {
            Ensure.NotNull(apiFactory, "apiFactory");

            // This will be added a service to callback stored in ApiConfiguration
            // Callback is called by ApiBase.AddApiServices method to add real services.
            ApiConfiguration.AddPublisherServices <TApi>(services =>
            {
                services.AddODataServices <TApi>();
            });
            using (var api = apiFactory())
            {
                var model = GetModel(api);

                var conventions = CreateRestierRoutingConventions(config, model, apiFactory);

                if (batchHandler != null && batchHandler.ApiFactory == null)
                {
                    batchHandler.ApiFactory = apiFactory;
                }

                // Customized path handler should be added in ConfigureApi as service
                // Allow to handle URL encoded slash (%2F), and backslash(%5C) with customized handler
                var handler = api.Context.GetApiService <IODataPathHandler>();
                if (handler == null)
                {
                    handler = new DefaultODataPathHandler();
                }

                var route = config.MapODataServiceRoute(
                    routeName, routePrefix, model, handler, conventions, batchHandler);

                // Customized converter should be added in ConfigureApi as service
                var converter = api.Context.GetApiService <ODataPayloadValueConverter>();
                if (converter == null)
                {
                    converter = new RestierPayloadValueConverter();
                }

                model.SetPayloadValueConverter(converter);

                return(Task.FromResult(route));
            }
        }
Exemplo n.º 2
0
        public async Task VerifyMetadataPropertyType()
        {
            ApiConfiguration.AddPublisherServices <PrimitivesApi>(services =>
            {
                services.AddODataServices <PrimitivesApi>();
            });
            var api      = new PrimitivesApi();
            var edmModel = await api.GetModelAsync();

            var entityType = (IEdmEntityType)
                             edmModel.FindDeclaredType(@"Microsoft.Restier.Providers.EntityFramework7.Tests.Models.Primitives.DateItem");

            Assert.NotNull(entityType);

            Assert.True(entityType.FindProperty("DTProperty").Type.IsDateTimeOffset());
            Assert.True(entityType.FindProperty("DateProperty").Type.IsDate());
            Assert.True(entityType.FindProperty("TODProperty").Type.IsTimeOfDay());
            Assert.True(entityType.FindProperty("TSProperty").Type.IsDuration());
        }