简体   繁体   English

父对象的集合成员的对象是否也应该引用父对象?

[英]Should objects of a collection member of parent object also reference the parent object?

So I have a ContactCardGroup class that contains a collection of ContactCardGroupMemberships: 因此,我有一个ContactCardGroup类,其中包含ContactCardGroupMemberships的集合:

 public class ContactCardGroup : AbstractEntity, IContactCardGroup
    {

        public ICollection<ContactCardGroupMembership> Members { get; protected set; }

And a ContactCardGroupMembership class: 还有一个ContactCardGroupMembership类:

 public class ContactCardGroupMembership : AbstractAspect
    {

        public long MembershipId { get; set; }

        public long GroupId { get; set; }

        public ContactCard Contact { get; set; }

        public bool IsPrimary { get; set; }

        public ICollection<ContactGroupRole> Roles { get; protected set; }

        public ContactCardGroupMembership()
        {
            this.Roles = new Collection<ContactGroupRole>();
        }
    }

So throughout the code it would be helpful if instead of GroupId, I had a ContactCardGroup member.... but is that not introducing a nasty circular reference especially around disposal/destruction.Is there a rule/pattern for such things? 因此,在整个代码中,如果我有一个ContactCardGroup成员而不是GroupId,将很有帮助....但是这不是在处理/销毁方面引入麻烦的循环引用吗?是否存在此类规则/模式?

Seems like ever since I installed ReSharper I no longer have any confidence in any of the coding choices I make ;( 自从我安装ReSharper以来,似乎对我所做的任何编码选择都不再有信心。

You can have circular references, and they can be very useful. 您可以有循环引用,它们可能非常有用。 Most ORMs deal with them nicely. 大多数ORM都很好地处理它们。

However, if you use serialization libraries, then they can be a bit tricky and often don't like circular references. 但是,如果您使用序列化库,那么它们可能会有些棘手,并且通常不喜欢循环引用。

But whether you should or shouldn't use them depends a lot on the problem you are solving. 但是,是否应该使用它们在很大程度上取决于您要解决的问题。

Two (or more) things that hold references to each other (an object graph) won't keep themselves alive if that graph becomes isolated from a root reference. 如果两个(或多个)相互引用(一个对象图)与根引用隔离开,它们将无法保持活动状态。 However you can accidentally end up holding on to large graphs of things just by holding a reference to one thing in the object graph. 但是,仅通过在对象图中保留对某事物的引用,您可能会无意间结束对大型事物图的保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM