Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SqlServerManager.cs 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using iiie.Logs.DataAccess;
  7. using iiie.Logs.DBO;
  8. namespace iiie.WebApiUtils.BusinessManager
  9. {
  10. /// <summary>
  11. /// Helper for SQL Server data access
  12. /// </summary>
  13. public abstract class SqlServerManager<TDbObject, TDboGet, TDboAdd, TDboEdit, TEntities, TThis>
  14. : SqlServerBasicManager<TDbObject, TEntities, TThis>
  15. where TDbObject : class
  16. where TEntities : DbContext, new()
  17. where TThis : SqlServerManager<TDbObject, TDboGet, TDboAdd, TDboEdit, TEntities, TThis>, new()
  18. {
  19. /// <summary>
  20. /// Convert a DB object to DBO
  21. /// </summary>
  22. /// <param name="obj">The object to be converted</param>
  23. /// <returns>The DBO</returns>
  24. public abstract TDboGet DbToDboGet(TDbObject obj);
  25. /// <summary>
  26. /// Convert a DBO to DB object
  27. /// </summary>
  28. /// <param name="obj">The object to be converted</param>
  29. /// <returns>The DB object</returns>
  30. public abstract TDbObject DboAddToDb(TDboAdd obj);
  31. /// <summary>
  32. /// Convert a DBO to DB object
  33. /// </summary>
  34. /// <param name="obj">The object to be converted</param>
  35. /// <param name="edit">The object beeing edited</param>
  36. /// <returns>The DB object</returns>
  37. public abstract TDbObject DboEditToDb(TDboEdit obj, TDbObject edit);
  38. /// <summary>
  39. /// Helper to convert DB object to DBO
  40. /// </summary>
  41. /// <param name="res">The result to convert</param>
  42. /// <returns>The DBO object</returns>
  43. public static TDboGet DbToDboGetStatic(TDbObject res)
  44. {
  45. var obj = new TThis();
  46. return obj.DbToDboGet(res);
  47. }
  48. /// <summary>
  49. /// Helper to convert DBO object to DB
  50. /// </summary>
  51. /// <param name="res">The result to convert</param>
  52. /// <returns>The DB object</returns>
  53. public static TDbObject DboAddToDbStatic(TDboAdd res)
  54. {
  55. var obj = new TThis();
  56. return obj.DboAddToDb(res);
  57. }
  58. /// <summary>
  59. /// Helper to convert DBO object to DB
  60. /// </summary>
  61. /// <param name="res">The result to convert</param>
  62. /// <param name="edit">The object beeing edited</param>
  63. /// <returns>The DB object</returns>
  64. public static TDbObject DboEditToDbStatic(TDboEdit res, TDbObject edit)
  65. {
  66. var obj = new TThis();
  67. return obj.DboEditToDb(res, edit);
  68. }
  69. /// <summary>
  70. /// Get a single DB object matching the predicate
  71. /// </summary>
  72. /// <param name="table">The table to search in</param>
  73. /// <param name="predicate">The predicate</param>
  74. /// <returns>The object that match</returns>
  75. public static OpResult<TDbObject> GetSingleDbObject(DbSet<TDbObject> table, Expression<Func<TDbObject, bool>> predicate)
  76. {
  77. var o = table.FirstOrDefault(predicate);
  78. if (o == null)
  79. {
  80. return OpResult<TDbObject>.Error(ResultStatus.NotFound, typeof(TThis).Name + ": Value not found", "");
  81. }
  82. return OpResult<TDbObject>.Ok(o);
  83. }
  84. /// <summary>
  85. /// Get a single DBO matching the predicate
  86. /// </summary>
  87. /// <param name="predicate">The predicate</param>
  88. /// <returns>The object that match, or null</returns>
  89. public static OpResult<TDboGet> GetSingle(Expression<Func<TDbObject, bool>> predicate)
  90. {
  91. return Execute((db, table) =>
  92. {
  93. var obj = GetSingleDbObject(table, predicate);
  94. if (!obj)
  95. return obj.To<TDboGet>();
  96. return OpResult<TDboGet>.Ok(DbToDboGetStatic(obj.Data));
  97. });
  98. }
  99. /// <summary>
  100. /// Get an object by its primary key(s)
  101. /// </summary>
  102. /// <param name="keys">The key names and values</param>
  103. /// <returns>The object</returns>
  104. public static OpResult<TDboGet> GetSingleByKeys(params KeyValuePair<string, object>[] keys)
  105. {
  106. return GetSingle(GetExpression(keys));
  107. }
  108. /// <summary>
  109. /// Get an object by its id
  110. /// </summary>
  111. /// <param name="id">The id of the object</param>
  112. /// <returns>The object</returns>
  113. public static OpResult<TDboGet> GetSingleById(long id)
  114. {
  115. return GetSingleByKeys(new KeyValuePair<string, object>("id", id));
  116. }
  117. /// <summary>
  118. /// Get all DB object matching the predicate
  119. /// </summary>
  120. /// <param name="predicate">The predicate</param>
  121. /// <param name="orderBy">The order by function</param>
  122. /// <param name="page">The page numeber (0 based)</param>
  123. /// <param name="perPage">The maximum number of items par page</param>
  124. /// <returns>All matching objects</returns>
  125. public static OpResult<IEnumerable<TDboGet>> GetMultiple<TKey>(Expression<Func<TDbObject, bool>> predicate,
  126. Expression<Func<TDbObject, TKey>> orderBy, int page = 0,int perPage = Int32.MaxValue)
  127. {
  128. return Execute((db, table) =>
  129. {
  130. var results = table.OrderBy(orderBy).Where(predicate).Skip(page * perPage).Take(perPage).ToList();
  131. return OpResult<IEnumerable<TDboGet>>.Ok(results.Select(DbToDboGetStatic));
  132. });
  133. }
  134. /// <summary>
  135. /// Get all DB object matching the predicate built from keys
  136. /// </summary>
  137. /// <param name="page">The page numeber (0 based)</param>
  138. /// <param name="perPage">The maximum number of items par page</param>
  139. /// <param name="orderBy">The order by function</param>
  140. /// <param name="keys">The key names and values</param>
  141. /// <returns>All matching objects</returns>
  142. public static OpResult<IEnumerable<TDboGet>> GetMultipleByKeys<TKey>(Expression<Func<TDbObject, TKey>> orderBy,
  143. int page = 0, int perPage = Int32.MaxValue, params KeyValuePair<string, object>[] keys)
  144. {
  145. return GetMultiple(GetExpression(keys), orderBy, page, perPage);
  146. }
  147. /// <summary>
  148. /// Add an entry in the database
  149. /// </summary>
  150. /// <param name="obj">The object to be added</param>
  151. /// <returns>Always true or an OpResult error</returns>
  152. public static OpResult<bool> Add(TDboAdd obj)
  153. {
  154. return Execute((db, table) =>
  155. {
  156. table.Add(DboAddToDbStatic(obj));
  157. db.SaveChanges();
  158. return OpResult<bool>.Ok(true);
  159. });
  160. }
  161. /// <summary>
  162. /// Edit an entry in the database
  163. /// </summary>
  164. /// <param name="obj">The object to be added</param>
  165. /// <param name="predicate">The predicate to be used to select data</param>
  166. /// <returns>Always true or an OpResult error</returns>
  167. public static OpResult<bool> Edit(TDboEdit obj, Expression<Func<TDbObject, bool>> predicate)
  168. {
  169. return Execute((db, table) =>
  170. {
  171. var edit = GetSingleDbObject(table, predicate);
  172. if (!edit)
  173. return edit.To<bool>();
  174. var res = DboEditToDbStatic(obj, edit.Data);
  175. db.Entry(edit.Data).CurrentValues.SetValues(res);
  176. db.SaveChanges();
  177. return OpResult<bool>.Ok(true);
  178. });
  179. }
  180. /// <summary>
  181. /// Edit an entry in the database
  182. /// </summary>
  183. /// <param name="obj">The object to be added</param>
  184. /// <param name="keys">The keys to be used to select data</param>
  185. /// <returns>Always true or an OpResult error</returns>
  186. public static OpResult<bool> Edit(TDboEdit obj, params KeyValuePair<string, object>[] keys)
  187. {
  188. return Edit(obj, GetExpression(keys));
  189. }
  190. /// <summary>
  191. /// Edit an entry in the database
  192. /// </summary>
  193. /// <param name="obj">The object to be added</param>
  194. /// <param name="id">The id to be edited</param>
  195. /// <returns>Always true or an OpResult error</returns>
  196. public static OpResult<bool> EditById(TDboEdit obj, long id)
  197. {
  198. return Edit(obj, new KeyValuePair<string, object>("id", id));
  199. }
  200. /// <summary>
  201. /// Delete an entry in the database
  202. /// </summary>
  203. /// <param name="predicate">The predicate to be used to delete data</param>
  204. /// <returns>Always true or an OpResult error</returns>
  205. public static OpResult<bool> Delete(Expression<Func<TDbObject, bool>> predicate)
  206. {
  207. return Execute((db, table) =>
  208. {
  209. var del = GetSingleDbObject(table, predicate);
  210. if (!del)
  211. return del.To<bool>();
  212. table.Remove(del.Data);
  213. db.SaveChanges();
  214. return OpResult<bool>.Ok(true);
  215. });
  216. }
  217. /// <summary>
  218. /// Delete an entry in the database
  219. /// </summary>
  220. /// <param name="keys">The keys to be used to delete data</param>
  221. /// <returns>Always true or an OpResult error</returns>
  222. public static OpResult<bool> Delete(params KeyValuePair<string, object>[] keys)
  223. {
  224. return Delete(GetExpression(keys));
  225. }
  226. /// <summary>
  227. /// Delete an entry in the database
  228. /// </summary>
  229. /// <param name="id">The id to be deleted</param>
  230. /// <returns>Always true or an OpResult error</returns>
  231. public static OpResult<bool> DeleteById(long id)
  232. {
  233. return Delete(new KeyValuePair<string, object>("id", id));
  234. }
  235. }
  236. }