从 ABP 中的 CrudAppService 检索子实体 [英] Retrieve child entities from CrudAppService in ABP

查看:28
本文介绍了从 ABP 中的 CrudAppService 检索子实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现成的 CrudAppServiceGetAllGet 方法不包含子实体.

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

是否可以修改其行为?

GetAllInducing 如果包含的实体具有到父级的导航属性,则存在一些问题;它属于一种循环依赖.是否有任何 Attribute 或技巧可以从序列化中排除导航属性?[NonSerialized] 属性似乎不适用于导航属性.

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; }
}

发布实体:

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

项目实体:

[Table("AbpItems")]
public class Item : Entity
{
    [ForeignKey("PostId")]
    public Post Post { get; set; }
    public int PostId { get; set; }
}

推荐答案

你必须使用预先加载.

覆盖您的 AppService 中的 CreateFilteredQueryGetEntityById:

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.

GetAllInducing 如果包含的实体具有到父级的导航属性,则存在一些问题;它属于一种循环依赖.是否有任何 Attribute 或技巧可以从序列化中排除导航属性?

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?

PostDto中返回ItemDto(没有导航属性).

Return ItemDto (without navigation property) in PostDto.

这篇关于从 ABP 中的 CrudAppService 检索子实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆