基于Dapper的泛型Repository

为减少代码量,这里实现一个基于Dapper的泛型Repository。

这里需要引用Dapper.dll和Dapper.Contrib.dll。

接口定义: 

/// 
/// Repository接口
/// 
/// 
public interface IRepository where T : class, IEntity
{
    /// 
    /// 查询
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    IEnumerable Query(string sql, object param = null, CommandType? commandType = null);

    /// 
    /// 删除行数据
    /// 
    /// 
    /// 
    /// 
    bool Delete(T entity);
    /// 
    /// 删除表所有数据
    /// 
    /// 
    /// 
    bool DeleteAll();
    /// 
    /// 获取行数据
    /// 
    /// 
    /// 
    /// 
    T Get(object id);
    /// 
    /// 获取表的所有数据
    /// 
    /// 
    /// 
    IEnumerable GetAll();
    /// 
    /// 添加行数据
    /// 
    /// 
    /// 
    /// 
    long Insert(T entity);
    /// 
    /// 更新行数据
    /// 
    /// 
    /// 
    /// 
    bool Update(T entity);

    /// 
    /// 分页方法
    /// 
    /// 
    /// 当前页码
    /// 每页显示条数
    /// 参数
    /// 
    /// 
    /// 
    PagedResult GetPageList(string sql, int pageIndex, int pageSize, object param = null);
}

 

 

接口实现:

/// 
/// 泛型Repository
/// 
/// 
public class Repository : IRepository where T : class, IEntity
{
    private IUnitOfWork _unitOfWork;

    public Repository(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;            
    }
    /// 
    /// 查询
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public IEnumerable Query(string sql, object param = null, CommandType? commandType = null)
    {
        var r = _unitOfWork.DbConnection.Query(sql, param: param, transaction: _unitOfWork.DbTransaction, commandType: commandType);
        return r;
    }
    /// 
    /// 删除行数据
    /// 
    /// 
    /// 
    /// 
    public bool Delete(T entity)
    {
        var r = _unitOfWork.DbConnection.Delete(entity, _unitOfWork.DbTransaction);
        return r;
    }
    /// 
    /// 删除表所有数据
    /// 
    /// 
    /// 
    public bool DeleteAll()
    {
        var r = _unitOfWork.DbConnection.DeleteAll(_unitOfWork.DbTransaction);
        return r;
    }
    /// 
    /// 获取行数据
    /// 
    /// 
    /// 
    /// 
    public T Get(object id)
    {
        var r = _unitOfWork.DbConnection.Get(id, _unitOfWork.DbTransaction);
        return r;
    }
    /// 
    /// 获取表的所有数据
    /// 
    /// 
    /// 
    public IEnumerable GetAll()
    {
        var r = _unitOfWork.DbConnection.GetAll(_unitOfWork.DbTransaction);
        return r;
    }
    /// 
    /// 添加行数据
    /// 
    /// 
    /// 
    /// 
    public long Insert(T entity)
    {
        var r = _unitOfWork.DbConnection.Insert(entity, _unitOfWork.DbTransaction);
        return r;
    }
    /// 
    /// 更新行数据
    /// 
    /// 
    /// 
    /// 
    public bool Update(T entity)
    {
        var r = _unitOfWork.DbConnection.Update(entity, _unitOfWork.DbTransaction);
        return r;
    }
    /// 
    /// 分页方法
    /// 
    /// 
    /// 当前页码
    /// 每页显示条数
    /// 参数
    /// 
    /// 
    /// 
    public PagedResult GetPageList(string sql, int pageIndex, int pageSize, object param = null)
    {
       var pagingResult =  _unitOfWork.DbConnection.GetPageList(sql, pageIndex, pageSize, param: param, transaction: _unitOfWork.DbTransaction);
        return pagingResult;
    }
}

 

GetPageList为Dapper的一个分页扩展:

/// 
/// 分页方法
/// 
/// 
/// 当前页码
/// 每页显示条数
/// 参数
/// 
/// 
/// 
public static PagedResult GetPageList(this IDbConnection connection, string sql, int pageIndex, int pageSize, object param = null, IDbTransaction transaction = null, int? commandTimeout = null)
{
    if (pageIndex < 1) pageIndex = 1;
    if (pageSize < 1) pageSize = 20;
    var startRow = (pageIndex - 1) * pageSize;

    //MySql分页
    sql = $"SELECT * FROM ({sql}) tt1  LIMIT {startRow},{pageSize};  SELECT COUNT(1) FROM ({sql}) tt2;";

    PagedResult pagingResult = new PagedResult();
    pagingResult.Paged.PageIndex = pageIndex;
    pagingResult.Paged.PageSize = pageSize;
    using (var result = connection.QueryMultiple(sql, param: param, transaction, commandTimeout))
    {
        var list = result.Read();
        var totalCount = result.Read<long>().FirstOrDefault();
        pagingResult.Data = list;
        pagingResult.Paged.TotalRowCount = totalCount;
    }
    return pagingResult;
}

 

PagedResult类:

/// 
/// 分页模型
/// 
/// 
public class PagedResult
{
    public PagedResult()
    {
        this.Paged = new Paged();
    }

    /// 
    /// 结果
    /// 
    public IEnumerable Data { get; set; }

    /// 
    /// 分页数据,包含数据总行数、当前页码、页数据行数和总的分页数
    /// 
    public Paged Paged { get; set; }
}

 

Paged类:

/// 
/// 分页数据
/// 
public class Paged
{
    /// 
    /// 当前页码
    /// 
    public int PageIndex { get; set; }
    /// 
    /// 页数据行数
    /// 
    public int PageSize { get; set; }
    /// 
    /// 总行数
    /// 
    public long TotalRow { get; set; }
    /// 
    /// 总的分页数
    /// 
    public int TotalPage
    {
        get
        {
            if (this.TotalRow > 0 && this.PageSize > 0)
                return (int)Math.Ceiling((decimal)this.TotalRow / this.PageSize);
            else
                return 0;
        }
    }
}

 

在IServiceCollection容器中注册:

services.AddScoped(typeof(IRepository<>), typeof(Repository<>));

你可能感兴趣的:(基于Dapper的泛型Repository)