NHibernate自定义集合类型

Kar*_*gha 7 collections nhibernate nhibernate-mapping nhibernate-collections

我正在调用一个实体对象,Patient并且该实体具有一个名为Visitstype 的属性VisitsCollection.

VisitsCollections是一个子类,IList<Visit>但它也为集合添加了一些自定义逻辑(如自动排序,一些验证,通知等).

需要使用自定义集合类型,因为它会将一些数据添加到添加到集合中的实体,并透明地执行其他一些文书工作.

现在我想在NHibernate中映射它,所以我创建了:

<list name="Visits" lazy="true" fetch="select">
    <key foreign-key="PatientId" />
    <index column="Timestamp" />
    <one-to-many class="Visit" not-found="ignore"/>
</list>
Run Code Online (Sandbox Code Playgroud)

我得到一个例外:

无法将'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>
Run Code Online (Sandbox Code Playgroud)

但是,我还是得到了这个例外:

自定义类型未实现UserCollectionType:..... VisitsCollection

我不想VisitsCollection从任何NHibernate类型继承我,因为集合类是我希望它与DAL无关的框架的一部分(因为它将在许多场景中使用 - 不仅仅用于数据库).

关于如何映射这个,保留我的代码结构的任何想法?

提前致谢.

tob*_*ris 6

我从不使用自定义集合类型,主要是因为我很懒.NHibernate希望你使用我相信的IUserCollectionType,这需要一些管道.

而不是那样,我的第一站是看Billly McCafferty讨论的使用扩展方法.但你有代码写的......

或者,您可以将您的集合映射为组件,如Colin Jack所述.对于您的方案,这可能更容易?

还要检查此SO线程.