使用ABP CLI创建Web 应用(六)—— 应用层

现在我们来创建应用层,首先在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

应用层编写完成了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容