在对象模型中实现多对一关系
假设一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我喜欢这个问题。与大多数设计决策一样,这是与上下文相关的。这个要怎么用呢。
假设我在西雅图有一个主要地址,在凤凰城有一个避暑别墅,还有一个用于商业目的的邮政信箱。我的主要地址是否比其他地址具有特殊优先级,或者地址的选择是任意的?在尝试我的避暑别墅或邮政信箱之前,您会尝试在西雅图找到我吗?
在以不同方式对待主地址的情况下,我会将其存储在明确单独的位置。否则,将它们存储在任意排序的集合中,并强制要求类实现中至少有一个。
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.