ICrudAppService with different Get/GetList DTO models

Resolves https://github.com/abpframework/abp/issues/1027
pull/1216/head
Yunus Emre Kalkan 6 years ago
parent f7584a10aa
commit 7fd47fa9f7

@ -45,28 +45,40 @@ namespace Volo.Abp.Application.Services
}
public abstract class AsyncCrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CrudAppServiceBase<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>,
IAsyncCrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: AsyncCrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
protected AsyncCrudAppService(IRepository<TEntity, TKey> repository)
: base(repository)
{
}
}
public abstract class AsyncCrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CrudAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>,
IAsyncCrudAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
public IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; }
protected AsyncCrudAppService(IRepository<TEntity, TKey> repository)
:base(repository)
: base(repository)
{
AsyncQueryableExecuter = DefaultAsyncQueryableExecuter.Instance;
}
public virtual async Task<TEntityDto> GetAsync(TKey id)
public virtual async Task<TGetOutputDto> GetAsync(TKey id)
{
await CheckGetPolicyAsync();
var entity = await GetEntityByIdAsync(id);
return MapToEntityDto(entity);
return MapToGetOutputDto(entity);
}
public virtual async Task<PagedResultDto<TEntityDto>> GetListAsync(TGetListInput input)
public virtual async Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input)
{
await CheckGetListPolicyAsync();
@ -79,29 +91,29 @@ namespace Volo.Abp.Application.Services
var entities = await AsyncQueryableExecuter.ToListAsync(query);
return new PagedResultDto<TEntityDto>(
return new PagedResultDto<TGetListOutputDto>(
totalCount,
entities.Select(MapToEntityDto).ToList()
entities.Select(MapToGetListOutputDto).ToList()
);
}
public virtual async Task<TEntityDto> CreateAsync(TCreateInput input)
public virtual async Task<TGetOutputDto> CreateAsync(TCreateInput input)
{
await CheckCreatePolicyAsync();
var entity = MapToEntity(input);
if(entity is IMultiTenant && !HasTenantIdProperty(entity))
if (entity is IMultiTenant && !HasTenantIdProperty(entity))
{
TryToSetTenantId(entity);
}
await Repository.InsertAsync(entity, autoSave: true);
return MapToEntityDto(entity);
return MapToGetOutputDto(entity);
}
public virtual async Task<TEntityDto> UpdateAsync(TKey id, TUpdateInput input)
public virtual async Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input)
{
await CheckUpdatePolicyAsync();
@ -110,7 +122,7 @@ namespace Volo.Abp.Application.Services
MapToEntity(input, entity);
await Repository.UpdateAsync(entity, autoSave: true);
return MapToEntityDto(entity);
return MapToGetOutputDto(entity);
}
public virtual async Task DeleteAsync(TKey id)

@ -44,8 +44,7 @@ namespace Volo.Abp.Application.Services
}
public abstract class CrudAppService<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CrudAppServiceBase<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>,
ICrudAppService<TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CrudAppService<TEntity, TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
{
@ -54,16 +53,30 @@ namespace Volo.Abp.Application.Services
{
}
}
public virtual TEntityDto Get(TKey id)
public abstract class CrudAppService<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
: CrudAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>,
ICrudAppService<TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput>
where TEntity : class, IEntity<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
protected CrudAppService(IRepository<TEntity, TKey> repository)
: base(repository)
{
}
public virtual TGetOutputDto Get(TKey id)
{
CheckGetPolicy();
var entity = GetEntityById(id);
return MapToEntityDto(entity);
return MapToGetOutputDto(entity);
}
public virtual PagedResultDto<TEntityDto> GetList(TGetListInput input)
public virtual PagedResultDto<TGetListOutputDto> GetList(TGetListInput input)
{
CheckGetListPolicy();
@ -76,29 +89,29 @@ namespace Volo.Abp.Application.Services
var entities = query.ToList();
return new PagedResultDto<TEntityDto>(
return new PagedResultDto<TGetListOutputDto>(
totalCount,
entities.Select(MapToEntityDto).ToList()
entities.Select(MapToGetListOutputDto).ToList()
);
}
public virtual TEntityDto Create(TCreateInput input)
public virtual TGetOutputDto Create(TCreateInput input)
{
CheckCreatePolicy();
var entity = MapToEntity(input);
if(entity is IMultiTenant && !HasTenantIdProperty(entity))
if (entity is IMultiTenant && !HasTenantIdProperty(entity))
{
TryToSetTenantId(entity);
}
Repository.Insert(entity, autoSave: true);
return MapToEntityDto(entity);
return MapToGetOutputDto(entity);
}
public virtual TEntityDto Update(TKey id, TUpdateInput input)
public virtual TGetOutputDto Update(TKey id, TUpdateInput input)
{
CheckUpdatePolicy();
@ -106,7 +119,7 @@ namespace Volo.Abp.Application.Services
MapToEntity(input, entity);
Repository.Update(entity, autoSave: true);
return MapToEntityDto(entity);
return MapToGetOutputDto(entity);
}
public virtual void Delete(TKey id)

@ -13,10 +13,11 @@ namespace Volo.Abp.Application.Services
/// This is a common base class for CrudAppService and AsyncCrudAppService classes.
/// Inherit either from CrudAppService or AsyncCrudAppService, not from this class.
/// </summary>
public abstract class CrudAppServiceBase<TEntity, TEntityDto, TKey, TGetListInput, TCreateInput, TUpdateInput> :
public abstract class CrudAppServiceBase<TEntity, TGetOutputDto, TGetListOutputDto, TKey, TGetListInput, TCreateInput, TUpdateInput> :
ApplicationService
where TEntity : class, IEntity<TKey>
where TEntityDto : IEntityDto<TKey>
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
protected IRepository<TEntity, TKey> Repository { get; }
@ -100,13 +101,23 @@ namespace Volo.Abp.Application.Services
}
/// <summary>
/// Maps <see cref="TEntity"/> to <see cref="TEntityDto"/>.
/// Maps <see cref="TEntity"/> to <see cref="TGetOutputDto"/>.
/// It uses <see cref="IObjectMapper"/> by default.
/// It can be overrided for custom mapping.
/// </summary>
protected virtual TEntityDto MapToEntityDto(TEntity entity)
protected virtual TGetOutputDto MapToGetOutputDto(TEntity entity)
{
return ObjectMapper.Map<TEntity, TEntityDto>(entity);
return ObjectMapper.Map<TEntity, TGetOutputDto>(entity);
}
/// <summary>
/// Maps <see cref="TEntity"/> to <see cref="TGetListOutputDto"/>.
/// It uses <see cref="IObjectMapper"/> by default.
/// It can be overrided for custom mapping.
/// </summary>
protected virtual TGetListOutputDto MapToGetListOutputDto(TEntity entity)
{
return ObjectMapper.Map<TEntity, TGetListOutputDto>(entity);
}
/// <summary>

@ -25,16 +25,24 @@ namespace Volo.Abp.Application.Services
}
public interface IAsyncCrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: IApplicationService
: IAsyncCrudAppService<TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
where TEntityDto : IEntityDto<TKey>
{
Task<TEntityDto> GetAsync(TKey id);
Task<PagedResultDto<TEntityDto>> GetListAsync(TGetListInput input);
}
public interface IAsyncCrudAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: IApplicationService
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
Task<TGetOutputDto> GetAsync(TKey id);
Task<PagedResultDto<TGetListOutputDto>> GetListAsync(TGetListInput input);
Task<TEntityDto> CreateAsync(TCreateInput input);
Task<TGetOutputDto> CreateAsync(TCreateInput input);
Task<TEntityDto> UpdateAsync(TKey id, TUpdateInput input);
Task<TGetOutputDto> UpdateAsync(TKey id, TUpdateInput input);
Task DeleteAsync(TKey id);
}

@ -24,16 +24,24 @@ namespace Volo.Abp.Application.Services
}
public interface ICrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: IApplicationService
: ICrudAppService<TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
where TEntityDto : IEntityDto<TKey>
{
TEntityDto Get(TKey id);
PagedResultDto<TEntityDto> GetList(TGetListInput input);
}
public interface ICrudAppService<TGetOutputDto, TGetListOutputDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: IApplicationService
where TGetOutputDto : IEntityDto<TKey>
where TGetListOutputDto : IEntityDto<TKey>
{
TGetOutputDto Get(TKey id);
PagedResultDto<TGetListOutputDto> GetList(TGetListInput input);
TEntityDto Create(TCreateInput input);
TGetOutputDto Create(TCreateInput input);
TEntityDto Update(TKey id, TUpdateInput input);
TGetOutputDto Update(TKey id, TUpdateInput input);
void Delete(TKey id);
}

Loading…
Cancel
Save