1
- using ClassLibrary . EFCore . Extensions ;
2
-
3
- namespace ClassLibrary . EFCore ;
1
+ namespace ClassLibrary . EFCore ;
4
2
5
3
public class Repository < TEntity , TKey > ( DbContext dbContext ) : IRepository < TEntity , TKey > where TEntity : class , IEntity < TKey > , new ( )
6
4
{
@@ -10,19 +8,38 @@ namespace ClassLibrary.EFCore;
10
8
public DbContext DbContext { get ; } = dbContext ?? throw new ArgumentNullException ( nameof ( dbContext ) ) ;
11
9
12
10
/// <summary>
13
- /// Retrieves all entities asynchronously.
11
+ /// Retrieves all entities asynchronously with optional filtering, ordering, and including related entities .
14
12
/// </summary>
15
- /// <returns>A task that represents the asynchronous operation. The task result contains a list of all entities.</returns>
16
- public async Task < List < TEntity > > GetAllAsync ( )
17
- => await DbContext . Set < TEntity > ( ) . AsNoTracking ( ) . ToListAsync ( ) ;
13
+ /// <param name="includes">A function to include related entities.</param>
14
+ /// <param name="filter">A filter expression to apply.</param>
15
+ /// <param name="orderBy">An expression to order the results.</param>
16
+ /// <param name="ascending">A boolean indicating whether the order should be ascending.</param>
17
+ /// <returns>A task that represents the asynchronous operation. The task result contains an IQueryable of TEntity.</returns>
18
+ public async Task < IQueryable < TEntity > > GetAllAsync ( Func < IQueryable < TEntity > ,
19
+ IIncludableQueryable < TEntity , object > > includes = null ! ,
20
+ Expression < Func < TEntity , bool > > filter = null ! ,
21
+ Expression < Func < TEntity , object > > orderBy = null ! ,
22
+ bool ascending = true )
23
+ {
24
+ var query = DbContext . Set < TEntity > ( ) . AsNoTracking ( ) ;
18
25
19
- /// <summary>
20
- /// Retrieves all entities that match the specified predicate asynchronously.
21
- /// </summary>
22
- /// <param name="predicate">The predicate to filter entities.</param>
23
- /// <returns>A task that represents the asynchronous operation. The task result contains a list of entities that match the predicate.</returns>
24
- public async Task < List < TEntity > > GetAllEntitiesAsync ( Func < TEntity , bool > predicate )
25
- => await Task . FromResult ( DbContext . Set < TEntity > ( ) . AsNoTracking ( ) . Where ( predicate ) . ToList ( ) ) ;
26
+ if ( includes != null )
27
+ {
28
+ query = includes ( query ) ;
29
+ }
30
+
31
+ if ( filter != null )
32
+ {
33
+ query = query . Where ( filter ) ;
34
+ }
35
+
36
+ if ( orderBy != null )
37
+ {
38
+ query = ascending ? query . OrderBy ( orderBy ) : query . OrderByDescending ( orderBy ) ;
39
+ }
40
+
41
+ return await Task . FromResult ( query ) ;
42
+ }
26
43
27
44
/// <summary>
28
45
/// Retrieves an entity by its identifier asynchronously.
@@ -72,7 +89,7 @@ public async Task UpdateAsync(TEntity entity)
72
89
}
73
90
74
91
/// <summary>
75
- /// Deletes an existing entity asynchronously.
92
+ /// Deletes an entity asynchronously.
76
93
/// </summary>
77
94
/// <param name="entity">The entity to delete.</param>
78
95
/// <returns>A task that represents the asynchronous operation.</returns>
@@ -98,37 +115,22 @@ public async Task DeleteByIdAsync(TKey id)
98
115
}
99
116
100
117
/// <summary>
101
- /// Retrieves a paginated list of entities that match the specified conditions asynchronously.
118
+ /// Retrieves a paginated result of entities asynchronously.
102
119
/// </summary>
103
- /// <param name="includes">A function to include related entities.</param>
104
- /// <param name="conditionWhere">The condition to filter entities.</param>
105
- /// <param name="orderBy">The expression to order entities.</param>
106
- /// <param name="ascending">A value indicating whether to order entities in ascending order.</param>
107
- /// <param name="pageIndex">The index of the page to retrieve.</param>
108
- /// <param name="pageSize">The size of the page to retrieve.</param>
109
- /// <returns>A task that represents the asynchronous operation. The task result contains a list of paginated entities.</returns>
110
- public Task < List < TEntity > > GetPaginatedAsync ( Func < IQueryable < TEntity > , IIncludableQueryable < TEntity , object > > includes ,
111
- Expression < Func < TEntity , bool > > conditionWhere , Expression < Func < TEntity , dynamic > > orderBy , bool ascending , int pageIndex , int pageSize )
120
+ /// <param name="query">The query to paginate.</param>
121
+ /// <param name="pageNumber">The page number to retrieve.</param>
122
+ /// <param name="pageSize">The size of the page.</param>
123
+ /// <returns>A task that represents the asynchronous operation. The task result contains a PaginatedResult of TEntity.</returns>
124
+ public async Task < PaginatedResult < TEntity > > GetPaginatedAsync ( IQueryable < TEntity > query , int pageNumber , int pageSize )
112
125
{
113
- IQueryable < TEntity > query = DbContext . Set < TEntity > ( ) ;
114
-
115
- query = query . AsNoTracking ( ) ;
116
-
117
- if ( includes != null )
126
+ var result = new PaginatedResult < TEntity >
118
127
{
119
- query = includes ( query ) ;
120
- }
121
-
122
- if ( conditionWhere != null )
123
- {
124
- query = query . Where ( conditionWhere ) ;
125
- }
126
-
127
- if ( orderBy != null )
128
- {
129
- query = query . OrderedByAscending ( orderBy , ascending ) ;
130
- }
128
+ CurrentPage = pageNumber ,
129
+ PageSize = pageSize ,
130
+ TotalItems = await query . CountAsync ( ) ,
131
+ Items = await query . Skip ( ( pageNumber - 1 ) * pageSize ) . Take ( pageSize ) . ToListAsync ( )
132
+ } ;
131
133
132
- return query . Page ( pageIndex , pageSize ) . ToListAsync ( ) ;
134
+ return result ;
133
135
}
134
- }
136
+ }
0 commit comments