Skip to content

Commit 7825fc1

Browse files
committed
Add CancellationToken to Repository methods
1 parent bba13a6 commit 7825fc1

File tree

3 files changed

+51
-25
lines changed

3 files changed

+51
-25
lines changed

src/KSFramework/GenericRepository/GenericRepository.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@ public GenericRepository(DbContext context) : base(context)
2323
/// Gets an entity by its primary key.
2424
/// </summary>
2525
/// <param name="id">The entity ID.</param>
26+
/// <param name="cancellationToken"></param>
2627
/// <returns>The entity if found; otherwise, null.</returns>
27-
public async ValueTask<TEntity?> GetByIdAsync(object id)
28+
public async ValueTask<TEntity?> GetByIdAsync(object id,
29+
CancellationToken cancellationToken = default)
2830
{
29-
return await DbSet.FindAsync(id);
31+
return await DbSet.FindAsync(id, cancellationToken);
3032
}
3133

3234
/// <summary>
3335
/// Gets all entities.
3436
/// </summary>
3537
/// <param name="asNoTracking">Whether to disable tracking for better performance.</param>
38+
/// <param name="cancellationToken"></param>
3639
/// <returns>A list of all entities.</returns>
37-
public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true)
40+
public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true,
41+
CancellationToken cancellationToken = default)
3842
{
39-
return await AsQueryable(asNoTracking).ToListAsync();
43+
return await AsQueryable(asNoTracking).ToListAsync(cancellationToken);
4044
}
4145

4246
/// <summary>
@@ -48,7 +52,11 @@ public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true)
4852
/// <param name="orderBy">Property name to order by.</param>
4953
/// <param name="desc">Order descending if true.</param>
5054
/// <returns>A paginated list of entities.</returns>
51-
public async Task<PaginatedList<TEntity>> GetPagedAsync(int pageIndex, int pageSize, Expression<Func<TEntity, bool>>? where = null, string? orderBy = "", bool desc = false)
55+
public async Task<PaginatedList<TEntity>> GetPagedAsync(int pageIndex,
56+
int pageSize,
57+
Expression<Func<TEntity, bool>>? where = null,
58+
string? orderBy = "",
59+
bool desc = false)
5260
{
5361
var query = ApplyWhere(AsQueryable(), where);
5462
return await PaginatedList<TEntity>.CreateAsync(query, pageIndex, pageSize, where, orderBy, desc);
@@ -84,28 +92,34 @@ public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate, bool
8492
/// Gets a single entity that matches the given predicate or null.
8593
/// </summary>
8694
/// <param name="predicate">The filter expression.</param>
95+
/// <param name="cancellationToken"></param>
8796
/// <returns>The matching entity or null.</returns>
88-
public async Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
97+
public async Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate,
98+
CancellationToken cancellationToken = default)
8999
{
90-
return await DbSet.SingleOrDefaultAsync(predicate);
100+
return await DbSet.SingleOrDefaultAsync(predicate, cancellationToken);
91101
}
92102

93103
/// <summary>
94104
/// Adds a new entity asynchronously.
95105
/// </summary>
96106
/// <param name="entity">The entity to add.</param>
97-
public async Task AddAsync(TEntity entity)
107+
/// <param name="cancellationToken"></param>
108+
public async Task AddAsync(TEntity entity,
109+
CancellationToken cancellationToken = default)
98110
{
99-
await DbSet.AddAsync(entity);
111+
await DbSet.AddAsync(entity, cancellationToken);
100112
}
101113

102114
/// <summary>
103115
/// Adds a range of entities asynchronously.
104116
/// </summary>
105117
/// <param name="entities">The entities to add.</param>
106-
public async Task AddRangeAsync(IEnumerable<TEntity> entities)
118+
/// <param name="cancellationToken"></param>
119+
public async Task AddRangeAsync(IEnumerable<TEntity> entities,
120+
CancellationToken cancellationToken = default)
107121
{
108-
await DbSet.AddRangeAsync(entities);
122+
await DbSet.AddRangeAsync(entities, cancellationToken);
109123
}
110124

111125
/// <summary>
@@ -139,9 +153,11 @@ public void RemoveRange(IEnumerable<TEntity> entities)
139153
/// Determines whether any entity exists that matches the specified predicate.
140154
/// </summary>
141155
/// <param name="predicate">The condition to check.</param>
156+
/// <param name="cancellationToken"></param>
142157
/// <returns>True if at least one entity exists; otherwise, false.</returns>
143-
public async Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate)
158+
public async Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate,
159+
CancellationToken cancellationToken = default)
144160
{
145-
return await DbSet.AnyAsync(predicate);
161+
return await DbSet.AnyAsync(predicate, cancellationToken);
146162
}
147163
}

src/KSFramework/GenericRepository/IGenericRepository.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ public interface IGenericRepository<TEntity> where TEntity : class
1414
/// </summary>
1515
/// <param name="id">The unique identifier of the entity.</param>
1616
/// <returns>A task representing the asynchronous operation, containing the entity if found; otherwise, null.</returns>
17-
ValueTask<TEntity?> GetByIdAsync(object id);
17+
ValueTask<TEntity?> GetByIdAsync(object id, CancellationToken cancellationToken = default);
1818

1919
/// <summary>
2020
/// Asynchronously retrieves all entities.
2121
/// </summary>
2222
/// <param name="asNoTracking">Whether to track entities in change tracker.</param>
2323
/// <returns>A task containing all entities.</returns>
24-
Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true);
24+
Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true, CancellationToken cancellationToken = default);
2525

2626
/// <summary>
2727
/// Asynchronously retrieves a paginated list of entities with optional filtering and ordering.
@@ -68,19 +68,21 @@ PaginatedList<TEntity> GetPaged(
6868
/// </summary>
6969
/// <param name="predicate">The condition to match.</param>
7070
/// <returns>A task containing a single entity or null.</returns>
71-
Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
71+
Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
7272

7373
/// <summary>
7474
/// Asynchronously adds an entity to the repository.
7575
/// </summary>
7676
/// <param name="entity">The entity to add.</param>
77-
Task AddAsync(TEntity entity);
77+
/// <param name="cancellationToken"></param>
78+
Task AddAsync(TEntity entity, CancellationToken cancellationToken = default);
7879

7980
/// <summary>
8081
/// Asynchronously adds multiple entities to the repository.
8182
/// </summary>
8283
/// <param name="entities">The entities to add.</param>
83-
Task AddRangeAsync(IEnumerable<TEntity> entities);
84+
/// <param name="cancellationToken"></param>
85+
Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
8486

8587
/// <summary>
8688
/// Updates an existing entity in the repository.
@@ -104,6 +106,8 @@ PaginatedList<TEntity> GetPaged(
104106
/// Asynchronously checks whether any entity matches the given predicate.
105107
/// </summary>
106108
/// <param name="predicate">The condition to match.</param>
109+
/// <param name="cancellationToken"></param>
107110
/// <returns>True if any entity matches; otherwise, false.</returns>
108-
Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate);
111+
Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate,
112+
CancellationToken cancellationToken = default);
109113
}

src/KSFramework/GenericRepository/IRepository.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ namespace KSFramework.GenericRepository;
99
/// <typeparam name="TEntity">The entity type.</typeparam>
1010
public interface IRepository<TEntity> where TEntity : class
1111
{
12-
Task AddAsync(TEntity entity);
13-
Task AddRangeAsync(IEnumerable<TEntity> entities);
12+
Task AddAsync(TEntity entity,
13+
CancellationToken cancellationToken = default);
14+
Task AddRangeAsync(IEnumerable<TEntity> entities,
15+
CancellationToken cancellationToken = default);
1416
IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate, bool asNoTracking = true);
15-
Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true);
17+
Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true,
18+
CancellationToken cancellationToken = default);
1619
Task<PaginatedList<TEntity>> GetPagedAsync(
1720
int pageIndex,
1821
int pageSize,
@@ -25,10 +28,13 @@ PaginatedList<TEntity> GetPaged(
2528
Expression<Func<TEntity, bool>>? where = null,
2629
string? orderBy = "",
2730
bool desc = false);
28-
ValueTask<TEntity?> GetByIdAsync(object id);
31+
ValueTask<TEntity?> GetByIdAsync(object id,
32+
CancellationToken cancellationToken = default);
2933
void Remove(TEntity entity);
3034
void RemoveRange(IEnumerable<TEntity> entities);
31-
Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
32-
Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate);
35+
Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate,
36+
CancellationToken cancellationToken = default);
37+
Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate,
38+
CancellationToken cancellationToken = default);
3339
void Update(TEntity entity);
3440
}

0 commit comments

Comments
 (0)