您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuEfCrudDataAccess.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 IQueryable<TModel> GetGetQueryable(TDbContext db, DbSet<TModel> table)
  61. {
  62. return table;
  63. }
  64. protected virtual IQueryable<TModel> GetEditQueryable(TDbContext db, DbSet<TModel> table)
  65. {
  66. return table;
  67. }
  68. protected virtual IQueryable<TModel> GetDeleteQueryable(TDbContext db, DbSet<TModel> table)
  69. {
  70. return table;
  71. }
  72. protected Func<TDboRead, T> GetIdFunc<T>()
  73. {
  74. return x => ((dynamic) x).Id;
  75. }
  76. protected TModel GetModelFromTUpdate(TDboUpdate obj, TModel model)
  77. {
  78. EditModelFromTUpdate(obj, model);
  79. return model;
  80. }
  81. protected LuResult<T> GetNotFoundResult<T>()
  82. {
  83. return LuResult<T>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
  84. }
  85. protected IQueryable<TModel> GetFilterQuery(LuFilterDbo filter, TDbContext db, DbSet<TModel> table)
  86. {
  87. return GetGetQueryable(db, table).Where(GetFilterExpression(filter));
  88. }
  89. public LuResult<T> Add<T>(IEnumerable<TDboCreate> objs, Func<IEnumerable<TDboRead>, T> returnFunc)
  90. {
  91. return Execute((db, table) =>
  92. {
  93. var models = objs.Select(GetModelFromTCreate).ToList();
  94. table.AddRange(models);
  95. db.SaveChanges();
  96. foreach (var model in models)
  97. {
  98. db.Entry(model).State = EntityState.Detached;
  99. }
  100. var dbos = models.Select(GetDboFromModel).ToList();
  101. var res = returnFunc(dbos);
  102. return LuResult<T>.Ok(res);
  103. });
  104. }
  105. public LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  106. {
  107. return Add(new List<TDboCreate> {obj}, list => returnFunc(list.First()));
  108. }
  109. public LuResult<IEnumerable<TId>> AddId(IEnumerable<TDboCreate> objs)
  110. {
  111. var func = GetIdFunc<TId>();
  112. return Add(objs, list => list.Select(func));
  113. }
  114. public LuResult<TId> AddId(TDboCreate obj)
  115. {
  116. return AddId(new List<TDboCreate> {obj}).To(list => list.First());
  117. }
  118. public LuResult<IEnumerable<TDboRead>> AddDbo(IEnumerable<TDboCreate> obj)
  119. {
  120. return Add(obj, read => read);
  121. }
  122. public LuResult<TDboRead> AddDbo(TDboCreate obj)
  123. {
  124. return AddDbo(new List<TDboCreate> {obj}).To(list => list.First());
  125. }
  126. public LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
  127. {
  128. return Execute((db, table) =>
  129. {
  130. var model = GetGetQueryable(db, table).AsNoTracking().FirstOrDefault(predicate);
  131. if (model == default(TModel))
  132. {
  133. return GetNotFoundResult<TDboRead>();
  134. }
  135. var dbo = GetDboFromModel(model);
  136. return LuResult<TDboRead>.Ok(dbo);
  137. });
  138. }
  139. public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
  140. {
  141. return GetSingle(GetExpression(keys));
  142. }
  143. public LuResult<TDboRead> GetSingleById(TId id)
  144. {
  145. var guid = GetId(id);
  146. if (guid == null)
  147. {
  148. return GetNotFoundResult<TDboRead>();
  149. }
  150. return GetSingleByKeys(new KeyValuePair<string, object>("id", guid));
  151. }
  152. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
  153. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
  154. params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
  155. {
  156. return Execute((db, table) =>
  157. {
  158. var queryable = GetGetQueryable(db, table);
  159. var count = queryable.Count(predicate);
  160. var ordered = orderBy(queryable);
  161. foreach (var func in otherOrderBy)
  162. {
  163. ordered = func(ordered);
  164. }
  165. var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).AsNoTracking().Select(GetDboFromModel).ToList();
  166. var result = new LuPaginatedDbo<TDboRead>
  167. {
  168. Count = count,
  169. Data = data
  170. };
  171. return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
  172. });
  173. }
  174. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
  175. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
  176. params Expression<Func<TModel, object>>[] otherOrderBy)
  177. {
  178. return GetMultiple(table => table.OrderBy(orderBy), predicate, page, perPage,
  179. otherOrderBy.Select<Expression<Func<TModel, object>>, Func<IOrderedQueryable<TModel>,
  180. IOrderedQueryable<TModel>>>(expression => (t => t.ThenBy(expression))).ToArray());
  181. }
  182. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Func<IQueryable<TModel>, IOrderedQueryable<TModel>> orderBy,
  183. int page = 0, int perPage = int.MaxValue, params Func<IOrderedQueryable<TModel>, IOrderedQueryable<TModel>>[] otherOrderBy)
  184. {
  185. return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
  186. }
  187. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(Expression<Func<TModel, object>> orderBy,
  188. int page = 0, int perPage = int.MaxValue, params Expression<Func<TModel, object>>[] otherOrderBy)
  189. {
  190. return GetMultiple(orderBy, x => true, page, perPage, otherOrderBy);
  191. }
  192. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, Func<TDbContext, DbSet<TModel>, IQueryable<TModel>> getQueryable,
  193. int page = 0, int perPage = int.MaxValue)
  194. {
  195. return Execute((db, table) =>
  196. {
  197. var queryable = getQueryable(db, table);
  198. var count = queryable.Count();
  199. IOrderedQueryable<TModel> ordered = null;
  200. foreach (var field in orderBy.Fields)
  201. {
  202. var exp = GetOrderByFieldExpression(field.Name);
  203. if (exp == null)
  204. {
  205. return LuResult<LuPaginatedDbo<TDboRead>>.Error(LuStatus.InputError,
  206. string.Format("LuEfCrudDataAccess: {0}", field.Name), "Invalid order by field");
  207. }
  208. if (ordered != null)
  209. {
  210. ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);
  211. }
  212. else
  213. {
  214. ordered = field.Asc ? queryable.OrderBy(exp) : queryable.OrderByDescending(exp);
  215. }
  216. }
  217. var data = ordered.Skip(page * perPage).Take(perPage).AsNoTracking().Select(GetDboFromModel).ToList();
  218. var result = new LuPaginatedDbo<TDboRead>
  219. {
  220. Count = count,
  221. Data = data
  222. };
  223. return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
  224. });
  225. }
  226. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, LuFilterDbo filter,
  227. int page = 0, int perPage = int.MaxValue)
  228. {
  229. return GetMultiple(orderBy, (db, table) => GetFilterQuery(filter, db, table), page, perPage);
  230. }
  231. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuOrderByDbo orderBy, int page = 0, int perPage = int.MaxValue)
  232. {
  233. return GetMultiple(orderBy, GetGetQueryable, page, perPage);
  234. }
  235. public LuResult<T> Edit<T>(Expression<Func<TModel, bool>> predicate, Action<TModel> update,
  236. Func<IEnumerable<TDboRead>, T> returnFunc)
  237. {
  238. return Execute((db, table) =>
  239. {
  240. var models = GetEditQueryable(db, table).Where(predicate).AsNoTracking().ToList();
  241. foreach (var model in models)
  242. {
  243. update(model);
  244. db.Entry(model).State = EntityState.Modified;
  245. }
  246. db.SaveChanges();
  247. var editedDbos = models.Select(GetDboFromModel).ToList();
  248. var res = returnFunc(editedDbos);
  249. return LuResult<T>.Ok(res);
  250. });
  251. }
  252. public LuResult<IEnumerable<TId>> EditId(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  253. {
  254. var func = GetIdFunc<TId>();
  255. return Edit(predicate, update, reads => reads.Select(func));
  256. }
  257. public LuResult<IEnumerable<TDboRead>> EditDbo(Expression<Func<TModel, bool>> predicate, Action<TModel> update)
  258. {
  259. return Edit(predicate, update, read => read);
  260. }
  261. public LuResult<T> EditSingleById<T>(TId id, Action<TModel> update, Func<TDboRead, T> returnFunc)
  262. {
  263. var guid = GetId(id);
  264. if (guid == null)
  265. {
  266. return GetNotFoundResult<T>();
  267. }
  268. return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)), update,
  269. list => returnFunc(list.FirstOrDefault()));
  270. }
  271. public LuResult<TId> EditSingleByIdId(TId id, Action<TModel> update)
  272. {
  273. return EditSingleById(id, update, GetIdFunc<TId>());
  274. }
  275. public LuResult<TDboRead> EditSingleByIdDbo(TId id, Action<TModel> update)
  276. {
  277. return EditSingleById(id, update, read => read);
  278. }
  279. public LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  280. {
  281. var guid = GetId(id);
  282. if (guid == null)
  283. {
  284. return GetNotFoundResult<T>();
  285. }
  286. return Edit(GetExpression(new KeyValuePair<string, object>("id", guid)),
  287. model => EditModelFromTUpdate(update, model), list => returnFunc(list.FirstOrDefault()));
  288. }
  289. public LuResult<TId> EditSingleByIdId(TId id, TDboUpdate update)
  290. {
  291. return EditSingleById(id, update, GetIdFunc<TId>());
  292. }
  293. public LuResult<TDboRead> EditSingleByIdDbo(TId id, TDboUpdate update)
  294. {
  295. return EditSingleById(id, update, read => read);
  296. }
  297. public LuResult<T> Delete<T>(Expression<Func<TModel, bool>> predicate,
  298. Func<IEnumerable<TDboRead>, T> returnFunc)
  299. {
  300. return Execute((db, table) =>
  301. {
  302. var models = GetDeleteQueryable(db, table).Where(predicate).AsNoTracking().ToList();
  303. table.RemoveRange(models);
  304. db.SaveChanges();
  305. var dbos = models.Select(GetDboFromModel);
  306. return LuResult<T>.Ok(returnFunc(dbos));
  307. });
  308. }
  309. public LuResult<IEnumerable<TId>> DeleteId(Expression<Func<TModel, bool>> predicate)
  310. {
  311. var func = GetIdFunc<TId>();
  312. return Delete(predicate, reads => reads.Select(func));
  313. }
  314. public LuResult<IEnumerable<TDboRead>> DeleteDbo(Expression<Func<TModel, bool>> predicate)
  315. {
  316. return Delete(predicate, read => read);
  317. }
  318. public LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
  319. {
  320. var guid = GetId(id);
  321. if (guid == null)
  322. {
  323. return GetNotFoundResult<T>();
  324. }
  325. return Delete(GetExpression(new KeyValuePair<string, object>("id", guid)),
  326. reads => returnFunc(reads.FirstOrDefault()));
  327. }
  328. public LuResult<TId> DeleteSingleByIdId(TId id)
  329. {
  330. var func = GetIdFunc<TId>();
  331. return DeleteSingleById(id, func);
  332. }
  333. public LuResult<TDboRead> DeleteSingleByIdDbo(TId id)
  334. {
  335. return DeleteSingleById(id, read => read);
  336. }
  337. }
  338. }