|
@@ -21,7 +21,7 @@ namespace Luticate2.Utils.DataAccess
|
21
|
21
|
where TDboUpdate : class
|
22
|
22
|
where TDbContext : DbContext
|
23
|
23
|
{
|
24
|
|
- protected LuEfCrudDataAccess(TDbContext db, DbSet<TModel> table) : base(db, table)
|
|
24
|
+ protected LuEfCrudDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
|
25
|
25
|
{
|
26
|
26
|
}
|
27
|
27
|
|
|
@@ -66,19 +66,19 @@ namespace Luticate2.Utils.DataAccess
|
66
|
66
|
return id;
|
67
|
67
|
}
|
68
|
68
|
|
69
|
|
- protected virtual IQueryable<TModel> GetGetQueryable()
|
|
69
|
+ protected virtual IQueryable<TModel> GetGetQueryable(DbContext db, DbSet<TModel> table)
|
70
|
70
|
{
|
71
|
|
- return Table;
|
|
71
|
+ return table;
|
72
|
72
|
}
|
73
|
73
|
|
74
|
|
- protected virtual IQueryable<TModel> GetEditQueryable()
|
|
74
|
+ protected virtual IQueryable<TModel> GetEditQueryable(DbContext db, DbSet<TModel> table)
|
75
|
75
|
{
|
76
|
|
- return Table;
|
|
76
|
+ return table;
|
77
|
77
|
}
|
78
|
78
|
|
79
|
|
- protected virtual IQueryable<TModel> GetDeleteQueryable()
|
|
79
|
+ protected virtual IQueryable<TModel> GetDeleteQueryable(DbContext db, DbSet<TModel> table)
|
80
|
80
|
{
|
81
|
|
- return Table;
|
|
81
|
+ return table;
|
82
|
82
|
}
|
83
|
83
|
|
84
|
84
|
protected Func<TDboRead, T> GetIdFunc<T>()
|
|
@@ -97,9 +97,9 @@ namespace Luticate2.Utils.DataAccess
|
97
|
97
|
return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
|
98
|
98
|
}
|
99
|
99
|
|
100
|
|
- protected IQueryable<TModel> GetFilterQuery(LuFilterDbo filter)
|
|
100
|
+ protected IQueryable<TModel> GetFilterQuery(LuFilterDbo filter, DbContext db, DbSet<TModel> table)
|
101
|
101
|
{
|
102
|
|
- return GetGetQueryable().Where(GetFilterExpression(filter));
|
|
102
|
+ return GetGetQueryable(db, table).Where(GetFilterExpression(filter));
|
103
|
103
|
}
|
104
|
104
|
|
105
|
105
|
|
|
@@ -107,14 +107,14 @@ namespace Luticate2.Utils.DataAccess
|
107
|
107
|
|
108
|
108
|
public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
|
109
|
109
|
{
|
110
|
|
- return Execute(() =>
|
|
110
|
+ return Execute((db, table) =>
|
111
|
111
|
{
|
112
|
112
|
var models = objs.Select(GetModelFromTCreate).ToList();
|
113
|
|
- Table.AddRange(models);
|
114
|
|
- Db.SaveChanges();
|
|
113
|
+ table.AddRange(models);
|
|
114
|
+ db.SaveChanges();
|
115
|
115
|
foreach (var model in models)
|
116
|
116
|
{
|
117
|
|
- Db.Entry(model).State = EntityState.Detached;
|
|
117
|
+ db.Entry(model).State = EntityState.Detached;
|
118
|
118
|
}
|
119
|
119
|
var dbos = models.Select(GetDboFromModel).ToList();
|
120
|
120
|
var res = returnFunc(dbos);
|
|
@@ -155,9 +155,9 @@ namespace Luticate2.Utils.DataAccess
|
155
|
155
|
|
156
|
156
|
public LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
|
157
|
157
|
{
|
158
|
|
- return Execute(() =>
|
|
158
|
+ return Execute((db, table) =>
|
159
|
159
|
{
|
160
|
|
- var model = GetGetQueryable().AsNoTracking().FirstOrDefault(predicate);
|
|
160
|
+ var model = GetGetQueryable(db, table).AsNoTracking().FirstOrDefault(predicate);
|
161
|
161
|
if (model == default(TModel))
|
162
|
162
|
{
|
163
|
163
|
return GetNotFoundResult<TDboRead>();
|
|
@@ -187,9 +187,9 @@ namespace Luticate2.Utils.DataAccess
|
187
|
187
|
Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
|
188
|
188
|
params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
|
189
|
189
|
{
|
190
|
|
- return Execute(() =>
|
|
190
|
+ return Execute((db, table) =>
|
191
|
191
|
{
|
192
|
|
- var queryable = GetGetQueryable();
|
|
192
|
+ var queryable = GetGetQueryable(db, table);
|
193
|
193
|
var count = queryable.Count(predicate);
|
194
|
194
|
var ordered = orderBy(queryable);
|
195
|
195
|
foreach (var func in otherOrderBy)
|
|
@@ -227,11 +227,12 @@ namespace Luticate2.Utils.DataAccess
|
227
|
227
|
return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
|
228
|
228
|
}
|
229
|
229
|
|
230
|
|
- public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, IQueryable<TModel> queryable,
|
|
230
|
+ public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, Func<TDbContext, DbSet<TModel>, IQueryable<TModel>> getQueryable,
|
231
|
231
|
int page = 0, int perPage = int.MaxValue)
|
232
|
232
|
{
|
233
|
|
- return Execute(() =>
|
|
233
|
+ return Execute((db, table) =>
|
234
|
234
|
{
|
|
235
|
+ var queryable = getQueryable(db, table);
|
235
|
236
|
var count = queryable.Count();
|
236
|
237
|
IOrderedQueryable<TModel> ordered = null;
|
237
|
238
|
foreach (var field in orderBy.Fields)
|
|
@@ -265,12 +266,12 @@ namespace Luticate2.Utils.DataAccess
|
265
|
266
|
public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
|
266
|
267
|
int page = 0, int perPage = int.MaxValue)
|
267
|
268
|
{
|
268
|
|
- return GetMultiple(orderBy, GetFilterQuery(filter), page, perPage);
|
|
269
|
+ return GetMultiple(orderBy, (db, table) => GetFilterQuery(filter, db, table), page, perPage);
|
269
|
270
|
}
|
270
|
271
|
|
271
|
272
|
public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
|
272
|
273
|
{
|
273
|
|
- return GetMultiple(orderBy, GetGetQueryable(), page, perPage);
|
|
274
|
+ return GetMultiple(orderBy, GetGetQueryable, page, perPage);
|
274
|
275
|
}
|
275
|
276
|
|
276
|
277
|
|
|
@@ -278,17 +279,17 @@ namespace Luticate2.Utils.DataAccess
|
278
|
279
|
public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
|
279
|
280
|
Func<IEnumerable<TDboRead>, T> returnFunc)
|
280
|
281
|
{
|
281
|
|
- return Execute(() =>
|
|
282
|
+ return Execute((db, table) =>
|
282
|
283
|
{
|
283
|
|
- var models = GetEditQueryable().Where(predicate).AsNoTracking();
|
|
284
|
+ var models = GetEditQueryable(db, table).Where(predicate).AsNoTracking();
|
284
|
285
|
var editedDbos = new List<TDboRead>();
|
285
|
286
|
foreach (var model in models)
|
286
|
287
|
{
|
287
|
288
|
update(model);
|
288
|
289
|
editedDbos.Add(GetDboFromModel(model));
|
289
|
|
- Db.Entry(model).State = EntityState.Modified;
|
|
290
|
+ db.Entry(model).State = EntityState.Modified;
|
290
|
291
|
}
|
291
|
|
- Db.SaveChanges();
|
|
292
|
+ db.SaveChanges();
|
292
|
293
|
var res = returnFunc(editedDbos);
|
293
|
294
|
return LuResult<T>.Ok(res);
|
294
|
295
|
});
|
|
@@ -355,11 +356,11 @@ namespace Luticate2.Utils.DataAccess
|
355
|
356
|
public LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
|
356
|
357
|
Func<IEnumerable<TDboRead>, T> returnFunc)
|
357
|
358
|
{
|
358
|
|
- return Execute(() =>
|
|
359
|
+ return Execute((db, table) =>
|
359
|
360
|
{
|
360
|
|
- var models = GetDeleteQueryable().Where(predicate).AsNoTracking().ToList();
|
361
|
|
- Table.RemoveRange(models);
|
362
|
|
- Db.SaveChanges();
|
|
361
|
+ var models = GetDeleteQueryable(db, table).Where(predicate).AsNoTracking().ToList();
|
|
362
|
+ table.RemoveRange(models);
|
|
363
|
+ db.SaveChanges();
|
363
|
364
|
var dbos = models.Select(GetDboFromModel);
|
364
|
365
|
return LuResult<T>.Ok(returnFunc(dbos));
|
365
|
366
|
});
|