Should a Parent with Children have a DefaultChild, or should a Child have a Default property?

Posted by Stijn on Programmers See other posts from Programmers or by Stijn
Published on 2014-06-02T14:42:15Z Indexed on 2014/06/02 15:56 UTC
Read the original article Hit count: 299

Filed under:
|

Which of the following two models makes more sense? I'm leaning towards the first one because there can only be one default child.

The examples are in C# but I think it can apply to other languages too.

Here DefaultChild holds one of the items in Children.

class Parent
{
    int ID { get; set; }

    Child DefaultChild { get; set; }

    IEnumerable<Child> Children { get; set; }
}

class Child
{
    int ID { get; set; }
}

Here one of the items in Children has Default set to true while the others have it set to false.

class Parent
{
    int ID { get; set; }

    IEnumerable<Child> Children { get; set; }
}

class Child
{
    int ID { get; set; }

    bool Default { get; set; }
}

A concrete situation: a User in our system has one or more Customers attached. When logging in, if said User has a default Customer, they are immediately working under this Customer. If they don't, they have to select a Customer to work under. While logged in, they can switch between Customers.

© Programmers or respective owner

Related posts about c#

Related posts about modeling