If I have a aggregate object e.g. Order –> OrderLine where my Order object is identified as the aggregate root, therefore to add OrderLine to the Order I would expect to do so through the aggregate root and by no other means e.g. Order.AddOrderLine(OrderLine line).
The Order object obviously exposes a collection of OrderLines, but how do I prevent consumers using this collection directly to add OrderLines, I assume the answer is to use a Readonly collection?? Does this stop consumers changing the state of the objects i.e. OrderLines within the collection??
Thanks
Expose your orderLines as IEnumerable < OrderLine > and implement Add/Remove methods as necessary. That way your clients can only iterate on the collection, not manipulate it w/o going thru your aggregate.