NHibernate 自定义集合类型
我有一个名为 Patient
的实体对象,该实体有一个名为 Visits
的属性,其类型为 VisitsCollection
。
VisitsCollections
是 IList
的子类,但它还向集合添加了一些自定义逻辑(例如自动排序、一些验证、通知等)。
我需要使用自定义集合类型,因为它将一些数据添加到添加到集合中的实体,并透明地执行一些其他文书工作。
现在我想在 NHibernate 中映射它,所以我创建了:
<list name="Visits" lazy="true" fetch="select">
<key foreign-key="PatientId" />
<index column="Timestamp" />
<one-to-many class="Visit" not-found="ignore"/>
</list>
我遇到了一个异常:
无法将“NHibernate.Collection.PersistentList”类型的对象转换为“...VisitsCollection”类型
每当我访问访问属性时,
。我也尝试过以这种方式映射它:
<list name="Visits" lazy="true" fetch="select" collection-type="VisitsCollection">
<key foreign-key="PatientId" />
<index column="Timestamp" />
<one-to-many class="Visit" not-found="ignore"/>
</list>
但是,我仍然遇到了这个异常:
自定义类型未实现 UserCollectionType: .....VisitsCollection
我不想从任何 NHibernate 类型继承我的 VisitsCollection
因为集合类是框架的一部分,我希望它是 DAL-不可知的(因为它将在许多场景中使用 - 不仅仅是数据库)。
关于如何映射它并保留我的代码结构有什么想法吗?
提前致谢。
I'm having an entity object called Patient
and this entity is having a property called Visits
which is of type VisitsCollection
.
VisitsCollections
is a child class of IList<Visit>
but it also adds some custom logic to the collection (like auto ordering, some validation, notifications, etc..).
I need to use the custom collection type as it adds some data to the entities that are added to the collection and performs some other paperwork transparently.
Now I want to map that in NHibernate, so I've created:
<list name="Visits" lazy="true" fetch="select">
<key foreign-key="PatientId" />
<index column="Timestamp" />
<one-to-many class="Visit" not-found="ignore"/>
</list>
I'm getting an exception:
Unable to cast object of type 'NHibernate.Collection.PersistentList' to type '...VisitsCollection'
Whenever I'm accessing the visits property.
I've also tried to map it this way:
<list name="Visits" lazy="true" fetch="select" collection-type="VisitsCollection">
<key foreign-key="PatientId" />
<index column="Timestamp" />
<one-to-many class="Visit" not-found="ignore"/>
</list>
but still, I'm getting this exception:
Custom type does not implement UserCollectionType: .....VisitsCollection
I don't want to inherit my VisitsCollection
from any NHibernate type as the collection class is part of a framework that I want it to be DAL-agnostic (as it will be used in many scenarios - not only with a database).
Any ideas on how to map this, preserving the structure of my code?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我从不使用自定义集合类型,主要是因为我很懒。我相信 NHibernate 希望您使用 IUserCollectionType,这需要一些管道。
相反,我的第一站是研究使用扩展方法 由 Billly McCafferty 讨论。但是您已经编写了这样的代码...
或者,您可以将集合映射为组件 科林·杰克在此讨论。这对于您的场景来说可能更容易?
另请检查此SO线程。
I never use custom collection types, mainly because I'm lazy. NHibernate wants you to use a IUserCollectionType I believe, which requires a bit of plumbing.
Rather than that, my first stop would be to look at using extension methods as discussed by Billly McCafferty. But you have code written so...
Alternatively, you could map your collection as a component as discussed here by Colin Jack. This might be easier for your scenario?
Also check this SO thread.
我也投票不使用自定义集合。无论如何,您可以通过组件来完成。
如何将 NHibernate 自定义集合与 FluentNHibernate 映射?
I also vote up not to use custom collections. Anyway, you can do it via component.
How to map NHibernate custom collection with fluentNHibernate?
仅供参考,以下是如何使用 FluentNHibernate 来做到这一点,恕我直言,
我们是否应该创建自定义集合类型是一个单独的主题
Just for reference, here is how you could do it using
FluentNHibernate
Whether we should or should not create a custom collection type is a separate topic IMHO