How do you properly reference an entity inside an aggregate? For example, by the index of a list (doesn't seem smart if you re-order the list); a GUID (thought it was supposed to be local?); or some other incrementing counter?
For example:
class Cart
{
public IReadOnlyList<Item> Items => _items.ToList();
public AddItem(Item item)
{
}
public RemoveItem(/* Item or ItemId or index (int)? */) <- Here
{
_items.Remove(xxxxx); // <- And here
}
private readonly List<Item> _items = new List<Item>(); // Maybe need different data structure
}
class Item
{
// Local identity here?
public int Quantity { get; private set; }
public Item(Product product)
{
// use product properties needed
}
// other item properties
}
It seems wrong for a client to have to return an List<Item>
from the Cart
to then use the index to remove the Item. Additionally, if I instantiate the Item outside the class prior to adding it, how do I impose a local Id (I would have a GUID if instantiating outside)?
TL;DR how to deal with local identities of entities (i.e. how local is "local")?