What is the best practice for readonly lists in NHibernate

后端 未结 5 1710
北荒
北荒 2020-12-06 02:32

Domain Model I am working on has root aggregate and child entities. Something like the following code:

class Order
{
   IList Lines {get;se         


        
5条回答
  •  感情败类
    2020-12-06 02:54

    The pattern I use is:

    class Order
    {
       private List lines = new List();
    
       IEnumerable Lines { get { return this.lines; } }
    
       void AddLine(OrderLine line)
       {
           this.orders.Add(line);
       }
    }
    

    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:

    Order.Lines[0] = new OrderLine().
    

提交回复
热议问题