diff --git a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services/IReadOnlyAppService.cs b/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services/IReadOnlyAppService.cs index 5b5f1cf06e..3d00f70286 100644 --- a/framework/src/Volo.Abp.Ddd.Application.Contracts/Volo/Abp/Application/Services/IReadOnlyAppService.cs +++ b/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 public interface IReadOnlyAppService : IApplicationService { + Task ExistsAsync(TKey id); + + Task FindAsync(TKey id); + Task GetAsync(TKey id); Task> GetListAsync(TGetListInput input); diff --git a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AbstractKeyReadOnlyAppService.cs b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AbstractKeyReadOnlyAppService.cs index 3791c4202f..778de5b6f6 100644 --- a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AbstractKeyReadOnlyAppService.cs +++ b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AbstractKeyReadOnlyAppService.cs @@ -49,6 +49,22 @@ public abstract class AbstractKeyReadOnlyAppService ExistsAsync(TKey id) + { + await CheckGetPolicyAsync(); + + return await ExistAsync(id); + } + + public virtual async Task FindAsync(TKey id) + { + await CheckGetPolicyAsync(); + + var entity = await FindEntityByIdAsync(id); + + return entity == null ? default : await MapToGetListOutputDtoAsync(entity); + } + public virtual async Task GetAsync(TKey id) { await CheckGetPolicyAsync(); @@ -83,6 +99,10 @@ public abstract class AbstractKeyReadOnlyAppService ExistAsync(TKey id); + + protected abstract Task FindEntityByIdAsync(TKey id); + protected abstract Task GetEntityByIdAsync(TKey id); protected virtual async Task CheckGetPolicyAsync() diff --git a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppService.cs b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppService.cs index 672d85a2be..e6d391fd33 100644 --- a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppService.cs +++ b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppService.cs @@ -74,12 +74,22 @@ public abstract class CrudAppService ExistAsync(TKey id) + { + return await Repository.AnyAsync(x => x.Id!.Equals(id)); + } + + protected async override Task FindEntityByIdAsync(TKey id) + { + return await Repository.FindAsync(id); + } + + protected async override Task DeleteByIdAsync(TKey id) { await Repository.DeleteAsync(id); } - protected override async Task GetEntityByIdAsync(TKey id) + protected async override Task GetEntityByIdAsync(TKey id) { return await Repository.GetAsync(id); } diff --git a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ReadOnlyAppService.cs b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ReadOnlyAppService.cs index 867ca35ad4..cffdb7f7d2 100644 --- a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ReadOnlyAppService.cs +++ b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ReadOnlyAppService.cs @@ -42,7 +42,17 @@ public abstract class ReadOnlyAppService GetEntityByIdAsync(TKey id) + protected override Task ExistAsync(TKey id) + { + return Repository.AnyAsync(x => x.Id!.Equals(id)); + } + + protected override Task FindEntityByIdAsync(TKey id) + { + return Repository.FindAsync(id); + } + + protected async override Task GetEntityByIdAsync(TKey id) { return await Repository.GetAsync(id); } diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/DistrictAppService.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/DistrictAppService.cs index 5f277437c8..32c9d7aad4 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/DistrictAppService.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Application/DistrictAppService.cs @@ -15,12 +15,26 @@ public class DistrictAppService : AbstractKeyCrudAppService d.CityId == id.CityId && d.Name == id.Name); } - protected override async Task GetEntityByIdAsync(DistrictKey id) + protected async override Task ExistAsync(DistrictKey id) + { + return await AsyncExecuter.AnyAsync( + (await Repository.GetQueryableAsync()).Where(d => d.CityId == id.CityId && d.Name == id.Name) + ); + } + + protected async override Task FindEntityByIdAsync(DistrictKey id) + { + return await AsyncExecuter.FirstOrDefaultAsync( + (await Repository.GetQueryableAsync()).Where(d => d.CityId == id.CityId && d.Name == id.Name) + ); + } + + protected async override Task GetEntityByIdAsync(DistrictKey id) { return await AsyncExecuter.FirstOrDefaultAsync( (await Repository.GetQueryableAsync()).Where(d => d.CityId == id.CityId && d.Name == id.Name)