现在我们来创建应用层,首先在Application.Contracts项目中创建Poets子目录,在这个目录中创建DTO和AppService的接口。我们需要两个Dto,一个负责保存查询数据,另一个负责将传输编辑数据,这两个Dto的代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;
namespace ZL.Test.Poets
{
public class PoetDto : AuditedEntityDto<Guid>
{
public string Name { get; set; }
public string Description { get; set; }
}
}
创建和修改的Dto:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace ZL.Test.Poets
{
public class CreateUpdatePoetDto
{
[Required]
[StringLength(128)]
public string Name { get; set; }
public string Description { get; set; }
}
}
服务接口从ICrudAppService派生:
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
namespace ZL.Test.Poets
{
public interface IPoetAppService : ICrudAppService< //Defines CRUD methods
PoetDto, //Used to show poets
Guid, //Primary key of the poet entity
PagedAndSortedResultRequestDto, //Used for paging/sorting
CreateUpdatePoetDto> //Used to create/update a poet
{
}
}
在Application中创建服务接口的实现,代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
namespace ZL.Test.Poets
{
public class PoetAppService :
CrudAppService<
Poet, //The Poet entity
PoetDto, //Used to show poets
Guid, //Primary key of the book entity
PagedAndSortedResultRequestDto, //Used for paging/sorting
CreateUpdatePoetDto>, //Used to create/update a poet
IPoetAppService
{
public PoetAppService(IRepository<Poet, Guid> repository) : base(repository)
{
}
}
}
还需要在AutoMapperProfile中增加Dto的映射:
using AutoMapper;
using ZL.Test.Poets;
namespace ZL.Test
{
public class TestApplicationAutoMapperProfile : Profile
{
public TestApplicationAutoMapperProfile()
{
/* You can configure your AutoMapper mapping configuration here.
* Alternatively, you can split your mapping configurations
* into multiple profile classes for a better organization. */
CreateMap<Poet, PoetDto>();
CreateMap<CreateUpdatePoetDto, Poet>();
}
}
}
到这里,应用层编写完成,ABP框架可以根据服务自动生成api的控制器,我们现在就可以运行应用,并使用swagger测试这些服务。运行应用,然后导航到swagger:
图片.png
我们可以看到,服务中增加了Poet,我们可以测试一下Post:
图片.png
我们输入了“李白”,返回显示已经输入成功,这个新记录的id是9ba46fce-7b44-77f7-9e3d-39f84b6d3ec8。我们可以通过浏览器访问这条记录,输入地址https://localhost:44362/api/app/poet/9ba46fce-7b44-77f7-9e3d-39f84b6d3ec8,显示结果如下:
图片.png
应用层编写完成了。