Skip to content

Commit 1e212d6

Browse files
committed
Add CancellationToken to GenericRepository GetPaged methods
1 parent 7825fc1 commit 1e212d6

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/KSFramework/GenericRepository/GenericRepository.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@ public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true,
5151
/// <param name="where">Optional filter expression.</param>
5252
/// <param name="orderBy">Property name to order by.</param>
5353
/// <param name="desc">Order descending if true.</param>
54+
/// <param name="cancellationToken"></param>
5455
/// <returns>A paginated list of entities.</returns>
5556
public async Task<PaginatedList<TEntity>> GetPagedAsync(int pageIndex,
5657
int pageSize,
5758
Expression<Func<TEntity, bool>>? where = null,
5859
string? orderBy = "",
59-
bool desc = false)
60+
bool desc = false,
61+
CancellationToken cancellationToken = default)
6062
{
6163
var query = ApplyWhere(AsQueryable(), where);
62-
return await PaginatedList<TEntity>.CreateAsync(query, pageIndex, pageSize, where, orderBy, desc);
64+
return await PaginatedList<TEntity>.CreateAsync(query, pageIndex, pageSize, where, orderBy, desc, cancellationToken);
6365
}
6466

6567
/// <summary>

src/KSFramework/GenericRepository/IGenericRepository.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ public interface IGenericRepository<TEntity> where TEntity : class
1313
/// Asynchronously retrieves an entity by its unique identifier.
1414
/// </summary>
1515
/// <param name="id">The unique identifier of the entity.</param>
16+
/// <param name="cancellationToken"></param>
1617
/// <returns>A task representing the asynchronous operation, containing the entity if found; otherwise, null.</returns>
1718
ValueTask<TEntity?> GetByIdAsync(object id, CancellationToken cancellationToken = default);
1819

1920
/// <summary>
2021
/// Asynchronously retrieves all entities.
2122
/// </summary>
2223
/// <param name="asNoTracking">Whether to track entities in change tracker.</param>
24+
/// <param name="cancellationToken"></param>
2325
/// <returns>A task containing all entities.</returns>
2426
Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true, CancellationToken cancellationToken = default);
2527

@@ -31,13 +33,15 @@ public interface IGenericRepository<TEntity> where TEntity : class
3133
/// <param name="where">Optional filter expression.</param>
3234
/// <param name="orderBy">Optional property name to order by.</param>
3335
/// <param name="desc">Indicates if the order should be descending.</param>
36+
/// <param name="cancellationToken"></param>
3437
/// <returns>A task containing a paginated list of entities.</returns>
3538
Task<PaginatedList<TEntity>> GetPagedAsync(
3639
int pageIndex,
3740
int pageSize,
3841
Expression<Func<TEntity, bool>>? where = null,
3942
string? orderBy = "",
40-
bool desc = false);
43+
bool desc = false,
44+
CancellationToken cancellationToken = default);
4145

4246
/// <summary>
4347
/// Retrieves a paginated list of entities with optional filtering and ordering.

src/KSFramework/Pagination/PaginatedList.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ public bool HasNextPage
3737
}
3838
}
3939

40-
public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize, Expression<Func<T, bool>>? where = null,
41-
string? orderBy = "", bool desc = false)
40+
public static async Task<PaginatedList<T>> CreateAsync(
41+
IQueryable<T> source,
42+
int pageIndex,
43+
int pageSize,
44+
Expression<Func<T, bool>>? where = null,
45+
string? orderBy = "",
46+
bool desc = false,
47+
CancellationToken cancellationToken = default)
4248
{
4349
if(where is null) where = x => true;
4450

@@ -54,8 +60,8 @@ public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int
5460
}
5561
}
5662

57-
var count = await source.CountAsync(where);
58-
var items = await source.Where(where).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
63+
var count = await source.CountAsync(where, cancellationToken);
64+
var items = await source.Where(where).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(cancellationToken);
5965
return new PaginatedList<T>(items, count, pageIndex, pageSize);
6066
}
6167

0 commit comments

Comments
 (0)