Retrieve child entities from CrudAppService in ABP

asp.net-core aspnetboilerplate c# entity-framework-core parent-child

Question

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

Is it possible to modify its behaviour?

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? The [NonSerialized] attribute does not seem to be applicable to a navigation property.

PostAppService:

public class PostAppService : CrudAppService<Post, PostDto>, IPostAppService
{
    IRepository<Post> _repository = null;

    public PostAppService(IRepository<Post> repository) : base(repository)
    {
        _repository = repository;
    }

    protected override IQueryable<Post> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
    {
        return _repository.GetAllIncluding(p => p.Items);
    }
}

PostDto:

[AutoMap(typeof(Post))]
public class PostDto : EntityDto
{
    public ICollection<Item> Items { get; set; }
}

Post entity:

[Table("AbpPosts")]
public class Post : FullAuditedEntity<int,User>
{
    public virtual ICollection<Item> Items { get; set; }
}

Item entity:

[Table("AbpItems")]
public class Item : Entity
{
    [ForeignKey("PostId")]
    public Post Post { get; set; }
    public int PostId { get; set; }
}
1
1
1/23/2018 4:50:06 PM

Accepted Answer

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.

7
1/23/2018 5:00:01 PM

Popular Answer

Yes, You have to include explicitly like this.

GetAll().Include(i => i.ChildEntities)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow