【问题标题】:What type to make child object of reference data?什么类型来制作参考数据的子对象?
【发布时间】:2011-01-23 22:06:56
【问题描述】:

我有一个对象CaseNote,它有许多子对象,例如CaseNoteContactType。对于 1 个 CaseNote,可以有 x 个 CaseNoteContactType。在 UI 中,ContactTypes 显示在 CheckListBox 中。

我的问题是如何表示 ContactType 子对象?这是一个简单的int|string 对。

public Class CaseNote
{
  public Guid CaseNoteID { get; set; }
  ...
  public ??? ContactType { get; set; }
  etc...
// Down here would be Methods for saving, loading, validating, etc...
}

ContactType 会是Dictionary 吗? IEnumerable<ContactType>? arraycollectionList<ContactType>??

在这些情况下有什么意义?如果没有 CaseNote,ContactType 就不能存在,但足以使其成为对象吗?我不明白每种类型的含义。另外,一个 CaseNote 可以有 0 到 30 个 ContactTypes 是否重要?

假设我确实创建了一个 ContactType 类,子类是否需要一个属性来存储它的父 ID?

非常感谢您的指导。

如果我离这里很远,那是因为我从来没有真正正确地设置业务对象,现在正在努力使我的环境适应我所阅读的内容。

【问题讨论】:

    标签: c# .net oop business-objects


    【解决方案1】:

    ContactType 听起来像是参考数据。一个 CaseNote 可能有 0 个或 N 个 ContactTypes,单个 ContactType 可能与多个 CaseNotes 相关联。对吗?

    如果是这样,我建议你创建一个名为 ContactType 的新类型,它包含两个属性,Id 和 Name(假设这就是 int 和 string 的用途):

    public class ContactType
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    然后在您的 CaseNote 类中,我会将它们声明为 List(或 IList 接口):

    public IList<ContactType> ContactTypes { get; set; }
    

    要检查他们是否有任何 ContactTypes 你可以这样做:

    if( myCase.ContactTypes.Count > 0 )
    {
        ...
    }
    

    【讨论】:

    • 嗯,我相信你有这个权利。 tblCaseNoteContactType 存储与它们关联的 CaseNoteID 和 ItemID,它是 ContactType,其描述存储在维护表中。有道理...
    猜你喜欢
    • 2021-09-03
    • 2023-01-09
    • 2011-11-15
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 2012-07-31
    相关资源
    最近更新 更多