Blame view

泰额版/Food Labeling Management Code/Yi.Doc.Md/02.框架功能模块教程/08.Crud增删改查.md 2.73 KB
59e51671   “wangming”   1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  ## 简介
  > 想做一个快乐的Crud boy??好,满足你
  
  可能绝大部分简单的业务,真的只是不用类型的Crud,大量的重复代码,使用cv方式,不够优雅
  
  框架内部封装各种场景下的crud
  
  ## 使用
  在应用层继承`YiCrudAppService`crud服务即可
  在这之前,你应该先了解各个dto的作用:
  
  > 注意,我们当然可以直接使用Abp中的`CrudAppService`,但由于Abp内置的Crud还缺少一些常用的接口,比如批量删除等方式,所以推荐使用`YiCrudAppService`,使用上完全没有区别
  
  ``` cs
  - TGetOutputDto (单查返回的dto)
  - TGetListOutputDto (多查返回的dto)
  - TGetListInput (多查的条件)
  - TCreateInput (创建的dto)
  - TUpdateInput (更新的dto)
  ```
  根据Dto业务场景,它有很多种选项,依次为:
  ``` cs
  - YiCrudAppService<TEntity, TEntityDto, TKey>
  - YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput>
  - YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput>
  - YiCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
  - YiCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
  ```
  dto可以放到`Application.Contracts`层,同理接口继承`IYiCrudAppService`即可
  ```cs
  - YiCrudAppService<TEntityDto, TKey>
  - YiCrudAppService<TEntityDto, TKey, TGetListInput>
  - YiCrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput>
  - YiCrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
  - YiCrudAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
  ```
  >  可以发现,接口,不应该与实体有直接关系
  
  其中,在YiCrudAppService中,我们提供了一些内置的方法:
  ```cs
  public virtual async Task<TGetOutputDto> CreateAsync(TCreateInput input)
  public virtual async Task<bool> DeleteAsync(string id)
  public virtual async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
  public virtual async Task<TGetOutputDto> GetAsync(TKey id)
  public virtual async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
  ```
  同时还有映射关系:
  ``` cs
  protected virtual Task<TGetOutputDto> MapToGetOutputDtoAsync(TEntity entity)
  protected virtual Task<List<TGetListOutputDto>> MapToGetListOutputDtosAsync(List<TEntity> entities)
  protected virtual Task<TGetListOutputDto> MapToGetListOutputDtoAsync(TEntity entity)
  protected virtual Task<TGetOutputDto> MapToGetOutputDtoAsync(TEntity entity)
  protected virtual Task<List<TGetListOutputDto>> MapToGetListOutputDtosAsync(List<TEntity> entities)
  protected virtual Task<TGetListOutputDto> MapToGetListOutputDtoAsync(TEntity entity)
  ```
  另外,它也提供了对应的`仓储`及`当前用户`等常用属性