From ba520310680859b6ab4b874c6c87605041953afa Mon Sep 17 00:00:00 2001 From: Mohammad Rahhal Date: Fri, 26 Jan 2024 23:52:34 +0100 Subject: [PATCH 1/6] Require HttpContext only where needed instead of throwing on ctor --- .../PaginationService.cs | 22 ++++++++++++------- .../PaginationServiceTest.cs | 14 ++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/MR.AspNetCore.Pagination/PaginationService.cs b/src/MR.AspNetCore.Pagination/PaginationService.cs index 22b53bd..43f9adc 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."); + 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(); + } +} From 93626d217c522cd8b404cfb4672cfc4011f4fe14 Mon Sep 17 00:00:00 2001 From: Mohammad Rahhal Date: Fri, 26 Jan 2024 23:56:53 +0100 Subject: [PATCH 2/6] Update CHANGELOG.md --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbbb02f..bea6fdc 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 + +### Improved + +- Require HttpContext 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.1.0...HEAD) + ## 2.1.0 - 2023-05-13 ### Added From 9e0530d17945c500d6d423902e799ae49f3f14f1 Mon Sep 17 00:00:00 2001 From: Mohammad Rahhal Date: Fri, 26 Jan 2024 23:59:06 +0100 Subject: [PATCH 3/6] Improve exception message --- src/MR.AspNetCore.Pagination/PaginationService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MR.AspNetCore.Pagination/PaginationService.cs b/src/MR.AspNetCore.Pagination/PaginationService.cs index 43f9adc..c86ed7e 100644 --- a/src/MR.AspNetCore.Pagination/PaginationService.cs +++ b/src/MR.AspNetCore.Pagination/PaginationService.cs @@ -663,7 +663,7 @@ private OffsetQueryModel ParseOffsetQueryModel( private IQueryCollection GetHttpContextRequestQueryOrThrow() { - var context = _httpContext ?? throw new InvalidOperationException("HttpContext is required."); + var context = _httpContext ?? throw new InvalidOperationException("HttpContext is required to parse the pagination model from the request query."); return context.Request.Query; } } From 57e24343d6c24d998c3284814e514348a2f90a51 Mon Sep 17 00:00:00 2001 From: Mohammad Rahhal Date: Sat, 27 Jan 2024 00:03:14 +0100 Subject: [PATCH 4/6] Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bea6fdc..f25e37e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased -### Improved +### Changed -- Require HttpContext only where needed instead of throwing on ctor ([#11](https://github.com/mrahhal/MR.AspNetCore.Pagination/pull/11)) +- 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.1.0...HEAD) From 64fd6c604363cb3e9d40584c1593344c3e88c7e9 Mon Sep 17 00:00:00 2001 From: Mohammad Rahhal Date: Sat, 27 Jan 2024 00:07:29 +0100 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f25e37e..dbbb02f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,14 +5,6 @@ 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.1.0...HEAD) - ## 2.1.0 - 2023-05-13 ### Added From 6adf364667870ae7db7c4357fa5b69cf2cdd4a56 Mon Sep 17 00:00:00 2001 From: Mohammad Rahhal Date: Sat, 27 Jan 2024 00:08:48 +0100 Subject: [PATCH 6/6] Update CHANGELOG.md --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) 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