【问题标题】:why my mapping work with IList and not with LIst为什么我的映射适用于 IList 而不是 LIst
【发布时间】:2013-01-12 17:53:50
【问题描述】:

真正大师的简单问题。 我浪费了很多时间来弄清楚如何在 nhib 中映射集合。通过代码映射,我现在有疑问,为什么我的映射适用于 IList 类型的集合而不是 List

这是代码

public class Account {
    private IList<Term> Terms; // When I use List it does not work
    public Account()
    {
       Terms = new List<Terms>(); 
    }
    public virtual IList<Term> Terms // When I use List it does not work
    {
       get { return _Terms; }
       set 
       { if (_Terms == value) return;
           _Terms = value;
       }
    }
}

AccountMap.cs(一个账户有多个条款)

Bag(x => x.Terms,
         m =>{},
         x => x.OneToMany()
);

【问题讨论】:

  • 怎么不行?我认为 NHibernate 要求您为要延迟加载的映射集合使用接口类型,因为它需要在运行时代理它们。

标签: c# nhibernate


【解决方案1】:

文档说:6.1. Persistent Collections:

NHibernate 要求持久的集合值字段 声明为接口类型

以及支持的接口列表:

实际的接口可能是Iesi.Collections.ISet, System.Collections.ICollection, System.Collections.IList, System.Collections.IDictionary, System.Collections.Generic.ICollection&lt;T&gt;, System.Collections.Generic.IList&lt;T&gt;, System.Collections.Generic.IDictionary&lt;K, V&gt;, Iesi.Collections.Generic.ISet&lt;T&gt;

或者...任何你喜欢的东西! (“你喜欢的任何东西”意味着你必须编写NHibernate.UserType.IUserCollectionType 的实现。)

【讨论】:

    【解决方案2】:

    NHibernate 与 IList 和 IList(以及其他一些集合接口类型)一起使用,因为 NHibernate 在内部使用这些接口的自己的实现来跟踪更改等。

    从对象设计的角度来看,在您的域类中公开集合接口类型而不是具体的集合实现也是合理的。

    【讨论】:

      猜你喜欢
      • 2011-07-03
      • 2011-03-06
      • 1970-01-01
      • 2010-12-22
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2016-08-13
      相关资源
      最近更新 更多