IRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq.Expressions;
  7. using System.Threading.Tasks;
  8. namespace Repository
  9. {
  10. public interface IRepository
  11. {
  12. DbContext GetCurrentDbContext();
  13. /// <summary>
  14. /// 添加实体
  15. /// </summary>
  16. /// <typeparam name="T"></typeparam>
  17. /// <param name="entity"></param>
  18. /// <returns></returns>
  19. bool Add<T>(T entity) where T : class;
  20. /// <summary>
  21. /// 异步实现
  22. /// </summary>
  23. /// <typeparam name="T"></typeparam>
  24. /// <param name="entity"></param>
  25. /// <returns></returns>
  26. Task<bool> AddAsync<T>(T entity) where T : class;
  27. /// <summary>
  28. /// 批量的进行添加实体
  29. /// </summary>
  30. /// <typeparam name="T"></typeparam>
  31. /// <param name="entity"></param>
  32. /// <returns></returns>
  33. bool AddRange<T>(IEnumerable<T> entity) where T : class;
  34. /// <summary>
  35. /// 批量的进行添加实体
  36. /// </summary>
  37. /// <typeparam name="T"></typeparam>
  38. /// <param name="entity"></param>
  39. /// <returns></returns>
  40. Task<bool> AddRangeAsync<T>(IEnumerable<T> entity) where T : class;
  41. /// <summary>
  42. /// 删除单个实体
  43. /// </summary>
  44. /// <typeparam name="T"></typeparam>
  45. /// <param name="entity"></param>
  46. /// <returns></returns>
  47. bool Delete<T>(T entity) where T : class;
  48. /// <summary>
  49. /// 删除所有
  50. /// </summary>
  51. /// <typeparam name="T"></typeparam>
  52. /// <returns></returns>
  53. bool DeleteAll<T>() where T : class;
  54. /// <summary>
  55. /// 异步删除所有
  56. /// </summary>
  57. /// <typeparam name="T"></typeparam>
  58. /// <returns></returns>
  59. Task<bool> DeleteAllAsync<T>() where T : class;
  60. /// <summary>
  61. /// 根据查询条件进行删除单个实体
  62. /// </summary>
  63. /// <typeparam name="T"></typeparam>
  64. /// <param name="whereLambda"></param>
  65. /// <returns></returns>
  66. bool Delete<T>(Expression<Func<T, bool>> whereLambda) where T : class;
  67. /// <summary>
  68. /// 根据查询条件进行异步删除单个实体
  69. /// </summary>
  70. /// <typeparam name="T"></typeparam>
  71. /// <param name="whereLambda"></param>
  72. /// <returns></returns>
  73. Task<bool> DeleteAsync<T>(Expression<Func<T, bool>> whereLambda) where T : class;
  74. bool DeleteById<T>(dynamic id) where T : class;
  75. Task<bool> DeleteByIdAsync<T>(dynamic id) where T : class;
  76. /// <summary>
  77. ///单个对象的修改
  78. /// </summary>
  79. /// <typeparam name="T"></typeparam>
  80. /// <param name="entity">需要修改的对象</param>
  81. /// <returns></returns>
  82. bool Update<T>(T entity) where T : class;
  83. Task<bool> UpdateAsync<T>(T entity) where T : class;
  84. /// <summary>
  85. /// 批量修改
  86. /// </summary>
  87. /// <typeparam name="T"></typeparam>
  88. /// <param name="updateLambda"></param>
  89. /// <param name="whereLambda">
  90. /// </param>
  91. /// <returns></returns>
  92. bool Update<T>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, T>> updateLambda) where T : class;
  93. /// <summary>
  94. /// 异步批量修改
  95. /// </summary>
  96. /// <typeparam name="T"></typeparam>
  97. /// <param name="updateLambda"></param>
  98. /// <param name="whereLambda">
  99. /// </param>
  100. /// <returns></returns>
  101. Task<bool> UpdateAsync<T>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, T>> updateLambda) where T : class;
  102. /// <summary>
  103. /// 批量的修改
  104. /// </summary>
  105. /// <typeparam name="T"></typeparam>
  106. /// <param name="entity"></param>
  107. /// <returns></returns>
  108. bool Update<T>(List<T> entity) where T : class;
  109. /// <summary>
  110. /// 批量统一的进行更新
  111. /// </summary>
  112. /// <typeparam name="T"></typeparam>
  113. /// <param name="model">需要修改的对象实体</param>
  114. /// <param name="whereLambda">查询的条件</param>
  115. /// <param name="modifiedProNames"></param>
  116. /// <returns></returns>
  117. bool Update<T>(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames) where T : class;
  118. /// <summary>
  119. /// 根据主键进行查询
  120. /// </summary>
  121. /// <typeparam name="T"></typeparam>
  122. /// <param name="id"></param>
  123. /// <returns></returns>
  124. T FindById<T>(dynamic id) where T : class;
  125. /// <summary>
  126. /// 根据主键进行查询异步
  127. /// </summary>
  128. /// <typeparam name="T"></typeparam>
  129. /// <param name="id"></param>
  130. /// <returns></returns>
  131. Task<T> FindByIdAsync<T>(dynamic id) where T : class;
  132. /// <summary>
  133. /// 默认查询选择第一条数据,没有那么进行返回NULL
  134. /// </summary>
  135. /// <typeparam name="T"></typeparam>
  136. /// <param name="whereLambda"></param>
  137. /// <returns>返回bool</returns>
  138. T GetFirstDefault<T>(Expression<Func<T, bool>> whereLambda = null) where T : class;
  139. Task<T> GetFirstDefaultAsync<T>(Expression<Func<T, bool>> whereLambda = null) where T : class;
  140. /// <summary>
  141. /// 查询所有的数据
  142. /// </summary>
  143. /// <typeparam name="T"></typeparam>
  144. /// <returns></returns>
  145. List<T> GetAll<T>(string orderProperty) where T : class;
  146. /// <summary>
  147. /// 查询所有的数据
  148. /// </summary>
  149. /// <typeparam name="T"></typeparam>
  150. /// <returns></returns>
  151. Task<List<T>> GetAllAsync<T>(string orderProperty) where T : class;
  152. /// <summary>
  153. /// 含有带条件的查询
  154. /// </summary>
  155. /// <typeparam name="T"></typeparam>
  156. /// <param name="path">Include Path</param>
  157. /// <param name="whereLambda"></param>
  158. /// <returns></returns>
  159. List<T> GetAllQuery<T>(string path,Expression<Func<T, bool>> whereLambda = null) where T : class;
  160. /// <summary>
  161. /// 含有带条件的查询
  162. /// </summary>
  163. /// <typeparam name="T"></typeparam>
  164. /// <param name="whereLambda"></param>
  165. /// <returns></returns>
  166. List<T> GetAllQuery<T>(Expression<Func<T, bool>> whereLambda = null) where T : class;
  167. /// <summary>
  168. /// 异步含有带条件的查询
  169. /// </summary>
  170. /// <typeparam name="T"></typeparam>
  171. /// <param name="whereLambda"></param>
  172. /// <returns></returns>
  173. Task<List<T>> GetAllQueryAsync<T>(Expression<Func<T, bool>> whereLambda = null) where T : class;
  174. /// <summary>
  175. ///获取查询的数量
  176. /// </summary>
  177. /// <typeparam name="T"></typeparam>
  178. /// <param name="whereLambda"></param>
  179. /// <returns></returns>
  180. int GetCount<T>(Expression<Func<T, bool>> whereLambda = null) where T : class;
  181. /// <summary>
  182. ///异步获取查询的数量
  183. /// </summary>
  184. /// <typeparam name="T"></typeparam>
  185. /// <param name="whereLambda"></param>
  186. /// <returns></returns>
  187. Task<int> GetCountAsync<T>(Expression<Func<T, bool>> whereLambda = null) where T : class;
  188. /// <summary>
  189. /// 判断对象是否存在
  190. /// </summary>
  191. /// <typeparam name="T"></typeparam>
  192. /// <param name="whereLambda"></param>
  193. /// <returns></returns>
  194. bool GetAny<T>(Expression<Func<T, bool>> whereLambda = null) where T : class;
  195. /// <summary>
  196. /// 异步判断对象是否存在
  197. /// </summary>
  198. /// <typeparam name="T"></typeparam>
  199. /// <param name="whereLambda"></param>
  200. /// <returns></returns>
  201. Task<bool> GetAnyAsync<T>(Expression<Func<T, bool>> whereLambda = null) where T : class;
  202. /// <summary>
  203. /// 根据查询过条件进行分页
  204. /// </summary>
  205. /// <typeparam name="T"></typeparam>
  206. /// <typeparam name="TKey"></typeparam>
  207. /// <param name="pageIndex">当前页面</param>
  208. /// <param name="pageSize">页面的大小</param>
  209. /// <param name="orderBy">排序的条件</param>
  210. /// <param name="whereLambda">查询条件</param>
  211. /// <param name="isOrder">是否正序</param>
  212. /// <returns></returns>
  213. List<T> Pagination<T, TKey>(int pageIndex, int pageSize, Expression<Func<T, TKey>> orderBy, Expression<Func<T, bool>> whereLambda = null, bool isOrder = true) where T : class;
  214. /// <summary>
  215. /// 异步根据查询过条件进行分页
  216. /// </summary>
  217. /// <typeparam name="T"></typeparam>
  218. /// <typeparam name="TKey"></typeparam>
  219. /// <param name="pageIndex">当前页面</param>
  220. /// <param name="pageSize">页面的大小</param>
  221. /// <param name="orderBy">排序的条件</param>
  222. /// <param name="whereLambda">查询条件</param>
  223. /// <param name="isOrder">是否正序</param>
  224. /// <returns></returns>
  225. Task<List<T>> PaginationAsync<T, TKey>(int pageIndex, int pageSize, Expression<Func<T, TKey>> orderBy,
  226. Expression<Func<T, bool>> whereLambda = null, bool isOrder = true) where T : class;
  227. /// <summary>
  228. /// 根据查询条件进行做分页查询
  229. /// </summary>
  230. /// <typeparam name="T">查询的对象</typeparam>
  231. /// <param name="pageIndex">当前的页码</param>
  232. /// <param name="pageSize">每页的大小</param>
  233. /// <param name="ordering">排序条件</param>
  234. /// <param name="whereLambda">查询条件</param>
  235. /// <returns></returns>
  236. List<T> Pagination<T>(int pageIndex, int pageSize, string ordering, Expression<Func<T, bool>> whereLambda = null) where T : class;
  237. /// <summary>
  238. /// 根据查询条件进行转化
  239. /// </summary>
  240. /// <typeparam name="T"></typeparam>
  241. /// <param name="whereLambda"></param>
  242. /// <returns></returns>
  243. List<T> GetSelect<T>(Expression<Func<T, bool>> whereLambda) where T : class;
  244. /// <summary>
  245. /// 执行存储过程或自定义sql语句--返回集合
  246. /// </summary>
  247. /// <typeparam name="T"></typeparam>
  248. /// <param name="sql"></param>
  249. /// <param name="parameters"></param>
  250. /// <param name="cmdType"></param>
  251. /// <returns></returns>
  252. List<T> QueryPro<T>(string sql, List<SqlParameter> parameters, CommandType cmdType = CommandType.Text) where T : class;
  253. /// <summary>
  254. /// 回滚
  255. /// </summary>
  256. /// <typeparam name="T"></typeparam>
  257. void RollBackChanges<T>() where T : class;
  258. }
  259. }