如何仅加载一次具有循环引用的层次结构中的实例?
我有一个像这样的对象结构:
public class Entity
{
IList<Relationship> Relationships{get;set;}
}
public class Relationship
{
public Relationship(Entity parent, IList<Entity> children)
{
//set properties
}
Entity Parent{get;private set;}
IList<Entity> Children{get;private set;}
}
关系包含有关父实例和子实例的所有信息,我想在构成该关系的父实例和所有子实例上共享相同的关系实例。
现在,当我从数据库加载实体时,我从顶部实体开始,然后加载关系。我想我可以缓存我正在构建的关系并为孩子们重用相同的实例。但这并不能创建一个我需要加载所有子实体的关系,因此每个子实体都会尝试重新创建我当前在创建子实体之前尝试获取子实体的相同关系,因此我最终创建了所有子实体在我可以将关系添加到缓存之前,先检查树中我下面的关系实例。
有没有一种方法可以解决这个问题,而不需要使子项成为我的关系的可设置属性,以便我可以在创建子项之前创建对关系的引用?
I have a object structure like this:
public class Entity
{
IList<Relationship> Relationships{get;set;}
}
public class Relationship
{
public Relationship(Entity parent, IList<Entity> children)
{
//set properties
}
Entity Parent{get;private set;}
IList<Entity> Children{get;private set;}
}
The Relationship contains all information about the parent and child instances, and I would like to share the same Relationship instance on the parent and all child instances that make up the relationship.
Now when I come to load my entities from the db I start with the top entity, which then loads the relationships. I thought I could just cache the relationship I am building and reuse the same instance for the children. But this doesn't work as to create a relationship I need to load all child entities, so each child entity tries to recreate the same relationship I am currently trying to get the children for before it has been created, so I end up creating all of the relationship instances below me in the tree before I can add the relationship to the cache.
Is there a way I can get round this without making the children a settable property of my relationship, so I can create the reference to the relationship before the children are created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我通过引入一个RelationshipBuilder类来解决这个问题,我用它来跟踪关系的父母和孩子,而不实际创建它。
这使我能够加载一个实体并在构建器中创建其关系,当关系尝试创建子项时,他们会与构建器进行检查以查看该关系是否已被创建并返回。
这意味着我可以沿着层次结构一路导航,加载所有实体实例并设置与关系关联的父实体和子实体。一旦所有实体都已加载并且递归已完成,我们将返回到开始加载实体的入口点,我要求关系构建器解析所有关系。然后,这将创建它所获悉的每个关系,并在该关系中涉及的父实例和子实例上设置关系实例。
So I managed this by introducing a RelationshipBuilder class which I used to keep track of the parents and children of a relationship without actually creating it.
This enabled me to load an entity and create its relationships in the builder, when the relationships tried to create the children they checked with the builder to see that the relationship was already being created and returned.
This meant I could navigate all the way down the hierarchy loading all of the entity instance and setting the parent entity and child entities associated with the relationships. Once all of the entities had been loaded and the recursion had completed and we were returned to the entry point where we started loading the entities I ask the relationship builder to resolve all the relationships. This then creates each relationship it has been informed about and sets the relationship instance on the parent and child instances which are involved in that relationship.