@@ -23,20 +23,24 @@ public GenericRepository(DbContext context) : base(context)
23
23
/// Gets an entity by its primary key.
24
24
/// </summary>
25
25
/// <param name="id">The entity ID.</param>
26
+ /// <param name="cancellationToken"></param>
26
27
/// <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 )
28
30
{
29
- return await DbSet . FindAsync ( id ) ;
31
+ return await DbSet . FindAsync ( id , cancellationToken ) ;
30
32
}
31
33
32
34
/// <summary>
33
35
/// Gets all entities.
34
36
/// </summary>
35
37
/// <param name="asNoTracking">Whether to disable tracking for better performance.</param>
38
+ /// <param name="cancellationToken"></param>
36
39
/// <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 )
38
42
{
39
- return await AsQueryable ( asNoTracking ) . ToListAsync ( ) ;
43
+ return await AsQueryable ( asNoTracking ) . ToListAsync ( cancellationToken ) ;
40
44
}
41
45
42
46
/// <summary>
@@ -48,7 +52,11 @@ public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true)
48
52
/// <param name="orderBy">Property name to order by.</param>
49
53
/// <param name="desc">Order descending if true.</param>
50
54
/// <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 )
52
60
{
53
61
var query = ApplyWhere ( AsQueryable ( ) , where ) ;
54
62
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
84
92
/// Gets a single entity that matches the given predicate or null.
85
93
/// </summary>
86
94
/// <param name="predicate">The filter expression.</param>
95
+ /// <param name="cancellationToken"></param>
87
96
/// <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 )
89
99
{
90
- return await DbSet . SingleOrDefaultAsync ( predicate ) ;
100
+ return await DbSet . SingleOrDefaultAsync ( predicate , cancellationToken ) ;
91
101
}
92
102
93
103
/// <summary>
94
104
/// Adds a new entity asynchronously.
95
105
/// </summary>
96
106
/// <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 )
98
110
{
99
- await DbSet . AddAsync ( entity ) ;
111
+ await DbSet . AddAsync ( entity , cancellationToken ) ;
100
112
}
101
113
102
114
/// <summary>
103
115
/// Adds a range of entities asynchronously.
104
116
/// </summary>
105
117
/// <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 )
107
121
{
108
- await DbSet . AddRangeAsync ( entities ) ;
122
+ await DbSet . AddRangeAsync ( entities , cancellationToken ) ;
109
123
}
110
124
111
125
/// <summary>
@@ -139,9 +153,11 @@ public void RemoveRange(IEnumerable<TEntity> entities)
139
153
/// Determines whether any entity exists that matches the specified predicate.
140
154
/// </summary>
141
155
/// <param name="predicate">The condition to check.</param>
156
+ /// <param name="cancellationToken"></param>
142
157
/// <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 )
144
160
{
145
- return await DbSet . AnyAsync ( predicate ) ;
161
+ return await DbSet . AnyAsync ( predicate , cancellationToken ) ;
146
162
}
147
163
}
0 commit comments