我们将封装通用的应用层,接口以及控制器基类。
创建接口ICurdAppService,在这里我们定义了通用的增删改查接口。
其中的泛型参数:
public interface ICurdAppService
{
Task GetAsync(TKey id);
Task> GetAllAsync(TGetListInput input);
Task CreateAsync(TCreateInput input);
Task UpdateAsync(TUpdateInput input);
Task DeleteAsync(TKey id);
}
创建应用层服务CurdAppServiceBase,它是一个抽象类,继承自Abp.Application.Services.CrudAppService。
代码如下:
public abstract class CurdAppServiceBase
: CrudAppService
where TEntity : class, IEntity
where TGetOutputDto : IEntityDto
where TGetListOutputDto : IEntityDto
{
protected CurdAppServiceBase(IRepository repository)
: base(repository)
{
}
}
创建控制器类CurdController,继承自AbpControllerBase。并实现ICurdAppService接口。
代码如下:
public abstract class CurdController
: AbpControllerBase
where ITAppService : ICurdAppService
where TGetOutputDto : IEntityDto
where TGetListOutputDto : IEntityDto
{
private readonly ITAppService _recipeAppService;
public CurdController(ITAppService recipeAppService)
{
_recipeAppService = recipeAppService;
}
[HttpPost]
[Route("Create")]
public virtual async Task CreateAsync(TCreateInput input)
{
return await _recipeAppService.CreateAsync(input);
}
[HttpDelete]
[Route("Delete")]
public virtual async Task DeleteAsync(TKey id)
{
await _recipeAppService.DeleteAsync(id);
}
[HttpGet]
[Route("GetAll")]
public virtual async Task> GetAllAsync(TGetListInput input)
{
return await _recipeAppService.GetAllAsync(input);
}
[HttpGet]
[Route("Get")]
public virtual async Task GetAsync(TKey id)
{
return await _recipeAppService.GetAsync(id);
}
[HttpPut]
[Route("Update")]
public virtual async Task UpdateAsync(TUpdateInput input)
{
return await _recipeAppService.UpdateAsync(input);
}
}
为了兼容旧版Abp,需更改增删查改服务(CrudAppService)的方法签名,可参考[Volo.Abp升级笔记]使用旧版Api规则替换RESTful Api以兼容老程序,此处不再赘述。
将UpdateAsync,GetListAsync方法封闭:
private new Task UpdateAsync(TKey id, TUpdateInput input)
{
return base.UpdateAsync(id, input);
}
private new Task> GetListAsync(TGetListInput input)
{
return base.GetListAsync(input);
}
封闭原有UpdateAsync, 新增的UpdateAsync方法更改了方法签名:
public virtual async Task UpdateAsync(TUpdateInput input)
{
await CheckUpdatePolicyAsync();
var entity = await GetEntityByIdAsync((input as IEntityDto).Id);
MapToEntity(input, entity);
await Repository.UpdateAsync(entity, autoSave: true);
return await MapToGetOutputDtoAsync(entity);
}
Brief是一种简化的查询实体集合的方法,其返回的Dto不包含导航属性,以减少数据传输量。
新增GetAllAsync和GetAllBriefAsync方法:
public virtual Task> GetAllAsync(TGetListInput input)
{
return this.GetListAsync(input);
}
public async Task> GetAllBriefAsync(TGetListBriefInput input)
{
await CheckGetListPolicyAsync();
var query = await CreateBriefFilteredQueryAsync(input);
var totalCount = await AsyncExecuter.CountAsync(query);
var entities = new List();
var entityDtos = new List();
if (totalCount > 0)
{
query = ApplySorting(query, input);
query = ApplyPaging(query, input);
entities = await AsyncExecuter.ToListAsync(query);
entityDtos = ObjectMapper.Map, List>(entities);
}
return new PagedResultDto(
totalCount,
entityDtos
);
}
目前为止,我们的应用层基类继承于Abp.Application.Services.CrudAppService
为了更好的代码重用,我们对泛型参数进行扩展,使用CurdAppServiceBase的类可根据实际业务需求选择泛型参数
其中的泛型参数:
首先扩展ICurdAppService:
public interface ICurdAppService
: ICurdAppService
{
}
public interface ICurdAppService
: ICurdAppService
{
}
public interface ICurdAppService
: ICurdAppService
{
}
public interface ICurdAppService
: ICurdAppService
{
}
public interface ICurdAppService
: ICurdAppService
{
}
public interface ICurdAppService
: ICurdAppService
{
}
public interface ICurdAppService
{
Task GetAsync(TKey id);
Task> GetAllAsync(TGetListInput input);
Task CreateAsync(TCreateInput input);
Task UpdateAsync(TUpdateInput input);
Task DeleteAsync(TKey id);
Task> GetAllBriefAsync(TGetListInput input);
}
扩展CurdAppServiceBase:
public abstract class CurdAppServiceBase
: CurdAppServiceBase
where TEntity : class, IEntity
where TEntityDto : IEntityDto
{
protected CurdAppServiceBase(IRepository repository)
: base(repository)
{
}
}
public abstract class CurdAppServiceBase
: CurdAppServiceBase
where TEntity : class, IEntity
where TEntityDto : IEntityDto
{
protected CurdAppServiceBase(IRepository repository)
: base(repository)
{
}
}
public abstract class CurdAppServiceBase
: CurdAppServiceBase
where TEntity : class, IEntity
where TEntityDto : IEntityDto
{
protected CurdAppServiceBase(IRepository repository)
: base(repository)
{
}
}
public abstract class CurdAppServiceBase
: CurdAppServiceBase
where TEntity : class, IEntity
where TEntityDto : IEntityDto
{
protected CurdAppServiceBase(IRepository repository)
: base(repository)
{
}
protected override Task MapToGetListOutputDtoAsync(TEntity entity)
{
return MapToGetOutputDtoAsync(entity);
}
protected override TEntityDto MapToGetListOutputDto(TEntity entity)
{
return MapToGetOutputDto(entity);
}
}
public abstract class CurdAppServiceBase
: CurdAppServiceBase
where TEntity : class, IEntity
where TGetOutputDto : IEntityDto
where TGetListOutputDto : IEntityDto
{
protected CurdAppServiceBase(IRepository repository)
: base(repository)
{
}
}
public abstract class CurdAppServiceBase
: CurdAppServiceBase
where TEntity : class, IEntity
where TGetOutputDto : IEntityDto
where TGetListOutputDto : IEntityDto
{
protected CurdAppServiceBase(IRepository repository)
: base(repository)
{
}
}
扩展CurdController
public abstract class CurdController
: CurdController
where ITAppService : ICurdAppService
where TEntityDto : IEntityDto
{
protected CurdController(ITAppService appService)
: base(appService)
{
}
}
public abstract class CurdController
: CurdController
where ITAppService : ICurdAppService
where TEntityDto : IEntityDto
{
protected CurdController(ITAppService appService)
: base(appService)
{
}
}
public abstract class CurdController
: CurdController
where ITAppService : ICurdAppService
where TEntityDto : IEntityDto
{
protected CurdController(ITAppService appService)
: base(appService)
{
}
}
public abstract class CurdController
: CurdController
where ITAppService : ICurdAppService
where TEntityDto : IEntityDto
{
protected CurdController(ITAppService appService)
: base(appService)
{
}
}
public abstract class CurdController
: CurdController
where ITAppService : ICurdAppService
where TGetOutputDto : IEntityDto
where TGetListOutputDto : IEntityDto
{
protected CurdController(ITAppService appService)
: base(appService)
{
}
}
public abstract class CurdController
: CurdController
where ITAppService : ICurdAppService
where TGetOutputDto : IEntityDto
where TGetListOutputDto : IEntityDto
where TGetListBriefInput : TGetListInput
{
protected CurdController(ITAppService appService)
: base(appService)
{
}
}
public abstract class CurdController
: AbpControllerBase
where ITAppService : ICurdAppService
where TGetOutputDto : IEntityDto
where TGetListOutputDto : IEntityDto
where TGetListBriefInput : TGetListInput
{
private readonly ITAppService _recipeAppService;
public CurdController(ITAppService recipeAppService)
{
_recipeAppService = recipeAppService;
}
[HttpPost]
[Route("Create")]
public virtual async Task CreateAsync(TCreateInput input)
{
return await _recipeAppService.CreateAsync(input);
}
[HttpDelete]
[Route("Delete")]
public virtual async Task DeleteAsync(TKey id)
{
await _recipeAppService.DeleteAsync(id);
}
[HttpGet]
[Route("GetAll")]
public virtual async Task> GetAllAsync(TGetListInput input)
{
return await _recipeAppService.GetAllAsync(input);
}
[HttpGet]
[Route("Get")]
public virtual async Task GetAsync(TKey id)
{
return await _recipeAppService.GetAsync(id);
}
[HttpPut]
[Route("Update")]
public virtual async Task UpdateAsync(TUpdateInput input)
{
return await _recipeAppService.UpdateAsync(input);
}
[HttpGet]
[Route("GetAllBrief")]
public virtual async Task> GetAllBriefAsync(TGetListBriefInput input)
{
return await _recipeAppService.GetAllBriefAsync(input);
}
}
在开发业务模块时,我们可以先使用简单的方式提供Curd服务,随着UI复杂度增加,逐步的使用更加复杂的Curd服务。某种程度上来说,即所谓“渐进式”的开发方式。
我们扩展应用层基类,控制器及其接口
以Alarm为例。来实现扩展型Curd服务(ExtendedCurd)。
假设你已完成创建实体、Dto以及配置完成AutoMapper映射
IApplicationService是ABP框架的接口,所有的应用服务都需要继承自此接口。
public interface IAlarmAppService : IExtendedCurdAppService, IApplicationService
{
}
public class AlarmAppService : ExtendedCurdAppServiceBase, IAlarmAppService
{
public AlarmAppService(IRepository basicInventoryRepository) : base(basicInventoryRepository)
{
}
}
[Area(HealthRemoteServiceConsts.ModuleName)]
[RemoteService(Name = HealthRemoteServiceConsts.RemoteServiceName)]
[Route("api/Health/alarm")]
public class AlarmController : ExtendedCurdController, IAlarmAppService
{
private readonly IAlarmAppService _alarmAppService;
public AlarmController(IAlarmAppService alarmAppService) : base(alarmAppService)
{
_alarmAppService = alarmAppService;
}
}
运行程序
可以看到,我们的接口已经包含所有扩展型Curd方法。
下一章我们将实现通用查询接口的按组织架构查询。