You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuEfCrudDataAccess.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Luticate2.Utils.Dbo.Basic;
  6. using Luticate2.Utils.Dbo.Filter;
  7. using Luticate2.Utils.Dbo.OrderBy;
  8. using Luticate2.Utils.Dbo.Result;
  9. using Luticate2.Utils.Interfaces;
  10. using Luticate2.Utils.Utils;
  11. using Microsoft.EntityFrameworkCore;
  12. namespace Luticate2.Utils.DataAccess
  13. {
  14. public abstract class LuEfCrudDataAccess<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext, TId> :
  15. LuEfDataAccess<TModel, TDbContext>,
  16. ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
  17. where TModel : class
  18. where TDboCreate : class
  19. where TDboRead : class
  20. where TDboUpdate : class
  21. where TDbContext : DbContext
  22. {
  23. protected LuEfCrudDataAccess(IServiceProvider serviceProvider) : base(serviceProvider)
  24. {
  25. }
  26. protected abstract TModel GetModelFromTCreate(TDboCreate obj);
  27. protected abstract void EditModelFromTUpdate(TDboUpdate obj, TModel model);
  28. protected abstract TDboRead GetDboFromModel(TModel model);
  29. protected virtual Expression<Func<TModel, object>> GetOrderByFieldExpression(string fieldName)
  30. {
  31. fieldName = fieldName.ToSnakeCase();
  32. if (!typeof(TModel).HasProperty(fieldName))
  33. {
  34. return null;
  35. }
  36. var param = Expression.Parameter(typeof(TModel), "x");
  37. var prop = Expression.Property(param, fieldName);
  38. var converted = Expression.Convert(prop, typeof(object));
  39. var exp = Expression.Lambda<Func<TModel, object>>(converted, param);
  40. return exp;
  41. }
  42. protected virtual Expression<Func<TModel, bool>> GetFilterExpression(LuFilterDbo filter)
  43. {
  44. return model => true;
  45. }
  46. protected virtual object GetId(TId id)
  47. {
  48. var s = id as string;
  49. if (s != null)
  50. {
  51. Guid guid;
  52. if (Guid.TryParse(s, out guid))
  53. {
  54. return guid;
  55. }
  56. return null;
  57. }
  58. return id;
  59. }
  60. protected virtual object GetModelId(object id)
  61. {
  62. var g = id as Guid?;
  63. if (g != null)
  64. {
  65. return g.ToString();
  66. }
  67. return id;
  68. }
  69. protected virtual IQueryable<TModel> GetGetQueryable(TDbContext db, DbSet<TModel> table)
  70. {
  71. return table;
  72. }
  73. protected virtual IQueryable<TModel> GetEditQueryable(TDbContext db, DbSet<TModel> table)
  74. {
  75. return table;
  76. }
  77. protected virtual IQueryable<TModel> GetDeleteQueryable(TDbContext db, DbSet<TModel> table)
  78. {
  79. return table;
  80. }
  81. protected virtual Func<TDboRead, T> GetIdFunc<T>()
  82. {
  83. return x => ((dynamic) x).Id;
  84. }
  85. protected virtual TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
  86. {
  87. EditModelFromTUpdate(obj, model);
  88. return model;
  89. }
  90. protected virtual LuResult<T> GetNotFoundResult<T>()
  91. {
  92. return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
  93. }
  94. protected virtual IQueryable<TModel> GetFilterQuery(LuFilterDbo filter, TDbContext db, DbSet<TModel> table)
  95. {
  96. return GetGetQueryable(db, table).Where(GetFilterExpression(filter));
  97. }
  98. public virtual LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
  99. {
  100. var models = objs.Select(GetModelFromTCreate).ToList();
  101. var addRes = Execute((db, table) =>
  102. {
  103. table.AddRange(models);
  104. db.SaveChanges();
  105. foreach (var model in models)
  106. {
  107. db.Entry(model).State = EntityState.Detached;
  108. }
  109. return LuResult<T>.Ok(default(T));
  110. });
  111. if (!addRes)
  112. {
  113. return addRes;
  114. }
  115. return GetMultiple(models, returnFunc);
  116. }
  117. public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  118. {
  119. return Add(new List<TDboCreate> {obj}, list => returnFunc(list.First()));
  120. }
  121. public virtual LuResult<IEnumerable<TId>> AddId(IEnumerable<TDboCreate> objs)
  122. {
  123. var func = GetIdFunc<TId>();
  124. return Add(objs, list => list.Select(func));
  125. }
  126. public virtual LuResult<TId> AddId(TDboCreate obj)
  127. {
  128. return AddId(new List<TDboCreate> {obj}).To(list => list.First());
  129. }
  130. public virtual LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
  131. {
  132. return Add(obj, read => read);
  133. }
  134. public virtual LuResult<TDboRead> AddDbo(TDboCreate obj)
  135. {
  136. return AddDbo(new List<TDboCreate> {obj}).To(list => list.First());
  137. }
  138. public virtual LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
  139. {
  140. return Execute((db, table) =>
  141. {
  142. var model = GetGetQueryable(db, table).AsNoTracking().FirstOrDefault(predicate);
  143. if (model == default(TModel))
  144. {
  145. return GetNotFoundResult<TDboRead>();
  146. }
  147. var dbo = GetDboFromModel(model);
  148. return LuResult<TDboRead>.Ok(dbo);
  149. });
  150. }
  151. public virtual LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
  152. {
  153. return GetSingle(GetExpression(keys));
  154. }
  155. public virtual LuResult<TDboRead> GetSingleById(TId id)
  156. {
  157. var guid = GetId(id);
  158. if (guid == null)
  159. {
  160. return GetNotFoundResult<TDboRead>();
  161. }
  162. return GetSingleByKeys(new KeyValuePair<string, object>("id", guid));
  163. }
  164. protected virtual LuResult<T> GetMultiple<T>(IList<TModel> models, Func<IEnumerable<TDboRead>, T> returnFunc)
  165. {
  166. var dbos = new List<TDboRead>();
  167. foreach (var model in models)
  168. {
  169. var getRes = GetSingleById((TId) GetModelId(((dynamic)model).id));
  170. if (!getRes)
  171. {
  172. return getRes.To<T>();
  173. }
  174. dbos.Add(getRes.Data);
  175. }
  176. var res = returnFunc(dbos);
  177. return LuResult<T>.Ok(res);
  178. }
  179. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
  180. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
  181. params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
  182. {
  183. return Execute((db, table) =>
  184. {
  185. var queryable = GetGetQueryable(db, table);
  186. var count = queryable.Count(predicate);
  187. var ordered = orderBy(queryable);
  188. foreach (var func in otherOrderBy)
  189. {
  190. ordered = func(ordered);
  191. }
  192. var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).AsNoTracking().Select(GetDboFromModel).ToList();
  193. var result = new LuPaginatedDbo<TDboRead>
  194. {
  195. Count = count,
  196. Data = data
  197. };
  198. return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
  199. });
  200. }
  201. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
  202. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
  203. params Expression<Func<TModel, object>>[] otherOrderBy)
  204. {
  205. return GetMultiple(table => table.OrderBy(orderBy), predicate, page, perPage,
  206. otherOrderBy.Select<Expression<Func<TModel, object>>, Func<IOrderedQueryable<TModel>,
  207. IOrderedQueryable<TModel>>>(expression => (t => t.ThenBy(expression))).ToArray());
  208. }
  209. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
  210. int page = 0, int perPage = int.MaxValue, params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
  211. {
  212. return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
  213. }
  214. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
  215. int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, object>>[] otherOrderBy)
  216. {
  217. return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
  218. }
  219. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, Func<TDbContext, DbSet<TModel>, IQueryable<TModel>> getQueryable,
  220. int page = 0, int perPage = int.MaxValue)
  221. {
  222. return Execute((db, table) =>
  223. {
  224. var queryable = getQueryable(db, table);
  225. var count = queryable.Count();
  226. IOrderedQueryable<TModel> ordered = null;
  227. foreach (var field in orderBy.Fields)
  228. {
  229. var exp = GetOrderByFieldExpression(field.Name);
  230. if (exp == null)
  231. {
  232. return LuResult<LuPaginatedDbo<TDboRead>>.Error(LuStatus.InputError,
  233. string.Format("LuEfCrudDataAccess: {0}", field.Name), "Invalid order by field");
  234. }
  235. if (ordered != null)
  236. {
  237. ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);
  238. }
  239. else
  240. {
  241. ordered = field.Asc ? queryable.OrderBy(exp) : queryable.OrderByDescending(exp);
  242. }
  243. }
  244. var data = ordered.Skip(page * perPage).Take(perPage).AsNoTracking().Select(GetDboFromModel).ToList();
  245. var result = new LuPaginatedDbo<TDboRead>
  246. {
  247. Count = count,
  248. Data = data
  249. };
  250. return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
  251. });
  252. }
  253. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
  254. int page = 0, int perPage = int.MaxValue)
  255. {
  256. return GetMultiple(orderBy, (db, table) => GetFilterQuery(filter, db, table), page, perPage);
  257. }
  258. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
  259. {
  260. return GetMultiple(orderBy, GetGetQueryable, page, perPage);
  261. }
  262. public virtual LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
  263. Func<IEnumerable<TDboRead>, T> returnFunc)
  264. {
  265. IList<TModel> models = null;
  266. var editRes = Execute((db, table) =>
  267. {
  268. models = GetEditQueryable(db, table).Where(predicate).AsNoTracking().ToList();
  269. foreach (var model in models)
  270. {
  271. update(model);
  272. db.Entry(model).State = EntityState.Modified;
  273. }
  274. db.SaveChanges();
  275. return LuResult<T>.Ok(default(T));
  276. });
  277. if (!editRes)
  278. {
  279. return editRes;
  280. }
  281. return GetMultiple(models, returnFunc);
  282. }
  283. public virtual LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  284. {
  285. var func = GetIdFunc<TId>();
  286. return Edit(predicate, update, reads => reads.Select(func));
  287. }
  288. public virtual LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  289. {
  290. return Edit(predicate, update, read => read);
  291. }
  292. public virtual LuResult<T> EditSingleById<T>(TId id, Action<TModel> update, Func<TDboRead, T> returnFunc)
  293. {
  294. var guid = GetId(id);
  295. if (guid == null)
  296. {
  297. return GetNotFoundResult<T>();
  298. }
  299. return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)), update,
  300. list => returnFunc(list.FirstOrDefault()));
  301. }
  302. public virtual LuResult<TId> EditSingleByIdId(TId id, Action<TModel> update)
  303. {
  304. return EditSingleById(id, update, GetIdFunc<TId>());
  305. }
  306. public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, Action<TModel> update)
  307. {
  308. return EditSingleById(id, update, read => read);
  309. }
  310. public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  311. {
  312. var guid = GetId(id);
  313. if (guid == null)
  314. {
  315. return GetNotFoundResult<T>();
  316. }
  317. return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)),
  318. model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
  319. }
  320. public virtual LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
  321. {
  322. return EditSingleById(id, update, GetIdFunc<TId>());
  323. }
  324. public virtual LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
  325. {
  326. return EditSingleById(id, update, read => read);
  327. }
  328. public virtual LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
  329. Func<IEnumerable<TDboRead>, T> returnFunc)
  330. {
  331. return Execute((db, table) =>
  332. {
  333. var models = GetDeleteQueryable(db, table).Where(predicate).AsNoTracking().ToList();
  334. var getRes = GetMultiple(models, returnFunc);
  335. if (!getRes)
  336. {
  337. return getRes;
  338. }
  339. table.RemoveRange(models);
  340. db.SaveChanges();
  341. return getRes;
  342. });
  343. }
  344. public virtual LuResult<IEnumerable<TId>> DeleteId(Expression<Func<TModel, bool>> predicate)
  345. {
  346. var func = GetIdFunc<TId>();
  347. return Delete(predicate, reads => reads.Select(func));
  348. }
  349. public virtual LuResult<IEnumerable<TDboRead>> DeleteDbo(Expression<Func<TModel, bool>> predicate)
  350. {
  351. return Delete(predicate, read => read);
  352. }
  353. public virtual LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
  354. {
  355. var guid = GetId(id);
  356. if (guid == null)
  357. {
  358. return GetNotFoundResult<T>();
  359. }
  360. return Delete(GetExpression(new KeyValuePair<string, object>("id", guid)),
  361. reads => returnFunc(reads.FirstOrDefault()));
  362. }
  363. public virtual LuResult<TId> DeleteSingleByIdId(TId id)
  364. {
  365. var func = GetIdFunc<TId>();
  366. return DeleteSingleById(id, func);
  367. }
  368. public virtual LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
  369. {
  370. return DeleteSingleById(id, read => read);
  371. }
  372. }
  373. }