上一節,在框架添加了實體,這一節,寫接口及服務類,實現實體的增刪改查:
- 創建接口: SIS.Application.Shared層
- 創建DTO: SIS.Application.Shared層,對應的Dto文件夾
- 創建Dto映射Mapper: SIS.Application層CustomDtoMapper.cs
- 創建服務層: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();
}
}
}