创建ABP模块(四)应用层
2020-11-19 本文已影响0人
寻找无名的特质
现在我们创建模块的应用层,首先在项目ZL.MyFirstModule.Application.Contracts中创建Poets目录,在这个目录中创建PoetDto:
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;
namespace ZL.MyFirstModule.Poets
{
public class PoetDto : AuditedEntityDto<Guid>
{
public string Name { get; set; }
public string Description { get; set; }
}
}
还有创建对象的Dto:
using System.ComponentModel.DataAnnotations;
namespace ZL.MyFirstModule.Poets
{
public class CreateUpdatePoetDto
{
[Required]
[StringLength(128)]
public string Name { get; set; }
public string Description { get; set; }
}
}
以及服务接口:
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
namespace ZL.MyFirstModule.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
{
}
}
在ZL.MyFirstModule.Application中创建子目录Poets,在这个目录中创建服务的实现:
namespace ZL.MyFirstModule.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)
{
}
}
}
还需要在MyFirstModuleApplicationAutoMapperProfile创建Dto的映射:
CreateMap<Poet, PoetDto>();
CreateMap<CreateUpdatePoetDto, Poet>();
我们还需要修改MyFirstModuleApplicationModule中的映射代码:
using Volo.Abp.Modularity;
using Volo.Abp.Application;
namespace ZL.MyFirstModule
{
[DependsOn(
typeof(MyFirstModuleDomainModule),
typeof(MyFirstModuleApplicationContractsModule),
typeof(AbpDddApplicationModule),
typeof(AbpAutoMapperModule)
)]
public class MyFirstModuleApplicationModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
// context.Services.AddAutoMapperObjectMapper<MyFirstModuleApplicationModule>();
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<MyFirstModuleApplicationModule>();
// options.AddMaps<MyFirstModuleApplicationModule>(validate: true);
});
}
}
}
这些工作完成后,就可以运行应用,并使用swagger测试应用层了:
图片.png