Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LuCrudBusiness.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using Luticate2.Utils.Dbo.Basic;
  4. using Luticate2.Utils.Dbo.PaginatedRequest;
  5. using Luticate2.Utils.Dbo.Result;
  6. using Luticate2.Utils.Interfaces;
  7. using Luticate2.Utils.Utils;
  8. namespace Luticate2.Utils.Business
  9. {
  10. public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate, TId> :
  11. LuBusiness,
  12. ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
  13. where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
  14. where TDboCreate : class
  15. where TDboRead : class
  16. where TDboUpdate : class
  17. {
  18. protected readonly TDataAccess DataAccess;
  19. protected readonly IServiceProvider ServiceProvider;
  20. private readonly IList<Type> _listeners = new List<Type>();
  21. protected LuCrudBusiness(TDataAccess dataAccess, IServiceProvider serviceProvider)
  22. {
  23. DataAccess = dataAccess;
  24. ServiceProvider = serviceProvider;
  25. }
  26. public void AddListener<T>()
  27. where T : ILuCrudNotifications
  28. {
  29. _listeners.Add(typeof(T));
  30. }
  31. protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
  32. {
  33. return LuResult<TDboCreate>.Ok(obj);
  34. }
  35. protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
  36. {
  37. return LuResult<TDboUpdate>.Ok(update);
  38. }
  39. protected virtual LuResult<TDboUpdate> GetAndCheckEdit(TId id, TDboUpdate update)
  40. {
  41. var res = GetSingleById(id);
  42. if (!res)
  43. {
  44. return res.To<TDboUpdate>();
  45. }
  46. return CheckEdit(res.Data, update);
  47. }
  48. public TId GetDboId(TDboRead obj)
  49. {
  50. return DataAccess.GetDboId(obj);
  51. }
  52. public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
  53. {
  54. var res = CheckAdd(obj);
  55. if (!res)
  56. {
  57. return res.To<T>();
  58. }
  59. var newEntity = DataAccess.AddDbo(res.Data);
  60. if (newEntity)
  61. {
  62. NotifyCreate(newEntity.Data);
  63. return LuResult<T>.Ok(returnFunc(newEntity.Data));
  64. }
  65. return newEntity.To<T>();
  66. }
  67. public virtual LuResult<TDboRead> GetSingleById(TId id)
  68. {
  69. return DataAccess.GetSingleById(id);
  70. }
  71. public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuPaginatedRequestDbo request)
  72. {
  73. return DataAccess.GetMultiple(request);
  74. }
  75. public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
  76. {
  77. var originalEntity = GetSingleById(id);
  78. if (!originalEntity)
  79. {
  80. return originalEntity.To<T>();
  81. }
  82. var obj = CheckEdit(originalEntity.Data, update);
  83. if (!obj)
  84. {
  85. return obj.To<T>();
  86. }
  87. var editedEntity = DataAccess.EditSingleByIdDbo(id, obj.Data);
  88. if (editedEntity)
  89. {
  90. NotifyUpdate(originalEntity.Data, editedEntity.Data);
  91. return LuResult<T>.Ok(returnFunc(editedEntity.Data));
  92. }
  93. return editedEntity.To<T>();
  94. }
  95. public virtual LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
  96. {
  97. var originalEntity = GetSingleById(id);
  98. if (!originalEntity)
  99. {
  100. return originalEntity.To<T>();
  101. }
  102. var res = DataAccess.DeleteSingleById(id, returnFunc);
  103. if (res)
  104. {
  105. NotifyDelete(originalEntity.Data);
  106. }
  107. return res;
  108. }
  109. private void ForeachListener(Action<ILuCrudNotifications> action)
  110. {
  111. foreach (var listener in _listeners)
  112. {
  113. var instance = ServiceProvider.GetService(listener) as ILuCrudNotifications;
  114. action(instance);
  115. }
  116. }
  117. private void NotifyCreate(object newEntity)
  118. {
  119. ForeachListener(notifications => notifications.NotifyCreate(GetType(), newEntity));
  120. }
  121. private void NotifyUpdate(object oldEntity, object newEntity)
  122. {
  123. ForeachListener(notifications => notifications.NotifyUpdate(GetType(), oldEntity, newEntity));
  124. }
  125. private void NotifyDelete( object oldEntity)
  126. {
  127. ForeachListener(notifications => notifications.NotifyDelete(GetType(), oldEntity));
  128. }
  129. }
  130. }