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 where T : class { [NonTrans] Task GetAsync(int id); [NonTrans] Task IsExistsAsync(Expression> expression); [NonTrans] Task> QueryAsync(string sql, object param = null); [NonTrans] Task> QueryBySqlAsync(string sql); [NonTrans] Task> QueryAsync(Expression> expression); [NonTrans] Task> QueryAsync(Expression> expression, string customSql); [NonTrans] Task QueryMultipleAsync(string sql, object param = null); [NonTrans] Task> QueryAsync(); [NonTrans] Task> QueryPagerAsync(QueryModel queryModel, int? timeout = null); [NonTrans] Task> QueryPagerAsync(QueryModel queryModel, string customSql, int? timeout = null); [NonTrans] Task CountAsync(Expression> expression); [NonTrans] Task> CountAsync(Expression> propName, Expression> expression, QueryModel model = null); [NonTrans] string GetWhere(List paramer); } public abstract class ViewRepository : IViewRepository 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 GetAsync(int id) { Ensure.GreaterThan(0, id, "id"); return await _unitOfWork.GetAsync(id); } public async Task IsExistsAsync(Expression> expression) { Ensure.NotNull(expression, "expression"); return await _unitOfWork.IsExistsAsync(expression); } public async Task> QueryAsync(string sql, object param = null) { Ensure.NotNullOrEmpty(sql, "sql"); return await _unitOfWork.Connection.QueryAsync(sql, param, _unitOfWork.Transaction); } public async Task> QueryAsync(Expression> expression) { Ensure.NotNull(expression, "expression"); return await _unitOfWork.QueryAsync(expression); } public virtual async Task> QueryAsync(Expression> expression, string customSql) { Ensure.NotNull(expression, "expression"); if (string.IsNullOrEmpty(customSql)) return await this.QueryAsync(expression); return await _unitOfWork.QueryAsync(expression, customSql); } public async Task QueryMultipleAsync(string sql, object param = null) { Ensure.NotNullOrEmpty(sql, "sql"); return await _unitOfWork.Connection.QueryMultipleAsync(sql, param, _unitOfWork.Transaction); } public async Task> QueryAsync() { return await _unitOfWork.QueryAsync(); } public async Task> 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(queryModel, timeout); } public virtual async Task> 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(queryModel, customSql, timeout); } public async Task CountAsync(Expression> expression) { return await _unitOfWork.CountAsync(expression); } public async Task> CountAsync(Expression> propName, Expression> expression, QueryModel model = null) { return await _unitOfWork.GroupCountAsync(propName, expression, model); } public async Task> QueryBySqlAsync(string sql) { return await _unitOfWork.QueryBySqlAsync(sql); } public string GetWhere(List paramer) { return _unitOfWork.GetWhere(paramer); } } }