怎样优雅地增删查改(四):创建通用查询基类

文章目录

    • 创建通用查询抽象层
    • 创建通用查询应用层基类
    • 创建通用查询控制器基类
    • [可选]替换RESTfulApi
    • 扩展泛型参数
    • 服务的“渐进式”
    • 使用

上一章我们实现了Employee管理模块,Employee的增删改查是通过其应用服务类,继承自Abp.Application.Services.CrudAppService实现的。

我们将封装通用的应用层,接口以及控制器基类。

创建通用查询抽象层

创建接口ICurdAppService,在这里我们定义了通用的增删改查接口。

其中的泛型参数:

  • TGetOutputDto: Get方法返回的实体Dto
  • TGetListOutputDto: GetAll方法返回的实体Dto
  • TKey: 实体的主键
  • TGetListInput: GetAll方法接收的输入参数
  • TCreateInput: Create方法接收的输入参数
  • TUpdateInput: Update方法接收的输入参数
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);
    }
}

[可选]替换RESTfulApi

为了兼容旧版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的类可根据实际业务需求选择泛型参数

其中的泛型参数:

  • TEntity: CRUD操作对应的实体类
  • TEntityDto: GetAll方法返回的实体Dto
  • TKey: 实体的主键
  • TGetListBriefInput: GetAllBrief方法的输入参数
  • TGetListBriefOutputDto: GetAllBrief方法的输出参数

首先扩展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服务。某种程度上来说,即所谓“渐进式”的开发方式。

  • BaseCurd: 基础型,仅包含Create、Update、Delete、Get
  • SimpleCurd: 简单型,包含BaseCurd的所有功能,同时包含GetAll
  • Curd:完整型,包含SimpleCurd的所有功能,同时包含GetAllBrief,GetAllBrief是一种简化的查询实体集合的方法,其返回的Dto不包含导航属性,以减少数据传输量。是最常用的服务类型。
  • ExtendedCurd:扩展型,包含Curd的所有功能,同时包含GetAllBriefWithoutPage,GetAllBriefWithoutPage 适合一些非分页场景,如日历视图,Echarts图表控件等。使用此接口需要注意:由于没有分页限制,需要其他的查询约束条件(比如日期范围),否则会返回大量的数据,影响性能。

我们扩展应用层基类,控制器及其接口

怎样优雅地增删查改(四):创建通用查询基类_第1张图片

怎样优雅地增删查改(四):创建通用查询基类_第2张图片

使用

以Alarm为例。来实现扩展型Curd服务(ExtendedCurd)。

假设你已完成创建实体、Dto以及配置完成AutoMapper映射

  1. 在Health模块的抽象层中,创建接口IAlarmAppService,继承自IExtendedCurdAppService和IApplicationService。

IApplicationService是ABP框架的接口,所有的应用服务都需要继承自此接口。

public interface IAlarmAppService : IExtendedCurdAppService, IApplicationService
{
}
  1. 在Health模块的应用层中,创建AlarmAppService
public class AlarmAppService : ExtendedCurdAppServiceBase, IAlarmAppService
{
    public AlarmAppService(IRepository basicInventoryRepository) : base(basicInventoryRepository)
    {
    }
}

  1. 在Health模块的HttpApi中,创建AlarmController,并实现IAlarmAppService接口
[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;
    }


}

运行程序

怎样优雅地增删查改(四):创建通用查询基类_第3张图片

可以看到,我们的接口已经包含所有扩展型Curd方法。

下一章我们将实现通用查询接口的按组织架构查询。

你可能感兴趣的:(架构,.NET,c#,asp.net,微服务)