2
2
3
3
namespace ClassLibrary . EFCore ;
4
4
5
- /// <summary>
6
- /// Repository class for Entity Framework Core operations.
7
- /// </summary>
8
- /// <typeparam name="TEntity">The type of the entity.</typeparam>
9
- /// <typeparam name="TKey">The type of the entity's primary key.</typeparam>
10
5
public class Repository < TEntity , TKey > ( DbContext dbContext ) : IRepository < TEntity , TKey > where TEntity : class , IEntity < TKey > , new ( )
11
6
{
12
7
/// <summary>
13
- /// Gets the DbContext .
8
+ /// Gets the database context .
14
9
/// </summary>
15
10
public DbContext DbContext { get ; } = dbContext ?? throw new ArgumentNullException ( nameof ( dbContext ) ) ;
16
11
17
12
/// <summary>
18
- /// Asynchronously gets all entities.
13
+ /// Retrieves all entities asynchronously .
19
14
/// </summary>
20
- /// <returns>A task that represents the asynchronous operation. The task result contains a list of entities.</returns>
15
+ /// <returns>A task that represents the asynchronous operation. The task result contains a list of all entities.</returns>
21
16
public async Task < List < TEntity > > GetAllAsync ( )
22
17
=> await DbContext . Set < TEntity > ( ) . AsNoTracking ( ) . ToListAsync ( ) ;
23
18
24
19
/// <summary>
25
- /// Asynchronously gets all entities that satisfy the given predicate.
20
+ /// Retrieves all entities that match the specified predicate asynchronously .
26
21
/// </summary>
27
- /// <param name="predicate">A function to test each element for a condition .</param>
28
- /// <returns>A task that represents the asynchronous operation. The task result contains a list of entities that satisfy the condition .</returns>
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>
29
24
public async Task < List < TEntity > > GetAllEntitiesAsync ( Func < TEntity , bool > predicate )
30
25
=> await Task . FromResult ( DbContext . Set < TEntity > ( ) . AsNoTracking ( ) . Where ( predicate ) . ToList ( ) ) ;
31
26
32
27
/// <summary>
33
- /// Asynchronously gets an entity by its id .
28
+ /// Retrieves an entity by its identifier asynchronously .
34
29
/// </summary>
35
- /// <param name="id">The id of the entity.</param>
30
+ /// <param name="id">The identifier of the entity.</param>
36
31
/// <returns>A task that represents the asynchronous operation. The task result contains the entity if found; otherwise, null.</returns>
37
32
public async Task < TEntity ? > GetByIdAsync ( TKey id )
38
33
{
@@ -49,7 +44,7 @@ public async Task<List<TEntity>> GetAllEntitiesAsync(Func<TEntity, bool> predica
49
44
}
50
45
51
46
/// <summary>
52
- /// Asynchronously creates a new entity.
47
+ /// Creates a new entity asynchronously .
53
48
/// </summary>
54
49
/// <param name="entity">The entity to create.</param>
55
50
/// <returns>A task that represents the asynchronous operation.</returns>
@@ -63,7 +58,7 @@ public async Task CreateAsync(TEntity entity)
63
58
}
64
59
65
60
/// <summary>
66
- /// Asynchronously updates an existing entity.
61
+ /// Updates an existing entity asynchronously .
67
62
/// </summary>
68
63
/// <param name="entity">The entity to update.</param>
69
64
/// <returns>A task that represents the asynchronous operation.</returns>
@@ -77,7 +72,7 @@ public async Task UpdateAsync(TEntity entity)
77
72
}
78
73
79
74
/// <summary>
80
- /// Asynchronously deletes an existing entity.
75
+ /// Deletes an existing entity asynchronously .
81
76
/// </summary>
82
77
/// <param name="entity">The entity to delete.</param>
83
78
/// <returns>A task that represents the asynchronous operation.</returns>
@@ -89,9 +84,9 @@ public async Task DeleteAsync(TEntity entity)
89
84
}
90
85
91
86
/// <summary>
92
- /// Asynchronously deletes an entity by its id .
87
+ /// Deletes an entity by its identifier asynchronously .
93
88
/// </summary>
94
- /// <param name="id">The id of the entity to delete.</param>
89
+ /// <param name="id">The identifier of the entity to delete.</param>
95
90
/// <returns>A task that represents the asynchronous operation.</returns>
96
91
public async Task DeleteByIdAsync ( TKey id )
97
92
{
@@ -103,12 +98,12 @@ public async Task DeleteByIdAsync(TKey id)
103
98
}
104
99
105
100
/// <summary>
106
- /// Asynchronously gets a paginated list of entities.
101
+ /// Retrieves a paginated list of entities that match the specified conditions asynchronously .
107
102
/// </summary>
108
103
/// <param name="includes">A function to include related entities.</param>
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>
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>
112
107
/// <param name="pageIndex">The index of the page to retrieve.</param>
113
108
/// <param name="pageSize">The size of the page to retrieve.</param>
114
109
/// <returns>A task that represents the asynchronous operation. The task result contains a list of paginated entities.</returns>
@@ -117,6 +112,8 @@ public Task<List<TEntity>> GetPaginatedAsync(Func<IQueryable<TEntity>, IIncludab
117
112
{
118
113
IQueryable < TEntity > query = DbContext . Set < TEntity > ( ) ;
119
114
115
+ query = query . AsNoTracking ( ) ;
116
+
120
117
if ( includes != null )
121
118
{
122
119
query = includes ( query ) ;
@@ -132,6 +129,6 @@ public Task<List<TEntity>> GetPaginatedAsync(Func<IQueryable<TEntity>, IIncludab
132
129
query = query . OrderedByAscending ( orderBy , ascending ) ;
133
130
}
134
131
135
- return query . Page ( pageIndex , pageSize ) . AsNoTracking ( ) . ToListAsync ( ) ;
132
+ return query . Page ( pageIndex , pageSize ) . ToListAsync ( ) ;
136
133
}
137
134
}
0 commit comments