Browse Source

Add `ExistAsync` and `FindAsync` methods to `IReadOnlyAppService`.

IReadOnlyAppService
maliming 9 months ago
parent
commit
a810bfe55d
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 7
      framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services/IReadOnlyAppService.cs
  2. 20
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AbstractKeyReadOnlyAppService.cs
  3. 14
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppService.cs
  4. 12
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ReadOnlyAppService.cs
  5. 18
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/DistrictAppService.cs

7
framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services/IReadOnlyAppService.cs

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
namespace Volo.Abp.Application.Services;
@ -18,6 +19,10 @@ public interface IReadOnlyAppService<TEntityDto, in TKey, in TGetListInput>
public interface IReadOnlyAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput>
: IApplicationService
{
Task<bool> ExistsAsync(TKey id);
Task<TGetListOutputDto?> FindAsync(TKey id);
Task<TGetOutputDto> GetAsync(TKey id);
Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input);

20
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AbstractKeyReadOnlyAppService.cs

@ -49,6 +49,22 @@ public abstract class AbstractKeyReadOnlyAppService<TEntity, TGetOutputDto, TGet
ReadOnlyRepository = repository;
}
public virtual async Task<bool> ExistsAsync(TKey id)
{
await CheckGetPolicyAsync();
return await ExistAsync(id);
}
public virtual async Task<TGetListOutputDto?> FindAsync(TKey id)
{
await CheckGetPolicyAsync();
var entity = await FindEntityByIdAsync(id);
return entity == null ? default : await MapToGetListOutputDtoAsync(entity);
}
public virtual async Task<TGetOutputDto> GetAsync(TKey id)
{
await CheckGetPolicyAsync();
@ -83,6 +99,10 @@ public abstract class AbstractKeyReadOnlyAppService<TEntity, TGetOutputDto, TGet
);
}
protected abstract Task<bool> ExistAsync(TKey id);
protected abstract Task<TEntity?> FindEntityByIdAsync(TKey id);
protected abstract Task<TEntity> GetEntityByIdAsync(TKey id);
protected virtual async Task CheckGetPolicyAsync()

14
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppService.cs

@ -74,12 +74,22 @@ public abstract class CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto,
Repository = repository;
}
protected override async Task DeleteByIdAsync(TKey id)
protected async override Task<bool> ExistAsync(TKey id)
{
return await Repository.AnyAsync(x => x.Id!.Equals(id));
}
protected async override Task<TEntity?> FindEntityByIdAsync(TKey id)
{
return await Repository.FindAsync(id);
}
protected async override Task DeleteByIdAsync(TKey id)
{
await Repository.DeleteAsync(id);
}
protected override async Task<TEntity> GetEntityByIdAsync(TKey id)
protected async override Task<TEntity> GetEntityByIdAsync(TKey id)
{
return await Repository.GetAsync(id);
}

12
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ReadOnlyAppService.cs

@ -42,7 +42,17 @@ public abstract class ReadOnlyAppService<TEntity, TGetOutputDto, TGetListOutputD
Repository = repository;
}
protected override async Task<TEntity> GetEntityByIdAsync(TKey id)
protected override Task<bool> ExistAsync(TKey id)
{
return Repository.AnyAsync(x => x.Id!.Equals(id));
}
protected override Task<TEntity?> FindEntityByIdAsync(TKey id)
{
return Repository.FindAsync(id);
}
protected async override Task<TEntity > GetEntityByIdAsync(TKey id)
{
return await Repository.GetAsync(id);
}

18
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/DistrictAppService.cs

@ -15,12 +15,26 @@ public class DistrictAppService : AbstractKeyCrudAppService<District, DistrictDt
{
}
protected override async Task DeleteByIdAsync(DistrictKey id)
protected async override Task DeleteByIdAsync(DistrictKey id)
{
await Repository.DeleteAsync(d => d.CityId == id.CityId && d.Name == id.Name);
}
protected override async Task<District> GetEntityByIdAsync(DistrictKey id)
protected async override Task<bool> ExistAsync(DistrictKey id)
{
return await AsyncExecuter.AnyAsync(
(await Repository.GetQueryableAsync()).Where(d => d.CityId == id.CityId && d.Name == id.Name)
);
}
protected async override Task<District> FindEntityByIdAsync(DistrictKey id)
{
return await AsyncExecuter.FirstOrDefaultAsync(
(await Repository.GetQueryableAsync()).Where(d => d.CityId == id.CityId && d.Name == id.Name)
);
}
protected async override Task<District> GetEntityByIdAsync(DistrictKey id)
{
return await AsyncExecuter.FirstOrDefaultAsync(
(await Repository.GetQueryableAsync()).Where(d => d.CityId == id.CityId && d.Name == id.Name)

Loading…
Cancel
Save