2010-09-07 25 views
2

按照描述的技术here,我能够填充为其子级使用自定义集合的域对象。有关属性映射看起来是这样的:NHibernate自定义集合hack在事务之外工作,但不在里面

<component name="Contacts" class="My.CustomList`1[[Domain.Object, DomainAssembly]], MyAssembly"> 
     <set name="InnerList"> 
      <key column="PARENT_ID" /> 
      <one-to-many class="Contact" /> 
     </set> 
    </component> 

我的自定义集合类公开InnerList属性作为ICollection像这样:

protected System.Collections.ICollection InnerList 
    { 
     get 
     { 
      return this; 
     } 
     set 
     { 
      Clear(); 
      foreach (DomainObject o in value) 
      { 
       Add(o); 
      } 
     } 
    } 

这工作就像一个魅力从数据库加载数据,而不必放弃我相当有用的自定义集合类。

然后我继续尝试实施保存,并按照建议given in this thread,决定在事务中将每次调用包装到NHibernate中。

现在,当我提交以下我的负载时,NHibernate抛出一个InvalidCastException:“无法强制'My.CustomList`1 [Domain.Object,DomainAssembly]类型的对象键入'Iesi.Collections.ISet'。

有没有办法让我的工作方式符合我的预期?

编辑:

继拉斐尔提供的领先优势,我想切换到ICollection<T>,给了我不同的InvalidCastException当我提交事务:无法转换类型My.CustomList`1 [Domain.Object的”对象]'键入'NHibernate.Collection.IPersistentCollection'。

+1

你InnerList(因此 - 您的CustomCollection)创建时 - 确实,从Iesi.Collections实现ISet的?如果它不 - 这就是你的问题所在。 – Goblin 2010-09-07 20:55:25

+0

我试图避免在我的域名中存在对Iesi.Collections的依赖,这就是为什么我在第一个地方使用“hack”的原因。 – Dan 2010-09-08 12:06:08

回答

1

更改属性为类型

IList<T> 
+1

使用IList 和一组映射会导致初始查询在尝试将NHibernate.Collection.Generic.PersistentGenericSet'1 [MyDomain.Contact]强制转换为System.Collections.Generic.IList'1 [MyDomain.Contact]时失败。 PersistentGenericSet支持ICollection和ICollection,但不支持IList 。 – Dan 2010-09-08 12:37:47

相关问题