Na seção anterior, uma entidade foi adicionada à estrutura.Nesta seção, escreva interfaces e classes de serviço para adicionar, excluir e modificar entidades:
- Criar interface: camada SIS.Application.Shared
- Criar camada DTO: SIS.Application.Shared, pasta Dto correspondente
- Criar mapeador de Dto: camada SIS.Application CustomDtoMapper.cs
- Crie uma camada de serviço: camada SIS.Application
Criar interface
Crie uma pasta Demo na camada SIS.Application.Shared e crie um arquivo de interface: IDemoObjectAppService, a interface define o método de adicionar, excluir, modificar, verificar e obter a lista de dados
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);
}
}
Criar DTO
Crie uma pasta Dto na pasta Demo na camada SIS.Application.Shared e crie o Dto usado no IDemoObjectAppService. Observe o nome do Dto:
CreateOrEditDemoObjectDto herda de EntityDto <int?>, Onde EntityDto contém apenas o ID, você pode julgar o ID Se está vazio para determinar se foi criado ou editado.
GetAllDemoObjectInput herda de PagedAndSortedResultRequestDto, o retornado inclui coleção e paginação de entidades, filtrando informações
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; }
}
}
Criar mapeador Dto
Adicione relação de mapeamento entre Dto e entidade no CustomDtoMapper.cs na camada SIS.Application, aqui está a configuração centralizada, você também pode consultar documentos oficiais, usar a configuração de recursos na classe Dto
configuration.CreateMap<DemoObject, DemoObjectDto>();
configuration.CreateMap<CreateOrEditDemoObjectDto, DemoObject>();
Crie uma camada de serviço
Na camada SIS.Application, adicione a pasta Demo e crie a classe DemoObjectAppService, herde a classe SISAppServiceBase e implemente a interface 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();
}
}
}