diff --git a/CHANGELOG.md b/CHANGELOG.md index c60c246..c706706 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + +### Changed + +- Require HttpContext in default `PaginationService` only where needed instead of throwing on ctor ([#11](https://github.com/mrahhal/MR.AspNetCore.Pagination/pull/11)) + +[**Full diff**](https://github.com/mrahhal/MR.EntityFrameworkCore.KeysetPagination/compare/v2.2.0...HEAD) + ## 2.2.0 - 2023-10-23 ### Other diff --git a/src/MR.AspNetCore.Pagination/PaginationService.cs b/src/MR.AspNetCore.Pagination/PaginationService.cs index 22b53bd..c86ed7e 100644 --- a/src/MR.AspNetCore.Pagination/PaginationService.cs +++ b/src/MR.AspNetCore.Pagination/PaginationService.cs @@ -302,18 +302,18 @@ public static OffsetPaginationResult OffsetPaginate( /// public class PaginationService : IPaginationService { - private readonly HttpContext _httpContext; private readonly PaginationOptions _options; + private readonly HttpContext? _httpContext; /// /// Creates a new instance of . /// public PaginationService( - IHttpContextAccessor httpContextAccessor, - IOptions options) + IOptions options, + IHttpContextAccessor? httpContextAccessor = null) { - _httpContext = httpContextAccessor?.HttpContext ?? throw new InvalidOperationException("HttpContext is required."); _options = options.Value; + _httpContext = httpContextAccessor?.HttpContext; } /// @@ -429,7 +429,7 @@ public Task> KeysetPaginateAsync( where TOut : class { var queryModel = ParseKeysetQueryModel( - _httpContext.Request.Query, + GetHttpContextRequestQueryOrThrow(), pageSize); return KeysetPaginateAsync(source, keysetQueryDefinition, getReferenceAsync, map, queryModel); } @@ -445,7 +445,7 @@ public Task> KeysetPaginateAsync( where TOut : class { var queryModel = ParseKeysetQueryModel( - _httpContext.Request.Query, + GetHttpContextRequestQueryOrThrow(), pageSize); return KeysetPaginateAsync(source, builderAction, getReferenceAsync, map, queryModel); } @@ -487,7 +487,7 @@ public Task> OffsetPaginateAsync( where TOut : class { var queryModel = ParseOffsetQueryModel( - _httpContext.Request.Query, + GetHttpContextRequestQueryOrThrow(), pageSize); return OffsetPaginateAsync(source, map, queryModel); } @@ -529,7 +529,7 @@ public OffsetPaginationResult OffsetPaginate( where TOut : class { var queryModel = ParseOffsetQueryModel( - _httpContext.Request.Query, + GetHttpContextRequestQueryOrThrow(), pageSize); return OffsetPaginate(source, map, queryModel); } @@ -660,4 +660,10 @@ private OffsetQueryModel ParseOffsetQueryModel( return model; } + + private IQueryCollection GetHttpContextRequestQueryOrThrow() + { + var context = _httpContext ?? throw new InvalidOperationException("HttpContext is required to parse the pagination model from the request query."); + return context.Request.Query; + } } diff --git a/test/MR.AspNetCore.Pagination.Tests/PaginationServiceTest.cs b/test/MR.AspNetCore.Pagination.Tests/PaginationServiceTest.cs index ed5f35e..e1bf7c4 100644 --- a/test/MR.AspNetCore.Pagination.Tests/PaginationServiceTest.cs +++ b/test/MR.AspNetCore.Pagination.Tests/PaginationServiceTest.cs @@ -311,3 +311,17 @@ public async Task KeysetPaginateAsync() } } } + +public class PaginationServiceQuickTest +{ + [Fact] + public void DoesNotThrowInCtorForNoHttpContext() + { + var services = new ServiceCollection(); + services.AddTransient(); + services.Configure(o => { }); + var provider = services.BuildServiceProvider(); + + var _ = provider.GetRequiredService(); + } +}