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.

SqlServerManager.cs 10KB

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