• asp.net zero 8.2 学习-5-实现增删改查服务及API测试


    在asp.net zero 中实现单表增删改查:

    编写增删改查服务方法
    API测试服务

    删除实体

    注意在删除实体Delete(EntityDto input),可以先查询再修改,也可以直接删除实体,删除实体注意要转换成主键类型

    public async Task Delete(EntityDto input)
    {
        //先查询再修改
        //var demoObject = await _demoObjectRepository.GetAsync(input.Id);
        //await _demoObjectRepository.DeleteAsync(demoObject);
    
        //直接删除实体,注意转换id,不转换则删不了
        await _demoObjectRepository.DeleteAsync((int)input.Id);
    }
    
    

    查询实体

    在分页查询数据集合时,需要先排序再映射,可能是Entity Framework 3.1的问题,也可能是System.Linq.Dynamic.Core程序集的问题,如果先映射再排序分页,就会出现错误:具体原因还没找到,另外一点CountAsync需要引用using Abp.Linq.Extensions; OrderBy动态查询需要引用using System.Linq.Dynamic.Core;

    查询实体报的错误:

    下面是修改后的代码(先排序再映射):

    public async Task<PagedResultDto<GetDemoObjectForViewDto>> GetAll(GetAllDemoObjectInput input)
    {
        //注意这里要用Abp.Linq.Extensions,否则返回的是IEnumerable类型
        var filter = _demoObjectRepository.GetAll()
            .WhereIf(!string.IsNullOrWhiteSpace(input.Filter), e => e.Name.Contains(input.Filter))
            .WhereIf(!string.IsNullOrWhiteSpace(input.NameFilter), e => e.Name.Equals(input.NameFilter));
    
        //先排序,再映射
        filter = filter.OrderBy(input.Sorting ?? "id asc");//OrderBy来自System.Linq.Dynamic.Core
    
        var query = (from o in filter
                        orderby o.Id
                        select new GetDemoObjectForViewDto()
                        {
                            DemoObject = ObjectMapper.Map<DemoObjectDto>(o)
                        });
    
        var totalCount = await query.CountAsync(); //CountAsync来自Abp.Linq.Extensions
    
        var demoObject = await query
            .PageBy(input)
            .ToListAsync();
    
    
        var result = new PagedResultDto<GetDemoObjectForViewDto>()
        {
            TotalCount = totalCount,
            Items = demoObject
        };
        return result;
    
    }
    

    下面是整个服务类:

    using Abp.Application.Services.Dto;
    using Abp.Collections.Extensions;
    using Abp.Domain.Repositories;
    using EDU.SIS.Demo.Dtos;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.EntityFrameworkCore;
    using Abp.Linq.Extensions;
    using System.Linq.Dynamic.Core;
    
    namespace EDU.SIS.Demo
    {
        public class DemoObjectAppService : SISAppServiceBase, IDemoObjectAppService
        {
            private readonly IRepository<DemoObject> _demoObjectRepository;
            public DemoObjectAppService(IRepository<DemoObject> demoObjectRepository)
            {
                _demoObjectRepository = demoObjectRepository;
            }
    
            /// <summary>
            /// 创建和修改
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            public async Task CreateOrEdit(CreateOrEditDemoObjectDto input)
            {
                if (input.Id==null)
                {
                    //创建
                   await Create(input);
                }
                else
                {
                    //修改
                    await Update(input);
                }
            }
    
            /// <summary>
            /// 创建实体
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            private async Task Create(CreateOrEditDemoObjectDto input)
            {
                var demoObject = ObjectMapper.Map<DemoObject>(input);
                await  _demoObjectRepository.InsertAsync(demoObject);
            }
    
            /// <summary>
            /// 修改实体
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            private async Task Update(CreateOrEditDemoObjectDto input)
            {
                var demoObject = await _demoObjectRepository.FirstOrDefaultAsync((int)input.Id);
                var demo = ObjectMapper.Map(input, demoObject);
    
                //await _demoObjectRepository.UpdateAsync(demo);
            }
    
            /// <summary>
            /// 数据删除
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            public async Task Delete(EntityDto input)
            {
                var demoObject = await _demoObjectRepository.GetAsync(input.Id);
                await _demoObjectRepository.DeleteAsync(demoObject);
            }
    
            /// <summary>
            /// 分页查询所有实体
            /// </summary>
            /// <param name="input">分页排序筛选</param>
            /// <returns></returns>
            public async Task<PagedResultDto<GetDemoObjectForViewDto>> GetAll(GetAllDemoObjectInput input)
            {
                //注意这里要用Abp.Linq.Extensions,否则返回的是IEnumerable类型
                var filter = _demoObjectRepository.GetAll()
                    .WhereIf(!string.IsNullOrWhiteSpace(input.Filter), e => e.Name.Contains(input.Filter))
                    .WhereIf(!string.IsNullOrWhiteSpace(input.NameFilter), e => e.Name.Equals(input.NameFilter));
    
                //先排序,再映射
                filter = filter.OrderBy(input.Sorting ?? "id asc");//OrderBy来自System.Linq.Dynamic.Core
    
                var query = (from o in filter
                             orderby o.Id
                             select new GetDemoObjectForViewDto()
                             {
                                 DemoObject = ObjectMapper.Map<DemoObjectDto>(o)
                             });
    
                var totalCount = await query.CountAsync(); //CountAsync来自Abp.Linq.Extensions
    
                var demoObject = await query
                    .PageBy(input)
                    .ToListAsync();
    
    
                var result = new PagedResultDto<GetDemoObjectForViewDto>()
                {
                    TotalCount = totalCount,
                    Items = demoObject
                };
                return result;
    
            }
    
            /// <summary>
            /// 获取修改数据详情
            /// </summary>
            /// <param name="input"></param>
            /// <returns></returns>
            public async Task<GetDemoObjectForEditOutput> GetDemoObjectForEdit(EntityDto input)
            {
                var demoObject = await _demoObjectRepository.GetAsync(input.Id);
                var result = new GetDemoObjectForEditOutput()
                {
                    DemoObject = ObjectMapper.Map<DemoObjectDto>(demoObject)
                };
                return result;
            }
    
            /// <summary>
            /// 获取单条数据
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public async Task<GetDemoObjectForViewDto> GetDemoObjectForView(int id)
            {
                var demoObject = await _demoObjectRepository.GetAsync(id);
                var result = new GetDemoObjectForViewDto()
                {
                   DemoObject =  ObjectMapper.Map<DemoObjectDto>(demoObject)
                };
                return result;
            }
        }
    }
    
    

    API测试

    分页查询:

    创建实体

    修改实体

    删除实体

    在删除实体时,无法用post,提示405错误,还没找到原因,可能是需要传递http头部参数,这里只能用delete方法,可以删除实体:

    查询单个实体

  • 相关阅读:
    使用TCP
    socket基本
    windows内核原理及实现-----进程线程管理------3.4节中 windows中进程句柄
    windows内核原理及实现-----进程线程管理------3.3节中 windows中进程线程的数据结构
    winDBG用法
    windows调试异常机制
    MYSQL5上运行多个实例
    Mysql双主实战
    Mysql-Proxy使用
    新的旅程
  • 原文地址:https://www.cnblogs.com/AlexanderZhao/p/12878777.html
Copyright © 2020-2023  润新知