123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using Dapper;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using XYY.Common.Standard;
- namespace XYY.Core.Standard.Data.Infrastructure
- {
- public interface IViewRepository<T> where T : class
- {
- [NonTrans]
- Task<T> GetAsync(int id);
- [NonTrans]
- Task<bool> IsExistsAsync(Expression<Func<T, bool>> expression);
- [NonTrans]
- Task<IEnumerable<T>> QueryAsync(string sql, object param = null);
- [NonTrans]
- Task<IEnumerable<T>> QueryBySqlAsync(string sql);
- [NonTrans]
- Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression);
- [NonTrans]
- Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression, string customSql);
- [NonTrans]
- Task<SqlMapper.GridReader> QueryMultipleAsync(string sql, object param = null);
- [NonTrans]
- Task<IEnumerable<T>> QueryAsync();
- [NonTrans]
- Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, int? timeout = null);
- [NonTrans]
- Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, string customSql, int? timeout = null);
- [NonTrans]
- Task<int> CountAsync(Expression<Func<T, bool>> expression);
- [NonTrans]
- Task<IDictionary<int, int>> CountAsync<TProperty>(Expression<Func<T, TProperty>> propName, Expression<Func<T, bool>> expression, QueryModel model = null);
- [NonTrans]
- string GetWhere(List<QueryParamer> paramer);
- }
- public abstract class ViewRepository<T> : IViewRepository<T> where T : class
- {
- //protected readonly IDbConnection _unitOfWork.Connection;
- //protected readonly IDbTransaction _unitOfWork.Transaction;
- protected readonly IUnitOfWork _unitOfWork;
- //public ViewRepository(IUnitOfWork unitOfWork)
- //{
- // _unitOfWork.Connection = connection;
- // _unitOfWork.Transaction = transaction;
- //}
- public ViewRepository(IUnitOfWork unitOfWork)
- {
- _unitOfWork = unitOfWork;
- }
- public async Task<T> GetAsync(int id)
- {
- Ensure.GreaterThan(0, id, "id");
- return await _unitOfWork.GetAsync<T>(id);
- }
- public async Task<bool> IsExistsAsync(Expression<Func<T, bool>> expression)
- {
- Ensure.NotNull(expression, "expression");
- return await _unitOfWork.IsExistsAsync(expression);
- }
- public async Task<IEnumerable<T>> QueryAsync(string sql, object param = null)
- {
- Ensure.NotNullOrEmpty(sql, "sql");
- return await _unitOfWork.Connection.QueryAsync<T>(sql, param, _unitOfWork.Transaction);
- }
- public async Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression)
- {
- Ensure.NotNull(expression, "expression");
- return await _unitOfWork.QueryAsync<T>(expression);
- }
- public virtual async Task<IEnumerable<T>> QueryAsync(Expression<Func<T, bool>> expression, string customSql)
- {
- Ensure.NotNull(expression, "expression");
- if (string.IsNullOrEmpty(customSql))
- return await this.QueryAsync(expression);
- return await _unitOfWork.QueryAsync<T>(expression, customSql);
- }
- public async Task<SqlMapper.GridReader> QueryMultipleAsync(string sql, object param = null)
- {
- Ensure.NotNullOrEmpty(sql, "sql");
- return await _unitOfWork.Connection.QueryMultipleAsync(sql, param, _unitOfWork.Transaction);
- }
- public async Task<IEnumerable<T>> QueryAsync()
- {
- return await _unitOfWork.QueryAsync<T>();
- }
- public async Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, int? timeout = null)
- {
- Ensure.NotNull(queryModel, "queryModel");
- Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex");
- Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize");
- return await _unitOfWork.GetPagingListAsync<T>(queryModel, timeout);
- }
- public virtual async Task<PageResult<T>> QueryPagerAsync(QueryModel queryModel, string customSql, int? timeout = null)
- {
- if (string.IsNullOrEmpty(customSql.Trim()))
- return await QueryPagerAsync(queryModel);
- Ensure.NotNull(queryModel, "queryModel");
- Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex");
- Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize");
- return await _unitOfWork.GetPagingListAsync<T>(queryModel, customSql, timeout);
- }
- public async Task<int> CountAsync(Expression<Func<T, bool>> expression)
- {
- return await _unitOfWork.CountAsync(expression);
- }
- public async Task<IDictionary<int, int>> CountAsync<TProperty>(Expression<Func<T, TProperty>> propName, Expression<Func<T, bool>> expression, QueryModel model = null)
- {
- return await _unitOfWork.GroupCountAsync<T, TProperty>(propName, expression, model);
- }
- public async Task<IEnumerable<T>> QueryBySqlAsync(string sql)
- {
- return await _unitOfWork.QueryBySqlAsync<T>(sql);
- }
- public string GetWhere(List<QueryParamer> paramer)
- {
- return _unitOfWork.GetWhere(paramer);
- }
- }
- }
|