-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRepository.cs
More file actions
276 lines (210 loc) · 8.25 KB
/
Copy pathRepository.cs
File metadata and controls
276 lines (210 loc) · 8.25 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
267
268
269
270
271
272
273
274
275
276
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using GenericRepositoryUnitOfWork.Data;
using Microsoft.EntityFrameworkCore;
namespace GenericRepositoryUnitOfWork.Repository
{
public class Repository<T> : IRepository<T> where T : class
{
private readonly ApplicationDbContext _context;
public string ErrorMessage { get; set; } = string.Empty;
public Repository(ApplicationDbContext context)
{
_context = context;
}
public int Count()
{
return _context.Set<T>().Count();
}
public async Task<int> CountAsync()
{
return await _context.Set<T>().CountAsync();
}
public void Delete(T t)
{
if (_context.Set<T>() == null)
{
ErrorMessage = "The item you trying to Delete is not in Database anymore";
throw new ArgumentNullException(nameof(t));
}
_context.Set<T>().Remove(t);
}
public bool Exist(Expression<Func<T, bool>> predicate)
{
var exist = _context.Set<T>().Where(predicate);
return exist.Any();
}
public IEnumerable<T> Filter(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "", int? page = null, int? pageSize = null)
{
IQueryable<T> query = _context.Set<T>();
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
query = orderBy(query);
}
if (includeProperties != null)
{
foreach (
var includeProperty in includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
}
if (page != null && pageSize != null)
{
query = query.Skip((page.Value - 1) * pageSize.Value).Take(pageSize.Value);
}
return query.ToList();
}
public T Find(Expression<Func<T, bool>> match)
{
return _context.Set<T>().SingleOrDefault(match);
}
public ICollection<T> FindAll(Expression<Func<T, bool>> match)
{
return _context.Set<T>().Where(match).ToList();
}
public async Task<ICollection<T>> FindAllAsync(Expression<Func<T, bool>> match)
{
return await _context.Set<T>().Where(match).ToListAsync();
}
public async Task<T> FindAsync(Expression<Func<T, bool>> match)
{
return await _context.Set<T>().SingleOrDefaultAsync(match);
}
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
{
return _context.Set<T>().Where(predicate);
}
public ICollection<T> GetAll()
{
return _context.Set<T>().ToList();
}
public async Task<ICollection<T>> GetAllAsync()
{
return await _context.Set<T>().ToListAsync();
}
public IList<T> GetAllInclude(params Expression<Func<T, object>>[] navigationProperties)
{
IQueryable<T> dbQuery = _context.Set<T>();
//Apply eager loading
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
dbQuery = dbQuery.Include<T, object>(navigationProperty);
var list = dbQuery
.AsNoTracking()
.ToList();
return list;
}
public async Task<IList<T>> GetAllIncludeAsync(params Expression<Func<T, object>>[] navigationProperties)
{
IQueryable<T> dbQuery = _context.Set<T>();
//Apply eager loading
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
dbQuery = dbQuery.Include<T, object>(navigationProperty);
return await dbQuery.AsNoTracking().ToListAsync();
}
/// <summary>
/// returns list of cattle by firm
/// </summary>
public T GetById(int? id)
{
return _context.Set<T>().Find(id);
}
public async Task<T> GetByIdAsync(int? id)
{
return await _context.Set<T>().FindAsync(id);
}
public IList<T> GetIncludeList(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties)
{
List<T> list;
IQueryable<T> dbQuery = _context.Set<T>();
//Apply eager loading
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
dbQuery = dbQuery.Include<T, object>(navigationProperty);
list = dbQuery
.AsNoTracking().AsEnumerable()
.Where(where)
.ToList<T>();
return list;
}
public T GetSingleInclude(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties)
{
T item = null;
IQueryable<T> dbQuery = _context.Set<T>();
//Apply eager loading
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
dbQuery = dbQuery.Include<T, object>(navigationProperty);
item = dbQuery
.AsNoTracking() //Don't track any changes for the selected item
.FirstOrDefault(where); //Apply where clause
return item;
}
public async Task<T> GetSingleIncludeAsync(Expression<Func<T, bool>> where, params Expression<Func<T, object>>[] navigationProperties)
{
T item = null;
IQueryable<T> dbQuery = _context.Set<T>();
//Apply eager loading
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
dbQuery = dbQuery.Include<T, object>(navigationProperty);
item = await dbQuery
.AsNoTracking() //Don't track any changes for the selected item
.SingleOrDefaultAsync(where); //Apply where clause
return item;
}
public T Insert(T entity)
{
_context.Set<T>().Add(entity);
return entity;
}
public ICollection<T> PaggedList(int? pageSize, int? page, params Expression<Func<T, object>>[] navigationProperties)
{
IQueryable<T> query = _context.Set<T>();
//Apply eager loadingf
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
query = query.Include<T, object>(navigationProperty);
if (page != null && pageSize != null)
{
query = query.Skip((page.Value - 1) * pageSize.Value).Take(pageSize.Value);
}
return query.ToList();
}
public async Task<ICollection<T>> PaggedListAsync(int? pageSize, int? page, params Expression<Func<T, object>>[] navigationProperties)
{
IQueryable<T> query = _context.Set<T>();
//Apply eager loadingf
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
query = query.Include<T, object>(navigationProperty);
if (page != null && pageSize != null)
{
query = query.Skip((page.Value - 1) * pageSize.Value).Take(pageSize.Value);
}
return await query.ToListAsync();
}
/// <summary>
///
/// Query like normal ef query
///
/// <code>Query()</code>
///</summary>
public IQueryable<T> Query()
{
return _context.Set<T>().AsQueryable();
}
public void Update(T updated)
{
if (_context.Set<T>() == null)
{
ErrorMessage = "No Database detected";
throw new ArgumentNullException(nameof(updated));
}
_context.Set<T>().Attach(updated);
_context.Entry(updated).State = EntityState.Modified;
}
}
}