Skip to main content

📓 Saving Objects Within Other Objects

In the last lesson, we set up our new Category class with basic functionality like getters, a constructor, unique IDs, and a static list to contain all Categorys. Now we can focus on tying together our Category and Item classes. That way, a Category object with a Name like "School" can hold many different To Do List Items with Descriptions like "Finish section 2 code review," "Email teacher about planned absence," and so on.

Saving Objects Within Objects


When we created our Category class, we included an Items property. It's a List that is empty at first:

ToDoList/Models/Category.cs
...

public class Category
{
...
public List<Item> Items { get; set; }

public Category(string categoryName)
{
...
Items = new List<Item>{};
}

...

This is where Items related to the parent Category will be stored. Let's add this functionality now. What's the next simplest behavior we can implement? First, let's make sure we can add an Item object into the Items property of a Category object. Here's the test:

ToDoList.Tests/ModelTests/CategoryTests.cs
...

[TestMethod]
public void AddItem_AssociatesItemWithCategory_ItemList()
{
//Arrange
string description = "Walk the dog.";
Item newItem = new Item(description);
List<Item> newList = new List<Item> { newItem };
string name = "Work";
Category newCategory = new Category(name);
newCategory.AddItem(newItem);

//Act
List<Item> result = newCategory.Items;

//Assert
CollectionAssert.AreEqual(newList, result);
}

...
  • We create a new Item and add it to a List.

  • Then we create a new Category and call the soon-to-be-created AddItem method upon it, passing in our sample Item.

  • Next, we call newCategory.Items, to retrieve the Items saved in our Category.

  • Finally, we assert that newCategory.Items should return a List containing our single Item.

Now let's create the AddItem() method necessary to run and pass this test:

ToDoList/Models/Category.cs
...

public void AddItem(Item item)
{
Items.Add(item);
}

...

AddItem() will accept an Item object and then use the built-in List Add() method to save that item into the Items property of a specific Category.

If we run our tests again, they should all pass. We're successfully saving objects of one type within objects of another type. In the next lesson, we'll integrate this new functionality into the MVC front end user interface of our application.