IList vs IEnumerable for Collections on Entities
Solution 1:
IEnumerable<T>
represents a series of items that you can iterate over (using foreach, for example), whereas IList<T>
is a collection that you can add to or remove from.
Typically you'll want to be able to modify an Order by adding or removing OrderLines to it, so you probably want Order.Lines to be an IList<OrderLine>
.
Having said that, there are some framework design decisions you should make. For example, should it be possible to add the same instance of OrderLine to two different orders? Probably not. So given that you'll want to be able to validate whether an OrderLine should be added to the order, you may indeed want to surface the Lines property as only an IEnumerable<OrderLine>
, and provide Add(OrderLine) and Remove(OrderLine) methods which can handle that validation.
Solution 2:
Most of the time I end up going with IList over IEnumerable because IEnumerable doesn't have the Count method and you can't access the collection through an index (although if you are using LINQ you can get around this with extension methods).