NHibernate Custom Collections 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);
}
}
}
这就像从数据库加载数据的魅力,而不必放弃我相当有用的自定义集合班级。
然后我继续尝试实现保存,并遵循建议
现在,当我在加载后提交时,NHibernate 会抛出 InvalidCastException
:“无法将类型为 'My.CustomList`1[Domain.Object, DomainAssembly]' 的对象转换为类型为 'Iesi.Collections.ISet' ”。
有没有办法让这项工作按照我期望的方式进行?
编辑:
按照Raphael提供的指导,我尝试切换到ICollection
,当我提交事务时,它给了我一个不同的InvalidCastException
:无法转换类型为'的对象My.CustomList`1[Domain.Object]' 以键入“NHibernate.Collection.IPercientCollection”。
Following the technique described here, I was able to populate a domain object that uses custom collections for its children. The relevant property mapping looks like this:
<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>
My custom collection class exposes the InnerList property as an ICollection
like so:
protected System.Collections.ICollection InnerList
{
get
{
return this;
}
set
{
Clear();
foreach (DomainObject o in value)
{
Add(o);
}
}
}
This worked like a charm to load data from the database and not have to abandon my rather useful custom collection class.
Then I moved on to try implement saving, and following the advice given in this thread, decided to wrap every call to NHibernate in a transaction.
Now when I commit following my load, NHibernate throws an InvalidCastException
: "Unable to cast object of type 'My.CustomList`1[Domain.Object, DomainAssembly]' to type 'Iesi.Collections.ISet'."
Is there a way to make this work the way I expect it to?
EDIT:
Following the lead provided by Raphael, I tried switching to ICollection<T>
which gives me a different InvalidCastException
when I commit the transaction: Unable to cast object of type 'My.CustomList`1[Domain.Object]' to type 'NHibernate.Collection.IPersistentCollection'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将属性更改为类型
Change the property to be of type