📓 3.2.3.3 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 Category
s. 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 Item
s with Description
s 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 Item
s 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
Item
and add it to aList
.Then we create a new
Category
and call the soon-to-be-createdAddItem
method upon it, passing in our sampleItem
.Next, we call
newCategory.Items
, to retrieve theItem
s saved in ourCategory
.Finally, we assert that
newCategory.Items
should return aList
containing 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.