ABP入门教程8 - 应用层创建应用服务
创建目录
在应用层(即JD.CRS.Application)下创建文件夹Course //用以存放Course相关应用服务
在JD.CRS.Application/Course下创建文件夹Dto //用以存放Course相关数据传输对象
创建数据传输对象
在JD.CRS.Application/Course/Dto下创建两个Dto
只读对象
CourseDto.cs //用于查询Course对象
贴上AutoMapFrom的特性
[AutoMapFrom(typeof(Entitys.Course))]
1 using Abp.Application.Services.Dto; 2 using Abp.AutoMapper; 3 using System; 4 using System.ComponentModel.DataAnnotations; 5 6 namespace JD.CRS.Course.Dto 7 { 8 9 [AutoMapFrom(typeof(Entitys.Course))] 10 public class CourseDto : EntityDto<int> 11 { 12 ///13 /// 课程编号 14 /// 15 [StringLength(50)] 16 public string Code { get; set; } 17 /// 18 /// 院系编号 19 /// 20 [StringLength(50)] 21 public string DepartmentCode { get; set; } 22 /// 23 /// 课程名称 24 /// 25 [StringLength(150)] 26 public string Name { get; set; } 27 /// 28 /// 课程积分 29 /// 30 [Range(0, 5)] 31 public int Credits { get; set; } 32 /// 33 /// 备注 34 /// 35 [StringLength(200)] 36 public string Remarks { get; set; } 37 /// 38 /// 状态: 0 正常, 1 废弃 39 /// 40 public int? Status { get; set; } 41 /// 42 /// 创建日期 43 /// 44 public DateTime? CreateDate { get; set; } 45 /// 46 /// 创建人 47 /// 48 [StringLength(50)] 49 public string CreateName { get; set; } 50 /// 51 /// 修改日期 52 /// 53 public DateTime? UpdateDate { get; set; } 54 /// 55 /// 修改人 56 /// 57 [StringLength(50)] 58 public string UpdateName { get; set; } 59 60 public DateTime CreationTime { get; set; } 61 } 62 }
可写对象
CreateUpdateCourseDto.cs //用于创建/修改Course对象
贴上AutoMapTo的特性
[AutoMapTo(typeof(Entitys.Course))]
1 using Abp.Application.Services.Dto; 2 using Abp.AutoMapper; 3 using System; 4 using System.ComponentModel.DataAnnotations; 5 6 namespace JD.CRS.Course.Dto 7 { 8 9 [AutoMapTo(typeof(Entitys.Course))] 10 public class CreateUpdateCourseDto : EntityDto<int> 11 { 12 ///13 /// 课程编号 14 /// 15 [StringLength(50)] 16 public string Code { get; set; } 17 /// 18 /// 院系编号 19 /// 20 [StringLength(50)] 21 public string DepartmentCode { get; set; } 22 /// 23 /// 课程名称 24 /// 25 [StringLength(150)] 26 public string Name { get; set; } 27 /// 28 /// 课程积分 29 /// 30 [Range(0, 5)] 31 public int Credits { get; set; } 32 /// 33 /// 备注 34 /// 35 [StringLength(200)] 36 public string Remarks { get; set; } 37 /// 38 /// 状态: 0 正常, 1 废弃 39 /// 40 public int? Status { get; set; } 41 /// 42 /// 创建日期 43 /// 44 public DateTime? CreateDate { get; set; } 45 /// 46 /// 创建人 47 /// 48 [StringLength(50)] 49 public string CreateName { get; set; } 50 /// 51 /// 修改日期 52 /// 53 public DateTime? UpdateDate { get; set; } 54 /// 55 /// 修改人 56 /// 57 [StringLength(50)] 58 public string UpdateName { get; set; } 59 60 public DateTime CreationTime { get; set; } 61 } 62 }
创建应用服务接口
在JD.CRS.Application/Course下新建项/接口
ICourseAppService.cs
1 using Abp.Application.Services; 2 using Abp.Application.Services.Dto; 3 using JD.CRS.Course.Dto; 4 5 namespace JD.CRS.Course 6 { 7 public interface ICourseAppService : IAsyncCrudAppService<//定义了CRUD方法 8 CourseDto, //用来展示课程 9 int, //Course实体的主键 10 PagedResultRequestDto, //获取课程的时候用于分页 11 CreateUpdateCourseDto, //用于创建课程 12 CreateUpdateCourseDto> //用于更新课程 13 { 14 } 15 }
创建应用服务
在JD.CRS.Application/Course下新建项/类
CourseAppService.cs
1 using Abp.Application.Services; 2 using Abp.Application.Services.Dto; 3 using Abp.Domain.Repositories; 4 using JD.CRS.Course.Dto; 5 using System.Threading.Tasks; 6 7 namespace JD.CRS.Course 8 { 9 public class CourseAppService : AsyncCrudAppServiceint, PagedResultRequestDto, 10 CreateUpdateCourseDto, CreateUpdateCourseDto>, ICourseAppService 11 12 { 13 public CourseAppService(IRepository int> repository) 14 : base(repository) 15 { 16 17 } 18 19 public override Task Create(CreateUpdateCourseDto input) 20 { 21 var sin = input; 22 return base.Create(input); 23 } 24 } 25 }