Resolved #4862 Support async object mapping for the base CRUD application service.

pull/4882/head
Halil İbrahim Kalkan 5 years ago
parent 14d67a0a54
commit 276a48a240

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Entities;
@ -51,6 +52,11 @@ namespace Volo.Abp.Application.Services
}
protected override Task<TEntityDto> MapToGetListOutputDtoAsync(TEntity entity)
{
return MapToGetOutputDtoAsync(entity);
}
protected override TEntityDto MapToGetListOutputDto(TEntity entity)
{
return MapToGetOutputDto(entity);
@ -80,13 +86,13 @@ namespace Volo.Abp.Application.Services
{
await CheckCreatePolicyAsync();
var entity = MapToEntity(input);
var entity = await MapToEntityAsync(input);
TryToSetTenantId(entity);
await Repository.InsertAsync(entity, autoSave: true);
return MapToGetOutputDto(entity);
return await MapToGetOutputDtoAsync(entity);
}
public virtual async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
@ -95,10 +101,10 @@ namespace Volo.Abp.Application.Services
var entity = await GetEntityByIdAsync(id);
//TODO: Check if input has id different than given id and normalize if it's default value, throw ex otherwise
MapToEntity(input, entity);
await MapToEntityAsync(input, entity);
await Repository.UpdateAsync(entity, autoSave: true);
return MapToGetOutputDto(entity);
return await MapToGetOutputDtoAsync(entity);
}
public virtual async Task DeleteAsync(TKey id)
@ -125,6 +131,17 @@ namespace Volo.Abp.Application.Services
await CheckPolicyAsync(DeletePolicyName);
}
/// <summary>
/// Maps <see cref="TCreateInput"/> to <see cref="TEntity"/> to create a new entity.
/// It uses <see cref="MapToEntity(TCreateInput)"/> by default.
/// It can be overriden for custom mapping.
/// Overriding this has higher priority than overriding the <see cref="MapToEntity(TCreateInput)"/>
/// </summary>
protected virtual Task<TEntity> MapToEntityAsync(TCreateInput createInput)
{
return Task.FromResult(MapToEntity(createInput));
}
/// <summary>
/// Maps <see cref="TCreateInput"/> to <see cref="TEntity"/> to create a new entity.
/// It uses <see cref="IObjectMapper"/> by default.
@ -153,6 +170,18 @@ namespace Volo.Abp.Application.Services
}
}
/// <summary>
/// Maps <see cref="TUpdateInput"/> to <see cref="TEntity"/> to update the entity.
/// It uses <see cref="MapToEntity(TUpdateInput, TEntity)"/> by default.
/// It can be overriden for custom mapping.
/// Overriding this has higher priority than overriding the <see cref="MapToEntity(TUpdateInput, TEntity)"/>
/// </summary>
protected virtual Task MapToEntityAsync(TUpdateInput updateInput, TEntity entity)
{
MapToEntity(updateInput, entity);
return Task.CompletedTask;
}
/// <summary>
/// Maps <see cref="TUpdateInput"/> to <see cref="TEntity"/> to update the entity.
/// It uses <see cref="IObjectMapper"/> by default.
@ -190,4 +219,4 @@ namespace Volo.Abp.Application.Services
return entity.GetType().GetProperty(nameof(IMultiTenant.TenantId)) != null;
}
}
}
}

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
@ -6,6 +7,7 @@ using Volo.Abp.Application.Dtos;
using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
namespace Volo.Abp.Application.Services
{
@ -52,7 +54,8 @@ namespace Volo.Abp.Application.Services
await CheckGetPolicyAsync();
var entity = await GetEntityByIdAsync(id);
return MapToGetOutputDto(entity);
return await MapToGetOutputDtoAsync(entity);
}
public virtual async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
@ -67,10 +70,11 @@ namespace Volo.Abp.Application.Services
query = ApplyPaging(query, input);
var entities = await AsyncExecuter.ToListAsync(query);
var entityDtos = await MapToGetListOutputDtosAsync(entities);
return new PagedResultDto<TGetListOutputDto>(
totalCount,
entities.Select(MapToGetListOutputDto).ToList()
entityDtos
);
}
@ -161,6 +165,17 @@ namespace Volo.Abp.Application.Services
return ReadOnlyRepository;
}
/// <summary>
/// Maps <see cref="TEntity"/> to <see cref="TGetOutputDto"/>.
/// It internally calls the <see cref="MapToGetOutputDto"/> by default.
/// It can be overriden for custom mapping.
/// Overriding this has higher priority than overriding the <see cref="MapToGetOutputDto"/>
/// </summary>
protected virtual Task<TGetOutputDto> MapToGetOutputDtoAsync(TEntity entity)
{
return Task.FromResult(MapToGetOutputDto(entity));
}
/// <summary>
/// Maps <see cref="TEntity"/> to <see cref="TGetOutputDto"/>.
/// It uses <see cref="IObjectMapper"/> by default.
@ -171,6 +186,33 @@ namespace Volo.Abp.Application.Services
return ObjectMapper.Map<TEntity, TGetOutputDto>(entity);
}
/// <summary>
/// Maps a list of <see cref="TEntity"/> to <see cref="TGetListOutputDto"/> objects.
/// It uses <see cref="MapToGetListOutputDtoAsync"/> method for each item in the list.
/// </summary>
protected virtual async Task<List<TGetListOutputDto>> MapToGetListOutputDtosAsync(List<TEntity> entities)
{
var dtos = new List<TGetListOutputDto>();
foreach (var entity in entities)
{
dtos.Add(await MapToGetListOutputDtoAsync(entity));
}
return dtos;
}
/// <summary>
/// Maps <see cref="TEntity"/> to <see cref="TGetListOutputDto"/>.
/// It internally calls the <see cref="MapToGetListOutputDto"/> by default.
/// It can be overriden for custom mapping.
/// Overriding this has higher priority than overriding the <see cref="MapToGetListOutputDto"/>
/// </summary>
protected virtual Task<TGetListOutputDto> MapToGetListOutputDtoAsync(TEntity entity)
{
return Task.FromResult(MapToGetListOutputDto(entity));
}
/// <summary>
/// Maps <see cref="TEntity"/> to <see cref="TGetListOutputDto"/>.
/// It uses <see cref="IObjectMapper"/> by default.

@ -55,6 +55,11 @@ namespace Volo.Abp.Application.Services
}
protected override Task<TEntityDto> MapToGetListOutputDtoAsync(TEntity entity)
{
return base.MapToGetOutputDtoAsync(entity);
}
protected override TEntityDto MapToGetListOutputDto(TEntity entity)
{
return MapToGetOutputDto(entity);

Loading…
Cancel
Save