Is it okay to have a list of objects in an object?

I'm new to OO design and I'm wondering if it's typical to have projects where objects contain lists of other objects. Below is an example:

// Person object containing a list of phone numbers
public class Person
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Phone> Contacts { get; set; }

    public void AddPhoneNumber(Phone phone)
    {
        Contacts.Add(phoneNumber);
    }
}

// Phone object
public class Phone
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Number { get; set; }
}

// Example code to establish an object:
Person p1 = new Person();
p1.FirstName = "John";
p1.LastName = "Smith";
p1.AddPhoneNumber(new Phone() { Number = "555957000" });
p1.AddPhoneNumber(new Phone() { Number = "555579561" });

      

Is there a better way to structure this, making it easier to design and easier to access the data? Thank!

+3


source to share


4 answers


Yes, it's perfectly okay for an object to contain a list of objects. This OOPs

is called Composition , which represents a strong relationship between participation class

.



+6


source


I'm wondering if it's typical to have projects where objects contain lists of other objects.

This is absolutely fine, as an object can only contain lists that belong only to that particular object. One of the many examples is when you are viewing a binary tree or you can have each node its own list that identifies their children. There are many more cases where an object should / could contain its own list.

Going back to your code, you seem to have an error because the code below states that the list will contain objects Phone

.

public List<Phone> Contacts { get; set; }

      



but still you are passing a string object, not a phone object.

public void AddPhoneNumber(string phoneNumber)  
{
     Contacts.Add(phoneNumber); // this code shouldnt compile
}

      

You can most likely do the following:

public void AddPhoneNumber(string phoneNumber)  
{
    Contacts.Add(new Phone() { Number = phoneNumber });
}

      

+1


source


There is nothing wrong with the code. This is perfectly true in order to have a list of objects within an object.

Although adding a string to the List of Phone object will throw an error.

You can add a phone number like this:

Person p1 = new Person();
p1.Contacts.Add(new Phone() { Number = "555579561" });

      

0


source


What you are doing works great with data, but when it comes time to store or retrieve the contact list for a given object, you can run into problems.

From what I understand, you will need to either serialize and deserialize the list each time to store it in a database field, or alternatively create a PersonContacts table that has two fields, personID and contactID for each contact.

Someone please correct me if there is a better solution for this.

0


source







All Articles