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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Luticate2.Utils.Dbo;
  6. using Microsoft.EntityFrameworkCore;
  7. namespace Luticate2.Utils.DataAccess
  8. {
  9. public abstract class LuEfCrudDataAccess<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext> : LuEfDataAccess<TModel, TDbContext>
  10. where TModel : class
  11. where TDboCreate : class
  12. where TDboRead : class
  13. where TDboUpdate : class
  14. where TDbContext : DbContext
  15. {
  16. protected LuEfCrudDataAccess(TDbContext db, DbSet<TModel> table) : base(db, table)
  17. {
  18. }
  19. protected abstract TModel GetModelFromTCreate(TDboCreate obj);
  20. protected abstract TModel GetModelFromTUpdate(TDboUpdate obj);
  21. protected abstract TDboRead GetDboFromModel(TModel model);
  22. public LuResult<T> Add<T>(TDboCreate obj, Expression<Func<TModel, T>> returnFunc)
  23. {
  24. return Execute(() =>
  25. {
  26. var model = GetModelFromTCreate(obj);
  27. Table.Add(model);
  28. Db.SaveChanges();
  29. var res = returnFunc.Compile().DynamicInvoke(model);
  30. return LuResult<T>.Ok((T) res);
  31. });
  32. }
  33. public LuResult<Guid> AddGuid(TDboCreate obj)
  34. {
  35. var param = Expression.Parameter(typeof(TModel), "x");
  36. var exp = Expression.Property(param, "id");
  37. var lambda = Expression.Lambda<Func<TModel, Guid>>(exp, param);
  38. return Add(obj, lambda);
  39. }
  40. public LuResult<string> AddGuidStr(TDboCreate obj)
  41. {
  42. var res = AddGuid(obj);
  43. if (!res)
  44. {
  45. return res.To<string>();
  46. }
  47. return LuResult<string>.Ok(res.Data.ToString());
  48. }
  49. public LuResult<long> AddId(TDboCreate obj)
  50. {
  51. var param = Expression.Parameter(typeof(TModel), "x");
  52. var exp = Expression.Property(param, "id");
  53. var lambda = Expression.Lambda<Func<TModel, long>>(exp, param);
  54. return Add(obj, lambda);
  55. }
  56. public LuResult<TDboRead> AddDbo(TDboCreate obj)
  57. {
  58. return Add(obj, model => GetDboFromModel(model));
  59. }
  60. public LuResult<TDboRead> GetSingle(Expression<Func<TModel, bool>> predicate)
  61. {
  62. return Execute(() =>
  63. {
  64. var model = Table.FirstOrDefault(predicate);
  65. if (model == null)
  66. {
  67. return LuResult<TDboRead>.Error(LuStatus.NotFound, typeof(TModel).Name + ": Value not found", "");
  68. }
  69. var dbo = GetDboFromModel(model);
  70. return LuResult<TDboRead>.Ok(dbo);
  71. });
  72. }
  73. public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
  74. {
  75. return GetSingle(GetExpression(keys));
  76. }
  77. public LuResult<TDboRead> GetSingleByGuid(Guid id)
  78. {
  79. return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
  80. }
  81. public LuResult<TDboRead> GetSingleByGuidStr(string id)
  82. {
  83. return GetSingleByGuid(new Guid(id));
  84. }
  85. public LuResult<TDboRead> GetSingleById(long id)
  86. {
  87. return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
  88. }
  89. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Func<IQueryable<TModel>> orderBy,
  90. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue,
  91. params Expression<Func<TModel, TKey>>[] otherOrderBy)
  92. {
  93. return Execute(() =>
  94. {
  95. var count = Table.Count(predicate);
  96. var ordered = orderBy();
  97. var data = ordered.Where(predicate).Skip(page * perPage).Take(perPage).Select(GetDboFromModel).ToList();
  98. var result = new LuPaginatedDbo<TDboRead>
  99. {
  100. Count = count,
  101. Data = data
  102. };
  103. return LuResult<LuPaginatedDbo<TDboRead>>.Ok(result);
  104. });
  105. }
  106. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Expression<Func<TModel, TKey>> orderBy,
  107. Expression<Func<TModel, bool>> predicate, int page = 0, int perPage = int.MaxValue)
  108. {
  109. return GetMultiple<TKey>(() => Table.OrderBy(orderBy), predicate, page, perPage);
  110. }
  111. public LuResult<LuPaginatedDbo<TDboRead>> GetMultiple<TKey>(Expression<Func<TModel, TKey>> orderBy,
  112. int page = 0, int perPage = int.MaxValue)
  113. {
  114. return GetMultiple(orderBy, x => true, page, perPage);
  115. }
  116. }
  117. }