在 ABP 中从 CrudAppService 中检索子实体
Retrieve child entities from CrudAppService in ABP
现成CrudAppService
的GetAll
和Get
方法不包含子实体。
是否可以修改其行为?
更新
GetAllIncluding
如果包含的实体具有到父级的导航 属性 则存在一些问题;它属于一种循环依赖。是否有任何 Attribute
或技巧可以从序列化中排除导航 属性? [NonSerialized]
属性似乎不适用于导航 属性。
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实体:
[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; }
}
您必须手动包含子实体。这是设计的延迟加载。
是的,你必须像这样明确地包含。
GetAll().Include(i => i.ChildEntities)
你必须使用预先加载。
在您的 AppService 中覆盖 CreateFilteredQuery
和 GetEntityById
:
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;
}
}
覆盖这些方法的好处是您可以继续免费获得 permission checking, counting, sorting, paging and mapping。
更新
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
(无导航 属性)在 PostDto
.
与 AsyncCrudAppService 一起工作的人,您有两个不同的子列表:
下面获取特定父对象及其子对象列表
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(必须添加到继承自 CrudAppService 的 PostService):
protected override IQueryable<Post> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
{
return _postReposiory
.Include(p => p.Item);
}
现成CrudAppService
的GetAll
和Get
方法不包含子实体。
是否可以修改其行为?
更新
GetAllIncluding
如果包含的实体具有到父级的导航 属性 则存在一些问题;它属于一种循环依赖。是否有任何 Attribute
或技巧可以从序列化中排除导航 属性? [NonSerialized]
属性似乎不适用于导航 属性。
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实体:
[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; }
}
您必须手动包含子实体。这是设计的延迟加载。
是的,你必须像这样明确地包含。
GetAll().Include(i => i.ChildEntities)
你必须使用预先加载。
在您的 AppService 中覆盖 CreateFilteredQuery
和 GetEntityById
:
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;
}
}
覆盖这些方法的好处是您可以继续免费获得 permission checking, counting, sorting, paging and mapping。
更新
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 anyAttribute
or trick to exclude the navigation property from the serialization?
Return ItemDto
(无导航 属性)在 PostDto
.
与 AsyncCrudAppService 一起工作的人,您有两个不同的子列表:
下面获取特定父对象及其子对象列表
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(必须添加到继承自 CrudAppService 的 PostService):
protected override IQueryable<Post> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
{
return _postReposiory
.Include(p => p.Item);
}