asp.net zero 8.2 学习-4-创建接口及服务

上一节,在框架添加了实体,这一节,写接口及服务类,实现实体的增删改查:

  1. 创建接口: SIS.Application.Shared层
  2. 创建DTO: SIS.Application.Shared层,对应的Dto文件夹
  3. 创建Dto映射Mapper: SIS.Application层CustomDtoMapper.cs
  4. 创建服务层:SIS.Application层

创建接口

在SIS.Application.Shared层创建Demo文件夹,并创建接口文件:IDemoObjectAppService,接口定义了增删改查及获取数据列表的方法

using Abp.Application.Services;
using Abp.Application.Services.Dto;
using EDU.SIS.Demo.Dtos;
using System.Threading.Tasks;

namespace EDU.SIS.Demo
{
    /// <summary>
    /// DemoObject 应用接口
    /// </summary>
    public interface IDemoObjectAppService:IApplicationService
    {
        /// <summary>
        /// 分页查询所有实体
        /// </summary>
        /// <param name="input">分页排序筛选</param>
        /// <returns></returns>
        Task<PagedResultDto<GetDemoObjectForViewDto>> GetAll(GetAllDemoObjectInput input);

        /// <summary>
        /// 创建和修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task CreateOrEdit(CreateOrEditDemoObjectDto input);

        /// <summary>
        /// 获取修改数据详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<GetDemoObjectForEditOutput> GetDemoObjectForEdit(EntityDto input);

        /// <summary>
        /// 数据删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task Delete(EntityDto input);

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<GetDemoObjectForViewDto> GetDemoObjectForView(int id);

    }
}

创建DTO

在SIS.Application.Shared层的Demo文件夹下创建Dto文件夹,并创建IDemoObjectAppService中使用的Dto,注意Dto的命名:
CreateOrEditDemoObjectDto继承自EntityDto<int?>,其中EntityDto里面只包含ID,可以通过判断ID是否为空来确定是创建或编辑状态。
GetAllDemoObjectInput继承自PagedAndSortedResultRequestDto,返回的包括实体集合及分页,过滤信息

CreateOrEditDemoObjectDto:

using Abp.Application.Services.Dto;
using System;
using System.ComponentModel.DataAnnotations;

namespace EDU.SIS.Demo.Dtos
{
    public class CreateOrEditDemoObjectDto:EntityDto<int?>
    {
        /// <summary>
        /// 姓名
        /// </summary>
        [Required]
        [StringLength(DemoObjectConsts.MaxNameLength)]
        public string Name { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }
        /// <summary>
        /// 价格
        /// </summary>
        public double Price { get; set; }
        /// <summary>
        /// 是否为会员
        /// </summary>
        public bool IsVip { get; set; }
        /// <summary>
        /// 截至时间
        /// </summary>
        public DateTime EndDateTime { get; set; }
    }
}

DemoObjectDto: 
using Abp.Application.Services.Dto;
using System;

namespace EDU.SIS.Demo.Dtos
{
    public class DemoObjectDto:EntityDto
    {
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }
        /// <summary>
        /// 价格
        /// </summary>
        public double Price { get; set; }
        /// <summary>
        /// 是否为会员
        /// </summary>
        public bool IsVip { get; set; }
        /// <summary>
        /// 截至时间
        /// </summary>
        public DateTime EndDateTime { get; set; }

    }
}

GetAllDemoObjectInput: 
using Abp.Application.Services.Dto;

namespace EDU.SIS.Demo.Dtos
{
    public class GetAllDemoObjectInput:PagedAndSortedResultRequestDto
    {
        // 模糊查询过滤器
        public string Filter { get; set; }
        //特定字段查询过滤器
        public string NameFilter { get; set; }
    }
}

GetDemoObjectForEditOutput:
namespace EDU.SIS.Demo.Dtos
{
    public class GetDemoObjectForEditOutput
    {
        public DemoObjectDto DemoObject { get; set; }
    }
}

GetDemoObjectForViewDto:
namespace EDU.SIS.Demo.Dtos
{
    public class GetDemoObjectForViewDto
    {
        public DemoObjectDto DemoObject { get; set; }
    }
}


创建Dto映射Mapper

在SIS.Application层CustomDtoMapper.cs中对Dto和实体添加映射关系,这里是集中配置,还可以参考官方文档,在Dto类中使用特性配置

configuration.CreateMap<DemoObject, DemoObjectDto>();
configuration.CreateMap<CreateOrEditDemoObjectDto, DemoObject>();

创建服务层

在SIS.Application层,添加Demo文件夹,并创建DemoObjectAppService类,继承SISAppServiceBase类,实现IDemoObjectAppService接口:

using Abp.Application.Services.Dto;
using EDU.SIS.Demo.Dtos;
using System;
using System.Threading.Tasks;

namespace EDU.SIS.Demo
{
    public class DemoObjectAppService : SISAppServiceBase, IDemoObjectAppService
    {
        /// <summary>
        /// 创建和修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task CreateOrEdit(CreateOrEditDemoObjectDto input)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 数据删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task Delete(EntityDto input)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 分页查询所有实体
        /// </summary>
        /// <param name="input">分页排序筛选</param>
        /// <returns></returns>
        public Task<PagedResultDto<GetDemoObjectForViewDto>> GetAll(GetAllDemoObjectInput input)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取修改数据详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Task<GetDemoObjectForEditOutput> GetDemoObjectForEdit(EntityDto input)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<GetDemoObjectForViewDto> GetDemoObjectForView(int id)
        {
            throw new NotImplementedException();
        }
    }
}

原文地址:https://www.cnblogs.com/AlexanderZhao/p/12878781.html