Browse Source

Merge pull request #1216 from abpframework/CrudAppService-with-different-Get/GetList-DTO-models

ICrudAppService with different Get/GetList DTO models
pull/1219/head
Halil İbrahim Kalkan 7 years ago
committed by GitHub
parent
commit
a13dddc8aa
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AsyncCrudAppService.cs
  2. 41
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppService.cs
  3. 21
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppServiceBase.cs
  4. 18
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/IAsyncCrudAppService.cs
  5. 18
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ICrudAppService.cs

44
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/AsyncCrudAppService.cs

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

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

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

21
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/CrudAppServiceBase.cs

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

18
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/IAsyncCrudAppService.cs

@ -25,16 +25,24 @@ namespace Volo.Abp.Application.Services
} }
public interface IAsyncCrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput> public interface IAsyncCrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: IApplicationService : IAsyncCrudAppService<TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
where TEntityDto : IEntityDto<TKey> 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); Task DeleteAsync(TKey id);
} }

18
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ICrudAppService.cs

@ -24,16 +24,24 @@ namespace Volo.Abp.Application.Services
} }
public interface ICrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput> public interface ICrudAppService<TEntityDto, in TKey, in TGetListInput, in TCreateInput, in TUpdateInput>
: IApplicationService : ICrudAppService<TEntityDto, TEntityDto, TKey, TGetListInput, TCreateInput, TCreateInput>
where TEntityDto : IEntityDto<TKey> 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); void Delete(TKey id);
} }

Loading…
Cancel
Save