@@ -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>
@@ -47,11 +51,17 @@ public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true)
4751 /// <param name="where">Optional filter expression.</param>
4852 /// <param name="orderBy">Property name to order by.</param>
4953 /// <param name="desc">Order descending if true.</param>
54+ /// <param name="cancellationToken"></param>
5055 /// <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 )
56+ public async Task < PaginatedList < TEntity > > GetPagedAsync ( int pageIndex ,
57+ int pageSize ,
58+ Expression < Func < TEntity , bool > > ? where = null ,
59+ string ? orderBy = "" ,
60+ bool desc = false ,
61+ CancellationToken cancellationToken = default )
5262 {
5363 var query = ApplyWhere ( AsQueryable ( ) , where ) ;
54- return await PaginatedList < TEntity > . CreateAsync ( query , pageIndex , pageSize , where , orderBy , desc ) ;
64+ return await PaginatedList < TEntity > . CreateAsync ( query , pageIndex , pageSize , where , orderBy , desc , cancellationToken ) ;
5565 }
5666
5767 /// <summary>
@@ -84,28 +94,34 @@ public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate, bool
8494 /// Gets a single entity that matches the given predicate or null.
8595 /// </summary>
8696 /// <param name="predicate">The filter expression.</param>
97+ /// <param name="cancellationToken"></param>
8798 /// <returns>The matching entity or null.</returns>
88- public async Task < TEntity ? > SingleOrDefaultAsync ( Expression < Func < TEntity , bool > > predicate )
99+ public async Task < TEntity ? > SingleOrDefaultAsync ( Expression < Func < TEntity , bool > > predicate ,
100+ CancellationToken cancellationToken = default )
89101 {
90- return await DbSet . SingleOrDefaultAsync ( predicate ) ;
102+ return await DbSet . SingleOrDefaultAsync ( predicate , cancellationToken ) ;
91103 }
92104
93105 /// <summary>
94106 /// Adds a new entity asynchronously.
95107 /// </summary>
96108 /// <param name="entity">The entity to add.</param>
97- public async Task AddAsync ( TEntity entity )
109+ /// <param name="cancellationToken"></param>
110+ public async Task AddAsync ( TEntity entity ,
111+ CancellationToken cancellationToken = default )
98112 {
99- await DbSet . AddAsync ( entity ) ;
113+ await DbSet . AddAsync ( entity , cancellationToken ) ;
100114 }
101115
102116 /// <summary>
103117 /// Adds a range of entities asynchronously.
104118 /// </summary>
105119 /// <param name="entities">The entities to add.</param>
106- public async Task AddRangeAsync ( IEnumerable < TEntity > entities )
120+ /// <param name="cancellationToken"></param>
121+ public async Task AddRangeAsync ( IEnumerable < TEntity > entities ,
122+ CancellationToken cancellationToken = default )
107123 {
108- await DbSet . AddRangeAsync ( entities ) ;
124+ await DbSet . AddRangeAsync ( entities , cancellationToken ) ;
109125 }
110126
111127 /// <summary>
@@ -139,9 +155,11 @@ public void RemoveRange(IEnumerable<TEntity> entities)
139155 /// Determines whether any entity exists that matches the specified predicate.
140156 /// </summary>
141157 /// <param name="predicate">The condition to check.</param>
158+ /// <param name="cancellationToken"></param>
142159 /// <returns>True if at least one entity exists; otherwise, false.</returns>
143- public async Task < bool > IsExistValueForPropertyAsync ( Expression < Func < TEntity , bool > > predicate )
160+ public async Task < bool > IsExistValueForPropertyAsync ( Expression < Func < TEntity , bool > > predicate ,
161+ CancellationToken cancellationToken = default )
144162 {
145- return await DbSet . AnyAsync ( predicate ) ;
163+ return await DbSet . AnyAsync ( predicate , cancellationToken ) ;
146164 }
147165}
0 commit comments