Referencing entity inside aggregate by local ID

by keelerjr12   Last Updated November 14, 2018 19:05 PM

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")?



Related Questions


Domain model design

Updated July 23, 2016 08:02 AM


What is a domain?

Updated October 23, 2017 18:05 PM