Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require HttpContext only where needed instead of throwing on ctor #11

Merged
merged 7 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions src/MR.AspNetCore.Pagination/PaginationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,18 @@ public static OffsetPaginationResult<T> OffsetPaginate<T>(
/// </summary>
public class PaginationService : IPaginationService
{
private readonly HttpContext _httpContext;
private readonly PaginationOptions _options;
private readonly HttpContext? _httpContext;

/// <summary>
/// Creates a new instance of <see cref="PaginationService"/>.
/// </summary>
public PaginationService(
IHttpContextAccessor httpContextAccessor,
IOptions<PaginationOptions> options)
IOptions<PaginationOptions> options,
IHttpContextAccessor? httpContextAccessor = null)
{
_httpContext = httpContextAccessor?.HttpContext ?? throw new InvalidOperationException("HttpContext is required.");
_options = options.Value;
_httpContext = httpContextAccessor?.HttpContext;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -429,7 +429,7 @@ public Task<KeysetPaginationResult<TOut>> KeysetPaginateAsync<T, TOut>(
where TOut : class
{
var queryModel = ParseKeysetQueryModel(
_httpContext.Request.Query,
GetHttpContextRequestQueryOrThrow(),
pageSize);
return KeysetPaginateAsync(source, keysetQueryDefinition, getReferenceAsync, map, queryModel);
}
Expand All @@ -445,7 +445,7 @@ public Task<KeysetPaginationResult<TOut>> KeysetPaginateAsync<T, TOut>(
where TOut : class
{
var queryModel = ParseKeysetQueryModel(
_httpContext.Request.Query,
GetHttpContextRequestQueryOrThrow(),
pageSize);
return KeysetPaginateAsync(source, builderAction, getReferenceAsync, map, queryModel);
}
Expand Down Expand Up @@ -487,7 +487,7 @@ public Task<OffsetPaginationResult<TOut>> OffsetPaginateAsync<T, TOut>(
where TOut : class
{
var queryModel = ParseOffsetQueryModel(
_httpContext.Request.Query,
GetHttpContextRequestQueryOrThrow(),
pageSize);
return OffsetPaginateAsync(source, map, queryModel);
}
Expand Down Expand Up @@ -529,7 +529,7 @@ public OffsetPaginationResult<TOut> OffsetPaginate<T, TOut>(
where TOut : class
{
var queryModel = ParseOffsetQueryModel(
_httpContext.Request.Query,
GetHttpContextRequestQueryOrThrow(),
pageSize);
return OffsetPaginate(source, map, queryModel);
}
Expand Down Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions test/MR.AspNetCore.Pagination.Tests/PaginationServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,17 @@ public async Task KeysetPaginateAsync()
}
}
}

public class PaginationServiceQuickTest
{
[Fact]
public void DoesNotThrowInCtorForNoHttpContext()
{
var services = new ServiceCollection();
services.AddTransient<PaginationService>();
services.Configure<PaginationOptions>(o => { });
var provider = services.BuildServiceProvider();

var _ = provider.GetRequiredService<PaginationService>();
}
}
Loading