Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LuEfCrudDataAccess.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. return LuResult<TDboRead>.Ok(GetDboFromModel(model));
  70. });
  71. }
  72. public LuResult<TDboRead> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
  73. {
  74. return GetSingle(GetExpression(keys));
  75. }
  76. public LuResult<TDboRead> GetSingleByGuid(Guid id)
  77. {
  78. return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
  79. }
  80. public LuResult<TDboRead> GetSingleByGuidStr(string id)
  81. {
  82. return GetSingleByGuid(new Guid(id));
  83. }
  84. public LuResult<TDboRead> GetSingleById(long id)
  85. {
  86. return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
  87. }
  88. }
  89. }