Exemplo n.º 1
0
        private async Task TransformResultAsync <TData>(
            ResultExecutingContext context,
            ObjectResult result,
            IQueryable <TData> query)
        {
            var page    = (int)context.RouteData.Values[nameof(PaginatedAttribute) + ".page"];
            var perPage = (int)context.RouteData.Values[nameof(PaginatedAttribute) + ".perPage"];

            int rowCount = await query.CountAsync();

            if (rowCount == 0)
            {
                result.Value = Array.Empty <object>();
                return;
            }

            var pageCount = (int)Math.Ceiling((double)rowCount / perPage);

            if (page > pageCount)
            {
                context.Result = new NotFoundResult();
                return;
            }

            AddLinkHeader(context, page, perPage, pageCount);

            IServiceProvider requestServices = context.HttpContext.RequestServices;
            var        urlHelperFactory      = requestServices.GetRequiredService <IUrlHelperFactory>();
            IUrlHelper urlHelper             = urlHelperFactory.GetUrlHelper(context);
            var        serviceProvider       = new ExtendedServiceProvider(requestServices)
            {
                urlHelper,
                context.HttpContext
            };

            result.Value = query.Skip((page - 1) * perPage)
                           .Take(perPage)
                           .AsEnumerable()
                           .Select(o => ActivatorUtilities.CreateInstance(serviceProvider, ResultType, o));
        }
        public static IKeyInfoService CreateFactoryService()
        {
            var provider = new ExtendedServiceProvider();

            return(provider.GetService <IKeyInfoService>());
        }
        public static IKeyInfoService CreateFactoryService()
        {
            var provider = new ExtendedServiceProvider();

            return provider.GetService<IKeyInfoService>();
        }