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.

254 lines
9.4KB

  1. using System.Data;
  2. using System.Data.Common;
  3. using System.Linq.Expressions;
  4. namespace HealthMonitor.Core.Dal
  5. {
  6. /// <summary>
  7. /// 数据接入层接口
  8. /// </summary>
  9. public interface IDataAccessor : IWithDataSchema, IDisposable
  10. {
  11. /// <summary>
  12. /// 是否已关闭
  13. /// </summary>
  14. /// <returns></returns>
  15. bool IsClose();
  16. /// <summary>
  17. /// 关闭连接
  18. /// </summary>
  19. void Close();
  20. /// <summary>
  21. /// 获取第一个符合条件的实体,或返回null
  22. /// </summary>
  23. /// <param name="expression"></param>
  24. /// <returns></returns>
  25. T GetFirstOrDefault<T>(Expression<Func<T, bool>> expression) where T : class;
  26. Task<T> GetFirstOrDefaultAsync<T>(Expression<Func<T, bool>> expression) where T : class;
  27. /// <summary>
  28. /// 根据主键值来查询某条记录,单主键的表可直接输入主键,多主键的表注意主键次序
  29. /// </summary>
  30. /// <typeparam name="T">实体类型</typeparam>
  31. /// <param name="values">主键值</param>
  32. /// <returns>返回主键值为传入值的实体</returns>
  33. T GetByID<T>(params object[] values) where T : class;
  34. /// <summary>
  35. /// 根据主键值来查询某条记录,单主键的表可直接输入主键,多主键的表注意主键次序
  36. /// </summary>
  37. /// <typeparam name="T">实体类型</typeparam>
  38. /// <param name="values">主键值</param>
  39. /// <returns>返回主键值为传入值的实体</returns>
  40. Task<T> GetByIDAsync<T>(params object[] values) where T : class;
  41. /// <summary>
  42. /// 获取某个表的所有数据
  43. /// </summary>
  44. /// <typeparam name="T"></typeparam>
  45. /// <returns></returns>
  46. IQueryable<T> GetAll<T>() where T : class;
  47. /// <summary>
  48. /// 根据表达式进行查询返回结果
  49. /// </summary>
  50. /// <typeparam name="T">查询的类</typeparam>
  51. /// <param name="expression">查询表达式</param>
  52. /// <returns></returns>
  53. IQueryable<T> GetMany<T>(Expression<Func<T, bool>> expression) where T : class;
  54. /// <summary>
  55. /// 根据某个字段的值进行排序
  56. /// </summary>
  57. /// <typeparam name="T">排序后获得的集合的类型</typeparam>
  58. /// <typeparam name="TKey">排序字段的类型</typeparam>
  59. /// <param name="orderExpression">字段表达式 如:basicInfo下根据caseID排序(s=>s.caseID)</param>
  60. /// <param name="isASC">是否升序</param>
  61. /// <returns></returns>
  62. IEnumerable<T> Order<T, TKey>(Func<T, TKey> orderExpression, bool isASC = false) where T : class;
  63. /// <summary>
  64. /// 在数据库中进行分页查询
  65. /// </summary>
  66. /// <typeparam name="T">查询的类</typeparam>
  67. /// <typeparam name="Tkey">查询类的主键类型</typeparam>
  68. /// <param name="pageSize">每页条数</param>
  69. /// <param name="pageIdx">当前页</param>
  70. /// <param name="expression">查询表达式</param>
  71. /// <param name="orderExpression"></param>
  72. /// <returns></returns>
  73. List<T> GetPageList<T, Tkey>(int pageSize, int pageIdx, Expression<Func<T, bool>> expression, Func<T, Tkey> orderExpression) where T : class;
  74. /// <summary>
  75. /// 在数据库中进行分页查询
  76. /// </summary>
  77. /// <typeparam name="T">查询的类</typeparam>
  78. /// <typeparam name="Tkey">查询类的主键类型</typeparam>
  79. /// <param name="pageSize">页大小</param>
  80. /// <param name="pageIdx">页下标</param>
  81. /// <param name="expression">查询表达式</param>
  82. /// <param name="orderExpression">排序表达式</param>
  83. /// <returns>分页查询结果</returns>
  84. Task<List<T>> GetPageListAsync<T, Tkey>(int pageSize, int pageIdx, Expression<Func<T, bool>> expression, Func<T, Tkey> orderExpression) where T : class;
  85. /// <summary>
  86. /// 获取条目数
  87. /// </summary>
  88. /// <typeparam name="T">查询的类</typeparam>
  89. /// <param name="expression">查询表达式</param>
  90. /// <returns>条目数</returns>
  91. int GetCount<T>(Expression<Func<T, bool>> expression) where T : class;
  92. /// <summary>
  93. /// 获取条目数
  94. /// </summary>
  95. /// <typeparam name="T">查询的类</typeparam>
  96. /// <param name="expression">查询表达式</param>
  97. /// <returns>条目数</returns>
  98. Task<int> GetCountAsync<T>(Expression<Func<T, bool>> expression) where T : class;
  99. /// <summary>
  100. /// 插入数据,保存并更新其自增键
  101. /// </summary>
  102. /// <typeparam name="T"></typeparam>
  103. /// <param name="model"></param>
  104. /// <returns></returns>
  105. T AddWithIdentity<T>(T model) where T : class;
  106. /// <summary>
  107. /// 插入数据,保存并更新其自增键
  108. /// </summary>
  109. /// <typeparam name="T"></typeparam>
  110. /// <param name="model"></param>
  111. /// <returns></returns>
  112. Task<T> AddWithIdentityAsync<T>(T model) where T : class;
  113. /// <summary>
  114. /// 向上下文增加记录,但不保存,需要手动调用Save
  115. /// </summary>
  116. /// <typeparam name="T"></typeparam>
  117. /// <param name="model"></param>
  118. /// <returns></returns>
  119. void Add<T>(T model) where T : class;
  120. /// <summary>
  121. /// 向上下文增加记录,但不保存,需要手动调用Save
  122. /// </summary>
  123. /// <param name="model"></param>
  124. /// <returns></returns>
  125. void Add(object model);
  126. /// <summary>
  127. /// 批量插入实体
  128. /// </summary>
  129. /// <param name="models"></param>
  130. void AddRange(IEnumerable<object> models);
  131. /// <summary>
  132. /// 按主键标记实体删除
  133. /// </summary>
  134. /// <typeparam name="T"></typeparam>
  135. /// <param name="model"></param>
  136. /// <returns></returns>
  137. void Delete<T>(T model) where T : class;
  138. /// <summary>
  139. /// 按主键标记实体删除
  140. /// </summary>
  141. /// <param name="model"></param>
  142. /// <returns></returns>
  143. void Delete(object model);
  144. /// <summary>
  145. ///批量删除实体
  146. /// </summary>
  147. /// <param name="models"></param>
  148. void DeleteRanage(IEnumerable<object> models);
  149. /// <summary>
  150. /// 更新操作
  151. /// </summary>
  152. /// <typeparam name="T"></typeparam>
  153. /// <param name="model"></param>
  154. /// <returns></returns>
  155. void Update<T>(T model) where T : class;
  156. /// <summary>
  157. /// 根据主键更新指定字段,使用时需要注意EFCore的仓储机制可能会使数据库和缓存仓的数据不一致。
  158. /// </summary>
  159. /// <typeparam name="T">映射类</typeparam>
  160. /// <param name="model">更新的实体</param>
  161. /// <param name="property">要更新的属性, VisualStudio在这里有Bug, 不能智能显示类型属性, 但不影响使用</param>
  162. /// <returns></returns>
  163. void Update<T>(T model, Expression<Func<T, object>> property) where T : class;
  164. /// <summary>
  165. /// 更新操作
  166. /// </summary>
  167. /// <param name="model"></param>
  168. /// <returns></returns>
  169. void Update(object model);
  170. /// <summary>
  171. /// 批量更新实体
  172. /// </summary>
  173. /// <param name="models"></param>
  174. void UpdateRanage(IEnumerable<object> models);
  175. /// <summary>
  176. /// 执行存储过程,返回存储过程中返回的数据表
  177. /// </summary>
  178. /// <param name="procName">存储过程名</param>
  179. /// <param name="parameters">参数</param>
  180. /// <returns>返回的数据表集合</returns>
  181. List<DataTable> CallProcedure(string procName, params DbParameter[] parameters);
  182. /// <summary>
  183. /// 执行存储过程,返回存储过程中返回的数据表
  184. /// </summary>
  185. /// <param name="procName">存储过程名</param>
  186. /// <param name="parameters">参数</param>
  187. /// <returns>返回的数据表集合</returns>
  188. Task<List<DataTable>> CallProcedureAsync(string procName, params DbParameter[] parameters);
  189. /// <summary>
  190. /// 提交对数据进行的处理,如无处理返回-1
  191. /// </summary>
  192. /// <returns></returns>
  193. int Save();
  194. /// <summary>
  195. /// 提交对数据进行的处理,如无处理返回-1
  196. /// </summary>
  197. /// <returns></returns>
  198. Task<int> SaveAsync();
  199. ///// <summary>
  200. ///// 异步获取一个事务对象
  201. ///// </summary>
  202. ///// <returns></returns>
  203. //Task<IDbContextTransaction> BeginTransactionAsync();
  204. ///// <summary>
  205. ///// 获取一个事务对象
  206. ///// </summary>
  207. ///// <returns></returns>
  208. //IDbContextTransaction BeginTransaction();
  209. /// <summary>
  210. /// 不跟踪实体状态(针对DbContext所用)
  211. /// </summary>
  212. /// <param name="models"></param>
  213. void UntrackEntities(IEnumerable<object> models);
  214. /// <summary>
  215. /// 不跟踪实体状态(针对DbContext所用)
  216. /// </summary>
  217. /// <param name="models"></param>
  218. void UntrackEntities(object model);
  219. }
  220. }