Skip to content

Commit 950f33d

Browse files
committed
Refactoring Repository e relativa interfaccia
1 parent e98d165 commit 950f33d

File tree

2 files changed

+62
-65
lines changed

2 files changed

+62
-65
lines changed

src/ClassLibrary.EFCore/Interfaces/IRepository.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
public interface IRepository<TEntity, TKey> where TEntity : class, IEntity<TKey>, new()
44
{
55
/// <summary>
6-
/// Retrieves all entities asynchronously.
6+
/// Retrieves all entities asynchronously with optional filtering, ordering, and including related entities.
77
/// </summary>
8-
/// <returns>A task that represents the asynchronous operation. The task result contains a list of all entities.</returns>
9-
Task<List<TEntity>> GetAllAsync();
10-
11-
/// <summary>
12-
/// Retrieves all entities that match the specified predicate asynchronously.
13-
/// </summary>
14-
/// <param name="predicate">The predicate to filter entities.</param>
15-
/// <returns>A task that represents the asynchronous operation. The task result contains a list of entities that match the predicate.</returns>
16-
Task<List<TEntity>> GetAllEntitiesAsync(Func<TEntity, bool> predicate);
8+
/// <param name="includes">A function to include related entities.</param>
9+
/// <param name="filter">A filter expression to apply.</param>
10+
/// <param name="orderBy">An expression to order the results.</param>
11+
/// <param name="ascending">A boolean indicating whether the order should be ascending.</param>
12+
/// <returns>A task that represents the asynchronous operation. The task result contains an IQueryable of TEntity.</returns>
13+
Task<IQueryable<TEntity>> GetAllAsync(Func<IQueryable<TEntity>,
14+
IIncludableQueryable<TEntity, object>> includes = null!, Expression<Func<TEntity, bool>> filter = null!,
15+
Expression<Func<TEntity, object>> orderBy = null!, bool ascending = true);
1716

1817
/// <summary>
1918
/// Retrieves an entity by its identifier asynchronously.
@@ -37,7 +36,7 @@
3736
Task UpdateAsync(TEntity entity);
3837

3938
/// <summary>
40-
/// Deletes an existing entity asynchronously.
39+
/// Deletes an entity asynchronously.
4140
/// </summary>
4241
/// <param name="entity">The entity to delete.</param>
4342
/// <returns>A task that represents the asynchronous operation.</returns>
@@ -51,15 +50,11 @@
5150
Task DeleteByIdAsync(TKey id);
5251

5352
/// <summary>
54-
/// Retrieves a paginated list of entities that match the specified conditions asynchronously.
53+
/// Retrieves a paginated result of entities asynchronously.
5554
/// </summary>
56-
/// <param name="includes">A function to include related entities.</param>
57-
/// <param name="conditionWhere">The condition to filter entities.</param>
58-
/// <param name="orderBy">The expression to order entities.</param>
59-
/// <param name="ascending">A value indicating whether to order entities in ascending order.</param>
60-
/// <param name="pageIndex">The index of the page to retrieve.</param>
61-
/// <param name="pageSize">The size of the page to retrieve.</param>
62-
/// <returns>A task that represents the asynchronous operation. The task result contains a list of paginated entities.</returns>
63-
public Task<List<TEntity>> GetPaginatedAsync(Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> includes,
64-
Expression<Func<TEntity, bool>> conditionWhere, Expression<Func<TEntity, dynamic>> orderBy, bool ascending, int pageIndex, int pageSize);
55+
/// <param name="query">The query to paginate.</param>
56+
/// <param name="pageNumber">The page number to retrieve.</param>
57+
/// <param name="pageSize">The size of the page.</param>
58+
/// <returns>A task that represents the asynchronous operation. The task result contains a PaginatedResult of TEntity.</returns>
59+
Task<PaginatedResult<TEntity>> GetPaginatedAsync(IQueryable<TEntity> query, int pageNumber, int pageSize);
6560
}

src/ClassLibrary.EFCore/Repository.cs

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

53
public 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

Comments
 (0)