ABP框架8——仓储的作用及其基础Demo

一、使用仓储的好处

  • 1.提高CRUD接口复用性
  • 2.解耦业务逻辑(BLL)和增删改查(CRUD),换ORM特别方便,不需要改应用层,直接改仓储层
  • 3.做复杂查询
  • 4.事务支持

二、Demo

cs 复制代码
public class BookRepository : IBookRepository, ITransientDependency  //接口依赖注入
{
    private readonly IFreeSql _fsql;

    public BookRepository(IFreeSql fsql)
    {
        _fsql = fsql;
    }

    /// <summary>
    /// 获取FreeSql
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public IFreeSql GetFreeSql()
    {
        return _fsql;
    }

    /// <summary>
    /// 分页查询:Book
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [Route(nameof(GetBookPageListAsync))]
    public async Task<IPageList<BookDto>> GetBookPageListAsync(BookSerachInput input)
    {
        var output = new PageList<BookDto>(new List<BookDto>(), input.PageIndex, input.maxResultCount);
        if (false)
        {
            return output;//不符合要求,返回空数据
        }
        var fsql = GetFreeSql();
        //查表
        var queryList = await fsql.Select<Book>()
                        .Where(x => x.TimePoint.Between(input.StartTime, input.EndTime))
                        .WhereIf(input.StationCodes.Any(), x => input.StationCodes.Contains(x.CityCode))
                        .OrderBy(!string.IsNullOrEmpty(input.orderby), input.orderby)
                        .Page(input.PageIndex, input.maxResultCount)
                        .Count(out var totalCount)
                        .ToListAsync();
        //映射结果
        var dtoList = _objectMapper.Map<List<Book>, List<BookDto>>(queryList);
        return new PageList<BookDto>(dtoList, input.PageIndex, input.maxResultCount, totalCount);
    }

    /// <summary>
    /// Book 单一实体 修改
    /// </summary>
    public async Task<bool> UpdateBookAsync(BookDto input)
    {
        var fsql = GetFreeSql();
        return fsql.Update<BookDto>(input).ExecuteAffrows() != 0;
    }

    /// <summary>
    /// Book 单一实体 保存
    /// </summary>
    public async Task<bool> SaveBookAsync(BookDto input)
    {
        var fsql = GetFreeSql();
        bool isExist = await fsql.Select<Book>().AnyAsync(x => x.Id == input.Id);//【根据实际修改】根据(联合)主键确定唯一性
        if (isExist)
        {
            return await UpdateBookAsync(input);
        }
        return await InsertBookAsync(input);
    }

    /// <summary>
    /// Book 单一实体 插入
    /// </summary>
    public async Task<bool> InsertBookAsync(BookDto input)
    {
        var fsql = GetFreeSql();
        return fsql.Insert(input).ExecuteAffrows() != 0;
    }

    /// <summary>
    /// Book 单一实体 删除
    /// </summary>
    public async Task<bool> DeleteBookAsync(BookDto input)
    {
        var fsql = GetFreeSql();
        return fsql.Delete<BookDto>(input).ExecuteAffrows() != 0;
    }

    /// <summary>
    /// Book 列表 保存
    /// </summary>
    public async Task<bool> SaveBookListAsync(List<BookDto> inputList)
    {
        bool result = true;
        var fsql = GetFreeSql();

        //获取输入主键
        var inputKeyList = inputList.Select(x => x.Id ).Distinct().ToList();

        //根据输入主键获取已存在实体
        var existList = await fsql.Select<Book>().Where(x => inputKeyList.Contains(x.Id)).ToListAsync();

        //获取已存在实体的主键列表
        var existKeyList = existList.Select(x => x.Id).Distinct().ToList();

        // 获取更新列表和插入列表
        var updateList = inputList.Where(x => existKeyList.Contains(x.Id)).ToList();
        var insertList = inputList.Where(x => !existKeyList.Contains(x.Id)).ToList();

        /*联合主键:
            //获取输入主键
            var inputKeyList = inputList.Select(x => new { x.Id , x.BookName }).Distinct().ToList();
            //根据输入主键获取已存在实体
            var existList = await fsql.Select<Book>().Where(x=> inputKeyList.Any(key=>key.Id == x.Id && key.BookName == x.BookName)).ToListAsync();
            //获取已存在实体的主键列表
            var existKeyList = existList.Select(x => new { x.Id, x.BookName }).Distinct().ToList();
            //获取更新列表和插入列表
            var updateList = inputList.Where(x => existKeyList.Contains(new { x.Id, x.BookName })).ToList();
            var insertList = inputList.Where(x => !existKeyList.Contains(new { x.Id, x.BookName })).ToList();
       */

        // 使用批量更新和批量插入方法
        bool updateSuccess = await UpdateBookListAsync(updateList);  // 批量更新
        bool insertSuccess = await InsertBookListAsync(insertList);  // 批量插入

        return updateSuccess && insertSuccess; // 返回是否全部成功
    }

    /// <summary>
    /// Book 列表 修改
    /// </summary>
    public async Task<bool> UpdateBookListAsync(List<BookDto> input)
    {
        var fsql = GetFreeSql();
        return fsql.Update<BookDto>(input).ExecuteAffrows() != 0;
    }

    /// <summary>
    /// Book 列表 插入
    /// </summary>
    public async Task<bool> InsertBookListAsync(List<BookDto> input)
    {
        var fsql = GetFreeSql();
        return fsql.Insert(input).ExecuteAffrows() != 0;
    }

    /// <summary>
    /// Book 列表 删除
    /// </summary>
    public async Task<bool> DeleteBookListAsync(List<BookDto> input)
    {
        var fsql = GetFreeSql();
        return fsql.Delete<BookDto>(input).ExecuteAffrows() != 0;
    }

}
相关推荐
ahauedu6 分钟前
CentOS 环境下 MySQL 数据库全部备份的操作指南
数据库·mysql·centos
張萠飛1 小时前
Redis如何判断哨兵模式下节点之间数据是否一致
数据库·redis·缓存
北漂老男孩1 小时前
MySQL、Oracle 和 PostgreSQL 是三种主流的关系型数据库的主要原理性差异分析
数据库·mysql·postgresql·oracle
小小鸭程序员1 小时前
零基础教程:Windows电脑安装Linux系统(双系统/虚拟机)全攻略
linux·运维·服务器·mysql·spring
GalaxyPokemon3 小时前
MySQL基础 [六] - 内置函数+复合查询+表的内连和外连
linux·运维·数据库·mysql·ubuntu
Linux运维老纪3 小时前
Linux 命令清单(Linux Command List)
linux·运维·服务器·数据库·mysql·云计算·运维开发
可观测性用观测云3 小时前
阿里巴巴 Druid 可观测性最佳实践
数据库
代码吐槽菌4 小时前
基于微信小程序的智慧乡村旅游服务平台【附源码】
java·开发语言·数据库·后端·微信小程序·小程序·毕业设计
喝醉酒的小白4 小时前
数据库如何确定或计算 LSN(日志序列号)
数据库
dengjiayue4 小时前
使用 redis 实现消息队列
数据库·redis·缓存