@@ -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>
@@ -47,11 +51,17 @@ public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true)
47
51
/// <param name="where">Optional filter expression.</param>
48
52
/// <param name="orderBy">Property name to order by.</param>
49
53
/// <param name="desc">Order descending if true.</param>
54
+ /// <param name="cancellationToken"></param>
50
55
/// <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 )
52
62
{
53
63
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 ) ;
55
65
}
56
66
57
67
/// <summary>
@@ -84,28 +94,34 @@ public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate, bool
84
94
/// Gets a single entity that matches the given predicate or null.
85
95
/// </summary>
86
96
/// <param name="predicate">The filter expression.</param>
97
+ /// <param name="cancellationToken"></param>
87
98
/// <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 )
89
101
{
90
- return await DbSet . SingleOrDefaultAsync ( predicate ) ;
102
+ return await DbSet . SingleOrDefaultAsync ( predicate , cancellationToken ) ;
91
103
}
92
104
93
105
/// <summary>
94
106
/// Adds a new entity asynchronously.
95
107
/// </summary>
96
108
/// <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 )
98
112
{
99
- await DbSet . AddAsync ( entity ) ;
113
+ await DbSet . AddAsync ( entity , cancellationToken ) ;
100
114
}
101
115
102
116
/// <summary>
103
117
/// Adds a range of entities asynchronously.
104
118
/// </summary>
105
119
/// <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 )
107
123
{
108
- await DbSet . AddRangeAsync ( entities ) ;
124
+ await DbSet . AddRangeAsync ( entities , cancellationToken ) ;
109
125
}
110
126
111
127
/// <summary>
@@ -139,9 +155,11 @@ public void RemoveRange(IEnumerable<TEntity> entities)
139
155
/// Determines whether any entity exists that matches the specified predicate.
140
156
/// </summary>
141
157
/// <param name="predicate">The condition to check.</param>
158
+ /// <param name="cancellationToken"></param>
142
159
/// <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 )
144
162
{
145
- return await DbSet . AnyAsync ( predicate ) ;
163
+ return await DbSet . AnyAsync ( predicate , cancellationToken ) ;
146
164
}
147
165
}
0 commit comments