Berryl
Berryl

Reputation: 12833

implementing many to one relationships in object model

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 {....}
}

Upvotes: 1

Views: 66

Answers (1)

Amish Programmer
Amish Programmer

Reputation: 2121

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.

Upvotes: 2

Related Questions