📓 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:
...
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:
...
[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
Itemand add it to aList.Then we create a new
Categoryand call the soon-to-be-createdAddItemmethod upon it, passing in our sampleItem.Next, we call
newCategory.Items, to retrieve theItems saved in ourCategory.Finally, we assert that
newCategory.Itemsshould return aListcontaining our singleItem.
Now let's create the AddItem() method necessary to run and pass this test:
...
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.