-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGenericRepository.cs
More file actions
266 lines (222 loc) · 7.54 KB
/
GenericRepository.cs
File metadata and controls
266 lines (222 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Simplify.Repository.EntityFramework;
/// <summary>
/// Provides generic repository pattern for easy Entity Framework repositories implementation
/// </summary>
/// <typeparam name="T"></typeparam>
/// <remarks>
/// Initializes a new instance of the <see cref="GenericRepository{T}"/> class.
/// </remarks>
/// <param name="session">The session.</param>
public class GenericRepository<T>(DbContext session) : IGenericRepository<T>
where T : class
{
/// <summary>
/// The NHibernate session
/// </summary>
protected readonly DbContext Session = session;
/// <summary>
/// Gets the single object by identifier.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns></returns>
public T GetSingleByID(object id) => Session.Find<T>(id)!;
/// <summary>
/// Gets the single object by identifier asynchronously.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns></returns>
public Task<T> GetSingleByIDAsync(object id) => Session.FindAsync<T>(id).AsTask()!;
/// <summary>
/// Gets the single object by query.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public T GetSingleByQuery(Expression<Func<T, bool>> query) => Session.Set<T>().SingleOrDefault(query)!;
/// <summary>
/// Gets the single object by query asynchronously.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public Task<T> GetSingleByQueryAsync(Expression<Func<T, bool>> query) => Session.Set<T>().FirstAsync(query);
/// <summary>
/// Gets the first object by query.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public T GetFirstByQuery(Expression<Func<T, bool>> query) => Session.Set<T>().FirstOrDefault(query)!;
/// <summary>
/// Gets the first object by query asynchronously.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public Task<T> GetFirstByQueryAsync(Expression<Func<T, bool>> query) => Session.Set<T>().FirstOrDefaultAsync(query)!;
/// <summary>
/// Gets the multiple objects by query or all objects without query.
/// </summary>
/// <param name="query">The query.</param>
/// <param name="customProcessing">The custom processing.</param>
/// <returns></returns>
public IList<T> GetMultiple(Expression<Func<T, bool>>? query = null, Func<IQueryable<T>, IQueryable<T>>? customProcessing = null)
{
var queryable = Session.Set<T>().AsQueryable();
if (query != null)
queryable = queryable.Where(query);
if (customProcessing != null)
queryable = customProcessing(queryable);
return queryable.ToList();
}
/// <summary>
/// Gets the multiple objects by query or all objects without query asynchronously.
/// </summary>
/// <param name="query">The query.</param>
/// <param name="customProcessing">The custom processing.</param>
/// <returns></returns>
public async Task<IList<T>> GetMultipleAsync(Expression<Func<T, bool>>? query = null, Func<IQueryable<T>, IQueryable<T>>? customProcessing = null)
{
var queryable = Session.Set<T>().AsQueryable();
if (query != null)
queryable = queryable.Where(query);
if (customProcessing != null)
queryable = customProcessing(queryable);
return await queryable.ToListAsync();
}
/// <summary>
/// Gets the multiple paged elements list.
/// </summary>
/// <param name="pageIndex">Index of the page.</param>
/// <param name="itemsPerPage">The items per page number.</param>
/// <param name="query">The query.</param>
/// <param name="customProcessing">The custom processing.</param>
/// <returns></returns>
public IList<T> GetPaged(int pageIndex,
int itemsPerPage,
Expression<Func<T, bool>>? query = null,
Func<IQueryable<T>, IQueryable<T>>? customProcessing = null)
{
var queryable = Session.Set<T>().AsQueryable();
if (query != null)
queryable = queryable.Where(query);
if (customProcessing != null)
queryable = customProcessing(queryable);
return queryable.Skip(pageIndex * itemsPerPage)
.Take(itemsPerPage).ToList();
}
/// <summary>
/// Gets the multiple paged elements list asynchronously.
/// </summary>
/// <param name="pageIndex">Index of the page.</param>
/// <param name="itemsPerPage">The items per page number.</param>
/// <param name="query">The query.</param>
/// <param name="customProcessing">The custom processing.</param>
/// <returns></returns>
public async Task<IList<T>> GetPagedAsync(int pageIndex,
int itemsPerPage,
Expression<Func<T, bool>>? query = null,
Func<IQueryable<T>, IQueryable<T>>? customProcessing = null)
{
var queryable = Session.Set<T>().AsQueryable();
if (query != null)
queryable = queryable.Where(query);
if (customProcessing != null)
queryable = customProcessing(queryable);
return await queryable.Skip(pageIndex * itemsPerPage)
.Take(itemsPerPage).ToListAsync();
}
/// <summary>
/// Gets the number of elements.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public int GetCount(Expression<Func<T, bool>>? query = null)
{
var queryable = Session.Set<T>().AsQueryable();
if (query != null)
queryable = queryable.Where(query);
return queryable.Count();
}
/// <summary>
/// Gets the number of elements asynchronously.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public Task<int> GetCountAsync(Expression<Func<T, bool>>? query = null)
{
var queryable = Session.Set<T>().AsQueryable();
if (query != null)
queryable = queryable.Where(query);
return queryable.CountAsync();
}
/// <summary>
/// Gets the long number of elements.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public long GetLongCount(Expression<Func<T, bool>>? query = null)
{
var queryable = Session.Set<T>().AsQueryable();
if (query != null)
queryable = queryable.Where(query);
return queryable.LongCount();
}
/// <summary>
/// Gets the long number of elements asynchronously.
/// </summary>
/// <param name="query">The query.</param>
/// <returns></returns>
public Task<long> GetLongCountAsync(Expression<Func<T, bool>>? query = null)
{
var queryable = Session.Set<T>().AsQueryable();
if (query != null)
queryable = queryable.Where(query);
return queryable.LongCountAsync();
}
/// <summary>
/// Adds the object.
/// </summary>
/// <param name="entity">The entity.</param>
public object Add(T entity) => Session.Add(entity);
/// <summary>
/// Adds the object asynchronously.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns>
/// The generated identifier
/// </returns>
public async Task<object> AddAsync(T entity) => await Session.AddAsync(entity).AsTask();
/// <summary>
/// Deletes the object.
/// </summary>
/// <param name="entity">The entity.</param>
public void Delete(T entity) => Session.Remove(entity);
/// <summary>
/// Deletes the object asynchronously.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
public Task DeleteAsync(T entity)
{
Session.Remove(entity);
return Task.CompletedTask;
}
/// <summary>
/// Updates the object.
/// </summary>
/// <param name="entity">The entity.</param>
public void Update(T entity) => Session.Update(entity);
/// <summary>
/// Updates the object asynchronously.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
public Task UpdateAsync(T entity)
{
Session.Update(entity);
return Task.CompletedTask;
}
}