IViewRepository.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Dapper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Threading.Tasks;
  7. using XYY.Common.Standard;
  8. namespace XYY.Core.Standard.Data.Infrastructure
  9. {
  10. public interface IViewRepository<T> where T : class
  11. {
  12. [NonTrans]
  13. Task<T> GetAsync(int id);
  14. [NonTrans]
  15. Task<bool> IsExistsAsync(Expression<Func<T, bool>> expression);
  16. [NonTrans]
  17. Task<IEnumerable<T>> QueryAsync(string sql, object param = null);
  18. [NonTrans]
  19. Task<IEnumerable<T>> QueryBySqlAsync(string sql);
  20. [NonTrans]
  21. Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression);
  22. [NonTrans]
  23. Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression, string customSql);
  24. [NonTrans]
  25. Task<SqlMapper.GridReader> QueryMultipleAsync(string sql, object param = null);
  26. [NonTrans]
  27. Task<IEnumerable<T>> QueryAsync();
  28. [NonTrans]
  29. Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, int? timeout = null);
  30. [NonTrans]
  31. Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, string customSql, int? timeout = null);
  32. [NonTrans]
  33. Task<int> CountAsync(Expression<Func<T, bool>> expression);
  34. [NonTrans]
  35. Task<IDictionary<int, int>> CountAsync<TProperty>(Expression<Func<T, TProperty>> propName, Expression<Func<T, bool>> expression, QueryModel model = null);
  36. [NonTrans]
  37. string GetWhere(List<QueryParamer> paramer);
  38. }
  39. public abstract class ViewRepository<T> : IViewRepository<T> where T : class
  40. {
  41. //protected readonly IDbConnection _unitOfWork.Connection;
  42. //protected readonly IDbTransaction _unitOfWork.Transaction;
  43. protected readonly IUnitOfWork _unitOfWork;
  44. //public ViewRepository(IUnitOfWork unitOfWork)
  45. //{
  46. // _unitOfWork.Connection = connection;
  47. // _unitOfWork.Transaction = transaction;
  48. //}
  49. public ViewRepository(IUnitOfWork unitOfWork)
  50. {
  51. _unitOfWork = unitOfWork;
  52. }
  53. public async Task<T> GetAsync(int id)
  54. {
  55. Ensure.GreaterThan(0, id, "id");
  56. return await _unitOfWork.GetAsync<T>(id);
  57. }
  58. public async Task<bool> IsExistsAsync(Expression<Func<T, bool>> expression)
  59. {
  60. Ensure.NotNull(expression, "expression");
  61. return await _unitOfWork.IsExistsAsync(expression);
  62. }
  63. public async Task<IEnumerable<T>> QueryAsync(string sql, object param = null)
  64. {
  65. Ensure.NotNullOrEmpty(sql, "sql");
  66. return await _unitOfWork.Connection.QueryAsync<T>(sql, param, _unitOfWork.Transaction);
  67. }
  68. public async Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression)
  69. {
  70. Ensure.NotNull(expression, "expression");
  71. return await _unitOfWork.QueryAsync<T>(expression);
  72. }
  73. public virtual async Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression, string customSql)
  74. {
  75. Ensure.NotNull(expression, "expression");
  76. if (string.IsNullOrEmpty(customSql))
  77. return await this.QueryAsync(expression);
  78. return await _unitOfWork.QueryAsync<T>(expression, customSql);
  79. }
  80. public async Task<SqlMapper.GridReader> QueryMultipleAsync(string sql, object param = null)
  81. {
  82. Ensure.NotNullOrEmpty(sql, "sql");
  83. return await _unitOfWork.Connection.QueryMultipleAsync(sql, param, _unitOfWork.Transaction);
  84. }
  85. public async Task<IEnumerable<T>> QueryAsync()
  86. {
  87. return await _unitOfWork.QueryAsync<T>();
  88. }
  89. public async Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, int? timeout = null)
  90. {
  91. Ensure.NotNull(queryModel, "queryModel");
  92. Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex");
  93. Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize");
  94. return await _unitOfWork.GetPagingListAsync<T>(queryModel, timeout);
  95. }
  96. public virtual async Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, string customSql, int? timeout = null)
  97. {
  98. if (string.IsNullOrEmpty(customSql.Trim()))
  99. return await QueryPagerAsync(queryModel);
  100. Ensure.NotNull(queryModel, "queryModel");
  101. Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex");
  102. Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize");
  103. return await _unitOfWork.GetPagingListAsync<T>(queryModel, customSql, timeout);
  104. }
  105. public async Task<int> CountAsync(Expression<Func<T, bool>> expression)
  106. {
  107. return await _unitOfWork.CountAsync(expression);
  108. }
  109. public async Task<IDictionary<int, int>> CountAsync<TProperty>(Expression<Func<T, TProperty>> propName, Expression<Func<T, bool>> expression, QueryModel model = null)
  110. {
  111. return await _unitOfWork.GroupCountAsync<T, TProperty>(propName, expression, model);
  112. }
  113. public async Task<IEnumerable<T>> QueryBySqlAsync(string sql)
  114. {
  115. return await _unitOfWork.QueryBySqlAsync<T>(sql);
  116. }
  117. public string GetWhere(List<QueryParamer> paramer)
  118. {
  119. return _unitOfWork.GetWhere(paramer);
  120. }
  121. }
  122. }