-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalRepository.cs
283 lines (244 loc) · 11 KB
/
GlobalRepository.cs
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
277
278
279
280
281
282
283
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
namespace YourProject.Repositories.Global
{
public class GlobalRepository : IGlobalRepository
{
ApplicationDbContext _dbContext;
SmartDeletersService _smartDeleters;
public GlobalRepository(ApplicationDbContext dbContext, SmartDeletersService smartDeleters)
{
_smartDeleters = smartDeleters;
_dbContext = dbContext;
}
public List<T> AddRange<T>(List<T> itemsToAdd) where T : class
{
_dbContext.Set<T>().AddRange(itemsToAdd);
_dbContext.SaveChanges();
return itemsToAdd;
}
public T Add<T>(T itemToAdd) where T : class
{
_dbContext.Set<T>().Add(itemToAdd);
_dbContext.SaveChanges();
return itemToAdd;
}
public T Add<T>(T itemToAdd, bool mustSave) where T : class
{
_dbContext.Set<T>().Add(itemToAdd);
if (mustSave)
_dbContext.SaveChanges();
return itemToAdd;
}
public IQueryable<T> Get<T>(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes) where T : class
{
var query = _dbContext.Set<T>().AsNoTracking();
if (includes != null)
if (includes.Length > 0)
foreach (var include in includes)
query = query.Include(include);
return query.Where(predicate);
}
public IQueryable<T> ForceGet<T>(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includes) where T : class
{
var query = _dbContext.Set<T>().IgnoreQueryFilters();
if (includes != null)
if (includes.Length > 0)
foreach (var include in includes)
query = query.Include(include);
return query.Where(predicate);
}
public object Restore(BaseEntity itemToRestore)
{
itemToRestore.IsDeleted = false;
SmartUpdate(itemToRestore);
return itemToRestore;
}
public T SimpleUpdate<T>(T itemToUpdate) where T : class
{
_dbContext.Set<T>().Update(itemToUpdate);
_dbContext.SaveChanges();
return itemToUpdate;
}
//Will detach all the tracked entities
public void DetachAllEntities<T>() where T : class
{
var changedEntriesCopy = _dbContext.ChangeTracker.Entries<T>()
.ToList();
foreach (var entry in changedEntriesCopy)
entry.State = EntityState.Detached;
}
public object SmartUpdate(BaseEntity itemToUpdate)
{
var proprityInfos = itemToUpdate.GetType().GetProperties().Where(p => p.Name.Contains("Id"));
PropertyInfo keyProperty = null;
foreach (var item in proprityInfos)
{
var keyAttr = item
.GetCustomAttributes(typeof(KeyAttribute), false)
.Cast<KeyAttribute>().FirstOrDefault();
if (keyAttr != null)
{
keyProperty = item;
break;
}
}
if (keyProperty != null)
{
var aux = _dbContext.Find(itemToUpdate.GetType(), keyProperty.GetValue(itemToUpdate));
foreach (PropertyInfo propertyInfo in aux.GetType().GetProperties())
{
var itemProprity = itemToUpdate.GetType().GetProperties().Where(p => p.Name.Equals(propertyInfo.Name)).First();
var currentItem = itemProprity.GetValue(itemToUpdate);
if (currentItem != null)
if (!(itemProprity.Name.Contains("Id") && currentItem.ToString() == "0"))
if (propertyInfo.CanWrite && propertyInfo.GetSetMethod(true).IsPublic)
propertyInfo.SetValue(aux, itemProprity.GetValue(itemToUpdate));
}
_dbContext.SaveChanges();
return aux;
}
return "The sent object does not match any table in the database. Did you forget to set a Key property to the given entity ?";
}
//SOFT DELETE : Here you will find tow types of SoftDelete :
// - Simple Soft Delete : the one we know, which will change IsDelete to True then leave + SoftDeleteRange another simple SoftDelete but for a range of records
// - Secured Soft Delete : This one is charged many times :
// - 2 times : Accept one record and check in one type + a SecuredSoftDeleteRange with the same principe but for a range of records
// - 2 times : Accept one record and a list of types + one with the same principe but for a range of records
//A Simple SoftDelete which will change the IsDeleted to true then return the updated record
public object SoftDelete(BaseEntity itemToDelete)
{
itemToDelete.IsDeleted = true;
itemToDelete.DeletedAt = DateTime.Now;
SmartUpdate(itemToDelete);
return itemToDelete;
}
//A Simple SoftDelete which will change the IsDeleted to true then return the updated record with the possibility to not save the changes (usefull for SoftDeleteRange)
public object SoftDelete(BaseEntity itemToDelete, bool saveChanges)
{
itemToDelete.IsDeleted = true;
itemToDelete.DeletedAt = DateTime.Now;
if(saveChanges)
SmartUpdate(itemToDelete);
return itemToDelete;
}
//Optimized SoftDeleteRange
public object SoftDeleteRange(List<BaseEntity> itemsToDelete)
{
itemsToDelete.ForEach(item => SoftDelete(item, false));
_dbContext.SaveChanges();
return itemsToDelete;
}
//A SoftDelete which will check if the "itemToDelete" is used in the database by the "toCheckin" type, if it is used it will not delete the record
public object SecuredSoftDelete(BaseEntity itemToDelete, Type toCheckIn)
{
if (!_smartDeleters.IsUsedBy(itemToDelete, toCheckIn))
{
itemToDelete.IsDeleted = true;
itemToDelete.DeletedAt = DateTime.Now;
SmartUpdate(itemToDelete);
}
return itemToDelete;
}
//Same as SecuredSoftDelete(BaseEntity itemToDelete, Type toCheckIn) with the possibility to not save the changes (usefull for SecuredSoftDeleteRange)
public object SecuredSoftDelete(BaseEntity itemToDelete, Type toCheckIn, bool saveChanges)
{
if (!_smartDeleters.IsUsedBy(itemToDelete, toCheckIn))
{
itemToDelete.IsDeleted = true;
itemToDelete.DeletedAt = DateTime.Now;
if (saveChanges)
SmartUpdate(itemToDelete);
}
return itemToDelete;
}
//SecuredSoftDelete for multiple records with 1 type as a parameter
public object SecuredSoftDeleteRange(List<BaseEntity> itemsToDelete, Type toCheckIn)
{
itemsToDelete.ForEach(item => SecuredSoftDelete(item, toCheckIn, false));
_dbContext.SaveChanges();
return itemsToDelete;
}
//A SoftDelete which will check if the "itemToDelete" is used in the database by the list of the "toCheckin" type, if it is used by 1 record it will not delete the record
public object SecuredSoftDelete(BaseEntity itemToDelete, List<Type> toCheckIn)
{
if (!_smartDeleters.IsUsedBy(itemToDelete, toCheckIn))
{
itemToDelete.IsDeleted = true;
itemToDelete.DeletedAt = DateTime.Now;
SmartUpdate(itemToDelete);
}
return itemToDelete;
}
//same as SoftDelete(BaseEntity itemToDelete, List<Type> toCheckIn) with the possibility to not save the changes (usefull for SecuredSoftDeleteRange)
public object SecuredSoftDelete(BaseEntity itemToDelete, List<Type> toCheckIn, bool saveChanges)
{
if (!_smartDeleters.IsUsedBy(itemToDelete, toCheckIn))
{
itemToDelete.IsDeleted = true;
itemToDelete.DeletedAt = DateTime.Now;
if (saveChanges)
SmartUpdate(itemToDelete);
}
return itemToDelete;
}
//SecuredSoftDelete for multiple records with many types as a parameter
public object SecuredSoftDeleteRange(List<BaseEntity> itemsToDelete, List<Type> toCheckIn)
{
itemsToDelete.ForEach(item => SecuredSoftDelete(item, toCheckIn, false));
_dbContext.SaveChanges();
return itemsToDelete;
}
//Hard DELETE : Same logic as SoftDelete but this one will perma delete thje records
public void HardDelete<T>(T itemToDelete) where T : class
{
_dbContext.Remove(itemToDelete);
_dbContext.SaveChanges();
}
public void HardDelete<T>(T itemToDelete, bool saveChanges) where T : class
{
_dbContext.Remove(itemToDelete);
if(saveChanges)
_dbContext.SaveChanges();
}
public void HardDeleteRange<T>(List<T> itemsToDelete) where T : class
{
_dbContext.RemoveRange(itemsToDelete);
_dbContext.SaveChanges();
}
public void SecuredHardDelete<T>(T itemToDelete, Type toCheckIn) where T : class
{
if (!_smartDeleters.IsUsedBy(itemToDelete, toCheckIn))
HardDelete(itemToDelete);
}
public void SecuredHardDelete<T>(T itemToDelete, Type toCheckIn, bool saveChanges) where T : class
{
if (!_smartDeleters.IsUsedBy(itemToDelete, toCheckIn))
HardDelete(itemToDelete, saveChanges);
}
public void SecuredHardDeleteRange<T>(List<T> itemsToDelete, Type toCheckIn) where T : class
{
itemsToDelete.ForEach(item => SecuredHardDelete(item, toCheckIn, false));
_dbContext.SaveChanges();
}
public void SecuredHardDelete<T>(T itemToDelete, List<Type> toCheckIn) where T : class
{
if (!_smartDeleters.IsUsedBy(itemToDelete, toCheckIn))
HardDelete(itemToDelete);
}
public void SecuredHardDelete<T>(T itemToDelete, List<Type> toCheckIn, bool saveChanges) where T : class
{
if (!_smartDeleters.IsUsedBy(itemToDelete, toCheckIn))
HardDelete(itemToDelete, saveChanges);
}
public void SecuredHardDeleteRange<T>(List<T> itemsToDelete, List<Type> toCheckIn) where T : class
{
itemsToDelete.ForEach(item => SecuredHardDelete(item, toCheckIn, false));
_dbContext.SaveChanges();
}
}
}