1- using ClassLibrary . EFCore . Extensions ;
2-
3- namespace ClassLibrary . EFCore ;
1+ namespace ClassLibrary . EFCore ;
42
53public class Repository < TEntity , TKey > ( DbContext dbContext ) : IRepository < TEntity , TKey > where TEntity : class , IEntity < TKey > , new ( )
64{
@@ -10,19 +8,38 @@ namespace ClassLibrary.EFCore;
108 public DbContext DbContext { get ; } = dbContext ?? throw new ArgumentNullException ( nameof ( dbContext ) ) ;
119
1210 /// <summary>
13- /// Retrieves all entities asynchronously.
11+ /// Retrieves all entities asynchronously with optional filtering, ordering, and including related entities .
1412 /// </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 ( ) ;
1825
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+ }
2643
2744 /// <summary>
2845 /// Retrieves an entity by its identifier asynchronously.
@@ -72,7 +89,7 @@ public async Task UpdateAsync(TEntity entity)
7289 }
7390
7491 /// <summary>
75- /// Deletes an existing entity asynchronously.
92+ /// Deletes an entity asynchronously.
7693 /// </summary>
7794 /// <param name="entity">The entity to delete.</param>
7895 /// <returns>A task that represents the asynchronous operation.</returns>
@@ -98,37 +115,22 @@ public async Task DeleteByIdAsync(TKey id)
98115 }
99116
100117 /// <summary>
101- /// Retrieves a paginated list of entities that match the specified conditions asynchronously.
118+ /// Retrieves a paginated result of entities asynchronously.
102119 /// </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 )
112125 {
113- IQueryable < TEntity > query = DbContext . Set < TEntity > ( ) ;
114-
115- query = query . AsNoTracking ( ) ;
116-
117- if ( includes != null )
126+ var result = new PaginatedResult < TEntity >
118127 {
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+ } ;
131133
132- return query . Page ( pageIndex , pageSize ) . ToListAsync ( ) ;
134+ return result ;
133135 }
134- }
136+ }
0 commit comments