views:

73

answers:

1

Which approach is recommended in a DDD world... and why ?

  1. aggregateRoot.Items.Add(...)
  2. aggregateRoot.AddItem(...)

I think the first option is better since it is more related to the ubiquitous language.

Should I expose a readonly (IEnumerable) collection and some AddItem()/RemoveItem()/etc on the aggregateRoot (option 1) or expose a strongly typed collection (I don't like exposing IList< T > or even worst, List< T >) that supports adding/removing/etc (option 2).

+2  A: 

Both have their uses, depending on what kind of API that you want to present to your user.

If you're encapsulating a collection that you don't want users directly accessing, then you could use AddItem() as a fairly self-discoverable method to assist users in adding an item to it.

If your object exposes the collection as-is, then Items.Add() is consistent collection behaviour and would likely be the best choice.

womp
Yes, you may be right... but I wanted an answer in terms of DDD and ubiquitous language. That is the reason I wrote aggreagteRoot. I'll edit my question to be clearer and I'll still vote your answer up.
W3Max