Retrieve child entities from CrudAppService in ABP

前端 未结 5 1020
忘了有多久
忘了有多久 2021-01-03 16:47

The GetAll and Get methods of the ready-made CrudAppService don\'t include child entities.

Is it possible to modify its behavi

相关标签:
5条回答
  • 2021-01-03 17:09

    You have to include the child entities manually. It's lazy loading by design.

    0 讨论(0)
  • 2021-01-03 17:19

    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);
            }
    
    
    0 讨论(0)
  • 2021-01-03 17:26

    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);
            }
    
    
    0 讨论(0)
  • 2021-01-03 17:28

    Yes, You have to include explicitly like this.

    GetAll().Include(i => i.ChildEntities)
    
    0 讨论(0)
  • 2021-01-03 17:34

    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.

    Update

    GetAllIncluding has some problem if the included entity has a navigation property to the parent; it falls into a sort of circular dependency. Is there any Attribute or trick to exclude the navigation property from the serialization?

    Return ItemDto (without navigation property) in PostDto.

    0 讨论(0)
提交回复
热议问题