The GetAll and Get methods of the ready-made CrudAppService don\'t include child entities.
Is it possible to modify its behavi
You have to include the child entities manually. It's lazy loading by design.
For whom that work with AsyncCrudAppService and you have two different lists of child:
Below to get specific parent Object with their list of child
protected override Task<Parent> GetEntityByIdAsync(int id)
{
var entity = Repository.GetAllIncluding(p => p.listOfFirstChild).Include(x => x.listOfSecondChild).FirstOrDefault(p => p.Id == id);
return base.GetEntityByIdAsync(id);
}
In Abp.io All You Need Is (that must be added to PostService inherited from CrudAppService ):
protected override IQueryable<Post> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
{
return _postReposiory
.Include(p => p.Item);
}
Yes, You have to include explicitly like this.
GetAll().Include(i => i.ChildEntities)
You have to use eager-loading.
Override CreateFilteredQuery and GetEntityById in your AppService:
public class MyAppService : CrudAppService<ParentEntity, ParentEntityDto>, IMyAppService
{
public MyAppService(IRepository<ParentEntity> repository)
: base(repository)
{
}
protected override IQueryable<ParentEntity> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
{
return Repository.GetAllIncluding(p => p.ChildEntity);
}
protected override ParentEntity GetEntityById(int id)
{
var entity = Repository.GetAllIncluding(p => p.ChildEntity).FirstOrDefault(p => p.Id == id);
if (entity == null)
{
throw new EntityNotFoundException(typeof(ParentEntity), id);
}
return entity;
}
}
The benefit of overriding these methods is that you continue to get permission checking, counting, sorting, paging and mapping for free.
GetAllIncludinghas some problem if the included entity has a navigation property to the parent; it falls into a sort of circular dependency. Is there anyAttributeor trick to exclude the navigation property from the serialization?
Return ItemDto (without navigation property) in PostDto.