簡體   English   中英

父對象的集合成員的對象是否也應該引用父對象?

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

因此,我有一個ContactCardGroup類,其中包含ContactCardGroupMemberships的集合:

 public class ContactCardGroup : AbstractEntity, IContactCardGroup
    {

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

還有一個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>();
        }
    }

因此,在整個代碼中,如果我有一個ContactCardGroup成員而不是GroupId,將很有幫助....但是這不是在處理/銷毀方面引入麻煩的循環引用嗎?是否存在此類規則/模式?

自從我安裝ReSharper以來,似乎對我所做的任何編碼選擇都不再有信心。

您可以有循環引用,它們可能非常有用。 大多數ORM都很好地處理它們。

但是,如果您使用序列化庫,那么它們可能會有些棘手,並且通常不喜歡循環引用。

但是,是否應該使用它們在很大程度上取決於您要解決的問題。

如果兩個(或多個)相互引用(一個對象圖)與根引用隔離開,它們將無法保持活動狀態。 但是,僅通過在對象圖中保留對某事物的引用,您可能會無意間結束對大型事物圖的保留。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM