When I have entities in my domain with lists of things, should they be exposed as ILists or IEnumerables? E.g. Order has a bunch of OrderLines.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
IEnumerable<T>represents a series of items that you can iterate over (using foreach, for example), whereasIList<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.