为减少代码量,这里实现一个基于Dapper的泛型Repository。
这里需要引用Dapper.dll和Dapper.Contrib.dll。
接口定义:
1 ///2 /// Repository接口 3 /// 4 /// 5 public interface IRepository where T : class, IEntity 6 { 7 /// 8 /// 查询 9 /// 10 /// 11 /// 12 /// 13 /// 14 /// 15 IEnumerable Query(string sql, object param = null, CommandType? commandType = null); 16 17 /// 18 /// 删除行数据 19 /// 20 /// 21 /// 22 /// 23 bool Delete(T entity); 24 /// 25 /// 删除表所有数据 26 /// 27 /// 28 /// 29 bool DeleteAll(); 30 /// 31 /// 获取行数据 32 /// 33 /// 34 /// 35 /// 36 T Get(object id); 37 /// 38 /// 获取表的所有数据 39 /// 40 /// 41 /// 42 IEnumerable GetAll(); 43 /// 44 /// 添加行数据 45 /// 46 /// 47 /// 48 /// 49 long Insert(T entity); 50 /// 51 /// 更新行数据 52 /// 53 /// 54 /// 55 /// 56 bool Update(T entity); 57 58 /// 59 /// 分页方法 60 /// 61 /// 62 /// 当前页码 63 /// 每页显示条数 64 /// 参数 65 /// 66 /// 67 /// 68 PagedResult GetPageList(string sql, int pageIndex, int pageSize, object param = null); 69 }
接口实现:
1 ///2 /// 泛型Repository 3 /// 4 /// 5 public class Repository : IRepository where T : class, IEntity 6 { 7 private IUnitOfWork _unitOfWork; 8 9 public Repository(IUnitOfWork unitOfWork) 10 { 11 _unitOfWork = unitOfWork; 12 } 13 /// 14 /// 查询 15 /// 16 /// 17 /// 18 /// 19 /// 20 /// 21 public IEnumerable Query(string sql, object param = null, CommandType? commandType = null) 22 { 23 var r = _unitOfWork.DbConnection.Query (sql, param: param, transaction: _unitOfWork.DbTransaction, commandType: commandType); 24 return r; 25 } 26 /// 27 /// 删除行数据 28 /// 29 /// 30 /// 31 /// 32 public bool Delete(T entity) 33 { 34 var r = _unitOfWork.DbConnection.Delete (entity, _unitOfWork.DbTransaction); 35 return r; 36 } 37 /// 38 /// 删除表所有数据 39 /// 40 /// 41 /// 42 public bool DeleteAll() 43 { 44 var r = _unitOfWork.DbConnection.DeleteAll (_unitOfWork.DbTransaction); 45 return r; 46 } 47 /// 48 /// 获取行数据 49 /// 50 /// 51 /// 52 /// 53 public T Get(object id) 54 { 55 var r = _unitOfWork.DbConnection.Get (id, _unitOfWork.DbTransaction); 56 return r; 57 } 58 /// 59 /// 获取表的所有数据 60 /// 61 /// 62 /// 63 public IEnumerable GetAll() 64 { 65 var r = _unitOfWork.DbConnection.GetAll (_unitOfWork.DbTransaction); 66 return r; 67 } 68 /// 69 /// 添加行数据 70 /// 71 /// 72 /// 73 /// 74 public long Insert(T entity) 75 { 76 var r = _unitOfWork.DbConnection.Insert (entity, _unitOfWork.DbTransaction); 77 return r; 78 } 79 /// 80 /// 更新行数据 81 /// 82 /// 83 /// 84 /// 85 public bool Update(T entity) 86 { 87 var r = _unitOfWork.DbConnection.Update (entity, _unitOfWork.DbTransaction); 88 return r; 89 } 90 /// 91 /// 分页方法 92 /// 93 /// 94 /// 当前页码 95 /// 每页显示条数 96 /// 参数 97 /// 98 /// 99 /// 100 public PagedResult GetPageList(string sql, int pageIndex, int pageSize, object param = null) 101 { 102 var pagingResult = _unitOfWork.DbConnection.GetPageList (sql, pageIndex, pageSize, param: param, transaction: _unitOfWork.DbTransaction); 103 return pagingResult; 104 } 105 }
GetPageList为Dapper的一个分页扩展:
1 ///2 /// 分页方法 3 /// 4 /// 5 /// 当前页码 6 /// 每页显示条数 7 /// 参数 8 /// 9 /// 10 /// 11 public static PagedResult GetPageList (this IDbConnection connection, string sql, int pageIndex, int pageSize, object param = null, IDbTransaction transaction = null, int? commandTimeout = null) 12 { 13 if (pageIndex < 1) pageIndex = 1; 14 if (pageSize < 1) pageSize = 20; 15 var startRow = (pageIndex - 1) * pageSize; 16 17 //MySql分页 18 sql = $"SELECT * FROM ({sql}) tt1 LIMIT {startRow},{pageSize}; SELECT COUNT(1) FROM ({sql}) tt2;"; 19 20 PagedResult pagingResult = new PagedResult (); 21 pagingResult.Paged.PageIndex = pageIndex; 22 pagingResult.Paged.PageSize = pageSize; 23 using (var result = connection.QueryMultiple(sql, param: param, transaction, commandTimeout)) 24 { 25 var list = result.Read (); 26 var totalCount = result.Read<long>().FirstOrDefault(); 27 pagingResult.Data = list; 28 pagingResult.Paged.TotalRow = totalCount; 29 } 30 return pagingResult; 31 }
PagedResult类:
1 ///2 /// 分页模型 3 /// 4 /// 5 public class PagedResult 6 { 7 public PagedResult() 8 { 9 this.Paged = new Paged(); 10 } 11 12 /// 13 /// 结果 14 /// 15 public IEnumerable Data { get; set; } 16 17 /// 18 /// 分页数据,包含数据总行数、当前页码、页数据行数和总的分页数 19 /// 20 public Paged Paged { get; set; } 21 }
Paged类:
1 ///2 /// 分页数据 3 /// 4 public class Paged 5 { 6 /// 7 /// 当前页码 8 /// 9 public int PageIndex { get; set; } 10 /// 11 /// 页数据行数 12 /// 13 public int PageSize { get; set; } 14 /// 15 /// 总行数 16 /// 17 public long TotalRow { get; set; } 18 /// 19 /// 总的分页数 20 /// 21 public int TotalPage 22 { 23 get 24 { 25 if (this.TotalRow > 0 && this.PageSize > 0) 26 return (int)Math.Ceiling((decimal)this.TotalRow / this.PageSize); 27 else 28 return 0; 29 } 30 } 31 }
在IServiceCollection容器中注册:
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));