Skip to content

Commit 9dab318

Browse files
committed
Refactoring methods
1 parent bf8978b commit 9dab318

File tree

3 files changed

+60
-30
lines changed

3 files changed

+60
-30
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace ClassLibrary.EFCore.Extensions;
2+
3+
public static class RepositoryExtensions
4+
{
5+
/// <summary>
6+
/// Paginates the queryable source by skipping the specified number of items and taking the specified page size.
7+
/// </summary>
8+
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
9+
/// <param name="source">The queryable source to paginate.</param>
10+
/// <param name="page">The page number to retrieve.</param>
11+
/// <param name="pageSize">The number of items per page.</param>
12+
/// <returns>A queryable source that contains the paginated items.</returns>
13+
public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int page, int pageSize)
14+
=> source.Skip((page - 1) * pageSize).Take(pageSize);
15+
16+
/// <summary>
17+
/// Orders the queryable source by the specified order expression in ascending or descending order.
18+
/// </summary>
19+
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
20+
/// <param name="sources">The queryable source to order.</param>
21+
/// <param name="orderBy">The expression to order by.</param>
22+
/// <param name="ascending">A boolean value indicating whether to order in ascending (true) or descending (false) order. Default is true.</param>
23+
/// <returns>A queryable source that is ordered according to the specified expression and order direction.</returns>
24+
public static IQueryable<TSource> OrderedByAscending<TSource>(this IQueryable<TSource> sources,
25+
Expression<Func<TSource, dynamic>> orderBy, bool ascending = true)
26+
{
27+
return ascending switch
28+
{
29+
false => sources.OrderByDescending(orderBy),
30+
_ => sources.OrderBy(orderBy)
31+
};
32+
}
33+
34+
//public static IQueryable<TSource> OrderByAscending<TSource>(this IQueryable<TSource> sources, Expression<Func<TSource, dynamic>> orderBy)
35+
// => sources.OrderBy(orderBy);
36+
37+
//public static IQueryable<TSource> OrderByDescending<TSource>(this IQueryable<TSource> sources, Expression<Func<TSource, dynamic>> orderBy)
38+
// => sources.OrderByDescending(orderBy);
39+
}

src/ClassLibrary.EFCore/Interfaces/IRepository.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,15 @@
5656
Task DeleteByIdAsync(TKey id);
5757

5858
/// <summary>
59-
/// Asynchronously gets a paginated list of entities.
59+
/// Asynchronously gets a paginated list of entities that satisfy the given conditions.
6060
/// </summary>
6161
/// <param name="includes">A function to include related entities.</param>
62-
/// <param name="conditionWhere">A condition to filter the entities.</param>
63-
/// <param name="orderBy">A function to order the entities.</param>
64-
/// <param name="orderType">The type of the order.</param>
65-
/// <param name="pageIndex">The index of the page.</param>
66-
/// <param name="pageSize">The size of the page.</param>
67-
/// <returns>A task that represents the asynchronous operation. The task result contains a list of entities.</returns>
68-
Task<List<TEntity>> GetPaginatedAsync(Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includes,
69-
Expression<Func<TEntity, bool>> conditionWhere, Expression<Func<TEntity, dynamic>> orderBy, string orderType,
70-
int pageIndex, int pageSize);
62+
/// <param name="conditionWhere">A function to test each element for a condition.</param>
63+
/// <param name="orderBy">A function to order the elements.</param>
64+
/// <param name="ascending">A boolean indicating whether the order is ascending.</param>
65+
/// <param name="pageIndex">The index of the page to retrieve.</param>
66+
/// <param name="pageSize">The size of the page to retrieve.</param>
67+
/// <returns>A task that represents the asynchronous operation. The task result contains a list of entities that satisfy the conditions.</returns>
68+
public Task<List<TEntity>> GetPaginatedAsync(Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includes,
69+
Expression<Func<TEntity, bool>> conditionWhere, Expression<Func<TEntity, dynamic>> orderBy, bool ascending, int pageIndex, int pageSize);
7170
}

src/ClassLibrary.EFCore/Repository.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace ClassLibrary.EFCore;
1+
using ClassLibrary.EFCore.Extensions;
2+
3+
namespace ClassLibrary.EFCore;
24

35
/// <summary>
46
/// Repository class for Entity Framework Core operations.
@@ -104,14 +106,14 @@ public async Task DeleteByIdAsync(TKey id)
104106
/// Asynchronously gets a paginated list of entities.
105107
/// </summary>
106108
/// <param name="includes">A function to include related entities.</param>
107-
/// <param name="conditionWhere">A function to filter the entities.</param>
108-
/// <param name="orderBy">A function to sort the entities.</param>
109-
/// <param name="orderType">The type of the order ("ASC" for ascending, "DESC" for descending).</param>
110-
/// <param name="pageIndex">The index of the page.</param>
111-
/// <param name="pageSize">The size of the page.</param>
112-
/// <returns>A task that represents the asynchronous operation. The task result contains a list of entities.</returns>
109+
/// <param name="conditionWhere">A condition to filter the entities.</param>
110+
/// <param name="orderBy">A function to order the entities.</param>
111+
/// <param name="ascending">A boolean indicating whether the order is ascending.</param>
112+
/// <param name="pageIndex">The index of the page to retrieve.</param>
113+
/// <param name="pageSize">The size of the page to retrieve.</param>
114+
/// <returns>A task that represents the asynchronous operation. The task result contains a list of paginated entities.</returns>
113115
public Task<List<TEntity>> GetPaginatedAsync(Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includes,
114-
Expression<Func<TEntity, bool>> conditionWhere, Expression<Func<TEntity, dynamic>> orderBy, string orderType, int pageIndex, int pageSize)
116+
Expression<Func<TEntity, bool>> conditionWhere, Expression<Func<TEntity, dynamic>> orderBy, bool ascending, int pageIndex, int pageSize)
115117
{
116118
IQueryable<TEntity> query = DbContext.Set<TEntity>();
117119

@@ -127,19 +129,9 @@ public Task<List<TEntity>> GetPaginatedAsync(Func<IQueryable<TEntity>, IIncludab
127129

128130
if (orderBy != null)
129131
{
130-
query = orderType switch
131-
{
132-
"ASC" => query.OrderBy(orderBy),
133-
"DESC" => query.OrderByDescending(orderBy),
134-
_ => query.OrderBy(orderBy),
135-
};
136-
}
137-
138-
if (pageIndex != 0 && pageSize != 0)
139-
{
140-
query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
132+
query = query.OrderedByAscending(orderBy, ascending);
141133
}
142134

143-
return query.AsNoTracking().ToListAsync();
135+
return query.Page(pageIndex, pageSize).AsNoTracking().ToListAsync();
144136
}
145137
}

0 commit comments

Comments
 (0)