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

19 篇文章 2 订阅


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

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

创建通用查询抽象层

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

其中的泛型参数:

  • TGetOutputDto: Get方法返回的实体Dto
  • TGetListOutputDto: GetAll方法返回的实体Dto
  • TKey: 实体的主键
  • TGetListInput: GetAll方法接收的输入参数
  • TCreateInput: Create方法接收的输入参数
  • TUpdateInput: Update方法接收的输入参数
public interface ICurdAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>

{
    Task<TGetOutputDto> GetAsync(TKey id);

    Task<PagedResultDto<TGetListOutputDto>> GetAllAsync(TGetListInput input);

    Task<TGetOutputDto> CreateAsync(TCreateInput input);

    Task<TGetOutputDto> UpdateAsync(TUpdateInput input);

    Task DeleteAsync(TKey id);
}

创建通用查询应用层基类

创建应用层服务CurdAppServiceBase,它是一个抽象类,继承自Abp.Application.Services.CrudAppService。

代码如下:

public abstract class CurdAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto,  TKey, TGetListInput,  TCreateInput, TUpdateInput>
: CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
    where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
    protected CurdAppServiceBase(IRepository<TEntity, TKey> repository)
: base(repository)
    {

    }
}

创建通用查询控制器基类

创建控制器类CurdController,继承自AbpControllerBase。并实现ICurdAppService接口。

代码如下:

public abstract class CurdController<ITAppService, TGetOutputDto, TGetListOutputDto,  TKey, TGetListInput,  TCreateInput, TUpdateInput>
    : AbpControllerBase
where ITAppService : ICurdAppService<TGetOutputDto, TGetListOutputDto,  TKey, TGetListInput,  TCreateInput, TUpdateInput>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{

    private readonly ITAppService _recipeAppService;

    public CurdController(ITAppService recipeAppService)
    {
        _recipeAppService = recipeAppService;
    }

    [HttpPost]
    [Route("Create")]
    
    public virtual async Task<TGetOutputDto> 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<PagedResultDto<TGetListOutputDto>> GetAllAsync(TGetListInput input)
    {
        return await _recipeAppService.GetAllAsync(input);
    }

    [HttpGet]
    [Route("Get")]
    
    public virtual async Task<TGetOutputDto> GetAsync(TKey id)
    {
        return await _recipeAppService.GetAsync(id);
    }

    [HttpPut]
    [Route("Update")]
    
    public virtual async Task<TGetOutputDto> UpdateAsync(TUpdateInput input)
    {
        return await _recipeAppService.UpdateAsync(input);
    }
}

[可选]替换RESTfulApi

为了兼容旧版Abp,需更改增删查改服务(CrudAppService)的方法签名,可参考[Volo.Abp升级笔记]使用旧版Api规则替换RESTful Api以兼容老程序,此处不再赘述。

将UpdateAsync,GetListAsync方法封闭:

private new Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
{
    return base.UpdateAsync(id, input);
}
private new Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
{
    return base.GetListAsync(input);
}


封闭原有UpdateAsync, 新增的UpdateAsync方法更改了方法签名:


public virtual async Task<TGetOutputDto> UpdateAsync(TUpdateInput input)
{
    await CheckUpdatePolicyAsync();
    var entity = await GetEntityByIdAsync((input as IEntityDto<TKey>).Id);
    MapToEntity(input, entity);
    await Repository.UpdateAsync(entity, autoSave: true);
    return await MapToGetOutputDtoAsync(entity);

}

Brief是一种简化的查询实体集合的方法,其返回的Dto不包含导航属性,以减少数据传输量。

新增GetAllAsync和GetAllBriefAsync方法:

public virtual Task<PagedResultDto<TGetListOutputDto>> GetAllAsync(TGetListInput input)
{
    return this.GetListAsync(input);
}


public async Task<PagedResultDto<TGetListBriefOutputDto>> GetAllBriefAsync(TGetListBriefInput input)
{
    await CheckGetListPolicyAsync();

    var query = await CreateBriefFilteredQueryAsync(input);
    var totalCount = await AsyncExecuter.CountAsync(query);

    var entities = new List<TEntity>();
    var entityDtos = new List<TGetListBriefOutputDto>();

    if (totalCount > 0)
    {
        query = ApplySorting(query, input);
        query = ApplyPaging(query, input);

        entities = await AsyncExecuter.ToListAsync(query);

        entityDtos = ObjectMapper.Map<List<TEntity>, List<TGetListBriefOutputDto>>(entities);

    }

    return new PagedResultDto<TGetListBriefOutputDto>(
        totalCount,
        entityDtos
    );

}

扩展泛型参数

目前为止,我们的应用层基类继承于Abp.Application.Services.CrudAppService

为了更好的代码重用,我们对泛型参数进行扩展,使用CurdAppServiceBase的类可根据实际业务需求选择泛型参数

其中的泛型参数:

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

首先扩展ICurdAppService:


public interface ICurdAppService<TEntityDto, in TKey>
    : ICurdAppService<TEntityDto, TKey, PagedAndSortedResultRequestDto>
{

}

public interface ICurdAppService<TEntityDto, in TKey, in TGetListInput>
    : ICurdAppService<TEntityDto, TKey, TGetListInput, TEntityDto>
{

}

public interface ICurdAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput>
    : ICurdAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
{

}


public interface ICurdAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
    : ICurdAppService<TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
{

}


public interface ICurdAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: ICurdAppService<TGetOutputDto,  TGetListOutputDto, TKey, TGetListInput, TGetListInput, TCreateInput, TUpdateInput>
{

}



public interface ICurdAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput, in TGetListBriefInput, in TCreateInput, in TUpdateInput>
: ICurdAppService<TGetOutputDto, TGetListOutputDto, TGetListOutputDto, TKey, TGetListInput, TGetListBriefInput, TCreateInput, TUpdateInput>
{

}



public interface ICurdAppService<TGetOutputDto, TGetListOutputDto, TGetListBriefOutputDto, in TKey, in TGetListInput, in TGetListBriefInput, in TCreateInput, in TUpdateInput>

{
    Task<TGetOutputDto> GetAsync(TKey id);

    Task<PagedResultDto<TGetListOutputDto>> GetAllAsync(TGetListInput input);

    Task<TGetOutputDto> CreateAsync(TCreateInput input);

    Task<TGetOutputDto> UpdateAsync(TUpdateInput input);

    Task DeleteAsync(TKey id);

    Task<PagedResultDto<TGetListBriefOutputDto>> GetAllBriefAsync(TGetListInput input);


}




扩展CurdAppServiceBase:


public abstract class CurdAppServiceBase<TEntity, TEntityDto, TKey>
    : CurdAppServiceBase<TEntity, TEntityDto, TKey, PagedAndSortedResultRequestDto>
    where TEntity : class, IEntity<TKey>
    where TEntityDto : IEntityDto<TKey>
{
    protected CurdAppServiceBase(IRepository<TEntity, TKey> repository)
        : base(repository)
    {

    }
}

public abstract class CurdAppServiceBase<TEntity, TEntityDto, TKey, TGetListInput>
    : CurdAppServiceBase<TEntity, TEntityDto, TKey, TGetListInput, TEntityDto>
    where TEntity : class, IEntity<TKey>
    where TEntityDto : IEntityDto<TKey>
{
    protected CurdAppServiceBase(IRepository<TEntity, TKey> repository)
        : base(repository)
    {

    }
}


public abstract class CurdAppServiceBase<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput>
    : CurdAppServiceBase<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
    where TEntity : class, IEntity<TKey>
    where TEntityDto : IEntityDto<TKey>
{
    protected CurdAppServiceBase(IRepository<TEntity, TKey> repository)
        : base(repository)
    {

    }
}

public abstract class CurdAppServiceBase<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CurdAppServiceBase<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
    protected CurdAppServiceBase(IRepository<TEntity, TKey> repository)
        : base(repository)
    {

    }

    protected override Task<TEntityDto> MapToGetListOutputDtoAsync(TEntity entity)
    {
        return MapToGetOutputDtoAsync(entity);
    }

    protected override TEntityDto MapToGetListOutputDto(TEntity entity)
    {
        return MapToGetOutputDto(entity);
    }
}

public abstract class CurdAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CurdAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
    protected CurdAppServiceBase(IRepository<TEntity, TKey> repository)
        : base(repository)
    {

    }
}


public abstract class CurdAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput,  TCreateInput, TUpdateInput>
: CurdAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto, TGetListOutputDto, TKey, TGetListInput,  TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
    protected CurdAppServiceBase(IRepository<TEntity, TKey> repository)
        : base(repository)
    {

    }
}

扩展CurdController

public abstract class CurdController<ITAppService, TEntityDto, TKey>
        : CurdController<ITAppService, TEntityDto, TKey, PagedAndSortedResultRequestDto>
        where ITAppService : ICurdAppService<TEntityDto, TKey>
        where TEntityDto : IEntityDto<TKey>
{
    protected CurdController(ITAppService appService)
        : base(appService)
    {

    }
}

public abstract class CurdController<ITAppService, TEntityDto, TKey, TGetListInput>
    : CurdController<ITAppService, TEntityDto, TKey, TGetListInput, TEntityDto>
    where ITAppService : ICurdAppService<TEntityDto, TKey, TGetListInput>
    where TEntityDto : IEntityDto<TKey>
{
    protected CurdController(ITAppService appService)
        : base(appService)
    {

    }
}


public abstract class CurdController<ITAppService, TEntityDto, TKey, TGetListInput, TCreateInput>
    : CurdController<ITAppService, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
    where ITAppService : ICurdAppService<TEntityDto, TKey, TGetListInput, TCreateInput>
    where TEntityDto : IEntityDto<TKey>
{
    protected CurdController(ITAppService appService)
        : base(appService)
    {

    }
}



public abstract class CurdController<ITAppService, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CurdController<ITAppService, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where ITAppService : ICurdAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
    where TEntityDto : IEntityDto<TKey>
{
    protected CurdController(ITAppService appService)
        : base(appService)
    {

    }

}




public abstract class CurdController<ITAppService, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CurdController<ITAppService, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TGetListInput, TCreateInput, TUpdateInput>
where ITAppService : ICurdAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
    protected CurdController(ITAppService appService)
        : base(appService)
    {

    }

}



public abstract class CurdController<ITAppService, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TGetListBriefInput, TCreateInput, TUpdateInput>
: CurdController<ITAppService, TGetOutputDto, TGetListOutputDto, TGetListOutputDto, TKey, TGetListInput, TGetListBriefInput, TCreateInput, TUpdateInput>
where ITAppService : ICurdAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TGetListBriefInput, TCreateInput, TUpdateInput>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
where TGetListBriefInput : TGetListInput
{
    protected CurdController(ITAppService appService)
        : base(appService)
    {

    }

}


public abstract class CurdController<ITAppService, TGetOutputDto, TGetListOutputDto, TGetListBriefOutputDto, TKey, TGetListInput, TGetListBriefInput, TCreateInput, TUpdateInput>
    : AbpControllerBase
where ITAppService : ICurdAppService<TGetOutputDto, TGetListOutputDto, TGetListBriefOutputDto, TKey, TGetListInput, TGetListBriefInput, TCreateInput, TUpdateInput>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
where TGetListBriefInput : TGetListInput
{

    private readonly ITAppService _recipeAppService;

    public CurdController(ITAppService recipeAppService)
    {
        _recipeAppService = recipeAppService;
    }

    [HttpPost]
    [Route("Create")]
    
    public virtual async Task<TGetOutputDto> 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<PagedResultDto<TGetListOutputDto>> GetAllAsync(TGetListInput input)
    {
        return await _recipeAppService.GetAllAsync(input);
    }

    [HttpGet]
    [Route("Get")]
    
    public virtual async Task<TGetOutputDto> GetAsync(TKey id)
    {
        return await _recipeAppService.GetAsync(id);
    }

    [HttpPut]
    [Route("Update")]
    
    public virtual async Task<TGetOutputDto> UpdateAsync(TUpdateInput input)
    {
        return await _recipeAppService.UpdateAsync(input);
    }

    [HttpGet]
    [Route("GetAllBrief")]
    
    public virtual async Task<PagedResultDto<TGetListBriefOutputDto>> 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图表控件等。使用此接口需要注意:由于没有分页限制,需要其他的查询约束条件(比如日期范围),否则会返回大量的数据,影响性能。

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

在这里插入图片描述

在这里插入图片描述

使用

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

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

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

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

public interface IAlarmAppService : IExtendedCurdAppService<AlarmDto, AlarmDto, AlarmBriefDto, long, GetAllAlarmInput, GetAllAlarmInput,  CreateAlarmInput, UpdateAlarmInput>, IApplicationService
{
}
  1. 在Health模块的应用层中,创建AlarmAppService
public class AlarmAppService : ExtendedCurdAppServiceBase<CAH.Health.Alarm.Alarm, AlarmDto, AlarmDto, AlarmBriefDto, long, GetAllAlarmInput, GetAllAlarmInput, CreateAlarmInput, UpdateAlarmInput>, IAlarmAppService
{
    public AlarmAppService(IRepository<CAH.Health.Alarm.Alarm, long> basicInventoryRepository) : base(basicInventoryRepository)
    {
    }
}

  1. 在Health模块的HttpApi中,创建AlarmController,并实现IAlarmAppService接口
[Area(HealthRemoteServiceConsts.ModuleName)]
[RemoteService(Name = HealthRemoteServiceConsts.RemoteServiceName)]
[Route("api/Health/alarm")]
public class AlarmController : ExtendedCurdController<IAlarmAppService, AlarmDto, AlarmDto, AlarmBriefDto, long, GetAllAlarmInput, GetAllAlarmInput, CreateAlarmInput, UpdateAlarmInput>, IAlarmAppService
{
    private readonly IAlarmAppService _alarmAppService;

    public AlarmController(IAlarmAppService alarmAppService) : base(alarmAppService)
    {
        _alarmAppService = alarmAppService;
    }


}

运行程序

在这里插入图片描述

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,我们可以定义一个基类 `NewsWorker`,里面包含一些新闻工作者的共同属性和方法,如下: ```java public class NewsWorker { private String name; private int age; private String phone; private String email; public NewsWorker(String name, int age, String phone, String email) { this.name = name; this.age = age; this.phone = phone; this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public void printInfo() { System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Phone: " + phone); System.out.println("Email: " + email); } } ``` 接下来,我们可以定义一个小型数据库类 `NewsDatabase`,用于存储新闻工作者的信息。这个类包含一些基本的增删查改操作,如下: ```java import java.util.ArrayList; public class NewsDatabase { private ArrayList<NewsWorker> workers; public NewsDatabase() { workers = new ArrayList<NewsWorker>(); } // 添加新闻工作者 public void addWorker(NewsWorker worker) { workers.add(worker); } // 删除新闻工作者 public void removeWorker(NewsWorker worker) { workers.remove(worker); } // 根据姓名查找新闻工作者 public NewsWorker findWorker(String name) { for (NewsWorker worker : workers) { if (worker.getName().equals(name)) { return worker; } } return null; } // 更新新闻工作者信息 public void updateWorker(NewsWorker worker, String name, int age, String phone, String email) { worker.setName(name); worker.setAge(age); worker.setPhone(phone); worker.setEmail(email); } // 打印所有新闻工作者信息 public void printAllWorkers() { for (NewsWorker worker : workers) { worker.printInfo(); System.out.println(); } } } ``` 最后,我们可以在 `Main` 类中使用这个数据库类进行增删查改操作,如下: ```java public class Main { public static void main(String[] args) { NewsDatabase database = new NewsDatabase(); // 添加新闻工作者 NewsWorker worker1 = new NewsWorker("张三", 30, "123456789", "[email protected]"); database.addWorker(worker1); NewsWorker worker2 = new NewsWorker("李", 25, "987654321", "[email protected]"); database.addWorker(worker2); // 查找新闻工作者 NewsWorker worker = database.findWorker("张三"); if (worker != null) { worker.printInfo(); System.out.println(); } // 更新新闻工作者信息 database.updateWorker(worker2, "王五", 28, "135792468", "[email protected]"); // 删除新闻工作者 database.removeWorker(worker1); // 打印所有新闻工作者信息 database.printAllWorkers(); } } ``` 这样,我们就完成了一个基于 Java 的简单的新闻工作者数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林晓lx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值