哪些是NHibernate的只读列表中的最佳实践 [英] What is the best practice for readonly lists in NHibernate

查看:259
本文介绍了哪些是NHibernate的只读列表中的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

域模型我的工作有根总量和子实体。 类似下面的code:

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的直接映射到线字段

NHibernate is mapped directly to the lines field.

现在的问题...

  • 你在这种情况下的做法?不要
  • 是否有人使用方法: 公开的IEnumerable GetLines()
  • 你在使用的财产返回类型
  • ?可能是ReadOnlyCollection还或IEnumerable的;
  • 在可能这是不是要问最好的地方?建议请。
  • 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.

更新:好像IEnumerable的胜利,但解决方案仍然是不完美的......

Update: Seems IEnumerable wins, however solution still is not perfect...

推荐答案

我使用的模式是:

class Order
{
   private List<OrderLine> lines = new List<OrderLine>();

   IEnumerable<OrderLine> Lines { get { return this.lines; } }

   void AddLine(OrderLine line)
   {
       this.orders.Add(line);
   }
}

如果您使用的是.NET 3.5,你得到所有你能想到的使用LINQ IEnumerable的搜索功能,并且隐藏您的收藏实施。

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().

这篇关于哪些是NHibernate的只读列表中的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆