在对象模型中实现多对一关系

发布于 2024-11-25 09:13:26 字数 229 浏览 5 评论 0原文

假设一个 Person 对象可能有多个地址,但必须有一个。

您是否赞成在对象模型中显式显示所需的一个地址?如果是这样,有什么实施技巧吗?

干杯,
贝里尔

Person{
    Address TheRequiredAddress {get;}    

    IList<Address> OtherAddresses {....}
}

Suppose a Person object may have many Addresses but must have one

Do you favor making the one required Address explicit in the object model? If so, any implementation tips?

Cheers,
Berryl

Person{
    Address TheRequiredAddress {get;}    

    IList<Address> OtherAddresses {....}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风疏影 2024-12-02 09:13:26

我喜欢这个问题。与大多数设计决策一样,这是与上下文相关的。这个要怎么用呢。

假设我在西雅图有一个主要地址,在凤凰城有一个避暑别墅,还有一个用于商业目的的邮政信箱。我的主要地址是否比其他地址具有特殊优先级,或者地址的选择是任意的?在尝试我的避暑别墅或邮政信箱之前,您会尝试在西雅图找到我吗?

在以不同方式对待主地址的情况下,我会将其存储在明确单独的位置。否则,将它们存储在任意排序的集合中,并强制要求类实现中至少有一个。

I like this question. As with most design decisions, this is contextual. How will this be used.

Assume I have a primary address in Seattle and a summer house in Phoenix, and a PO box for business purposes. Will my primary address have special priority over my other addresses, or is the choice of address arbitrary? Will you try to locate me in Seattle before you try my summer house or my PO box?

In the case where the primary address is treated differently, I would store it in an explicitly separate location. Otherwise, store them in an arbitrarily ordered collection and enforce the requirement for at least one within the class implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文