Domain Model I am working on has root aggregate and child entities. Something like the following code:
class Order { IList<OrderLine> Lines {get;set;} } class OrderLine { }
Now I want my Order to control lines. Something like that:
class Order { OrderLine[] Lines {get;} void AddLine(OrderLine line); }
At this time we are using the following pattern:
class Order { private IList<OrderLine> lines = new List<OrderLine>(); OrderLine[] Lines {get {return this.lines;}} void AddLine(OrderLine line) { this.orders.Add(line); { }
NHibernate is mapped directly to the lines field.
Now questions…
- What do you practice in such situations?
- Does anyone use methods: public IEnumerable GetLines()
- What are you using as return type for property? May be ReadOnlyCollection or IEnumerable;
- May be this is not the best place to ask? Suggest please.
Update: Seems IEnumerable wins, however solution still is not perfect…
The pattern I use is:
If you’re using NET 3.5 you get all the search functionality you could want for IEnumerable using LINQ, and you hide your collection implementation.
The problem with returning OrderLine[] is that your collection can be modified externally eg: