简体   繁体   English

NHibernate自定义集合类型

[英]NHibernate custom collection type

I'm having an entity object called Patient and this entity is having a property called Visits which is of type VisitsCollection . 我有一个名为Patient的实体对象,这个实体有一个名为Visits的属性,其类型为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..). VisitsCollectionsIList<Visit>的子类,但它也为集合添加了一些自定义逻辑(如自动排序,一些验证,通知等)。

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: 现在我想在NHibernate中映射它,所以我创建了:

<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' 无法将'NHibernate.Collection.PersistentList'类型的对象强制转换为'... 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 自定义类型未实现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). 我不想从任何NHibernate类型继承我的VisitsCollection ,因为集合类是我希望它与DAL无关的框架的一部分(因为它将在许多场景中使用 - 不仅仅用于数据库)。

Any ideas on how to map this, preserving the structure of my code? 关于如何映射这个,保留我的代码结构的任何想法?

Thanks in advance. 提前致谢。

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. NHibernate希望你使用我相信的IUserCollectionType,这需要一些管道。

Rather than that, my first stop would be to look at using extension methods as discussed by Billly McCafferty . 而不是那样,我的第一站是看Billly McCafferty讨论的使用扩展方法。 But you have code written so... 但你有代码写的......

Alternatively, you could map your collection as a component as discussed here by Colin Jack . 或者,您可以将您的集合映射为组件,如Colin Jack所述 This might be easier for your scenario? 对于您的方案,这可能更容易?

Also check this SO thread . 还要检查此SO线程

I also vote up not to use custom collections. 我也投票不使用自定义集合。 Anyway, you can do it via component. 无论如何,你可以通过组件来做到这一点。

<component name="Warehouses" class="Core.Domain.Collections.EntitySet`1[Core.Domain.OrgStructure.IWarehouseEntity,Core],Core">
<set name="_internalCollection" table="`WAREHOUSE`" cascade="save-update" access="field" generic="true" lazy="true" >
  <key column="`WarehouseOrgId`" foreign-key="FK_OrgWarehouse" />
  <!--This is used to set the type of the collection items-->
  <one-to-many class="Domain.Model.OrgStructure.WarehouseEntity,Domain"/>
</set>

How to map NHibernate custom collection with fluentNHibernate? 如何使用fluentNHibernate映射NHibernate自定义集合?

Just for reference, here is how you could do it using FluentNHibernate 仅供参考,以下是使用FluentNHibernate

Whether we should or should not create a custom collection type is a separate topic IMHO 我们是否应该创建自定义集合类型是一个单独的主题恕我直言

public class PatientOverride : IAutoMappingOverride<Patient>
{
        public void Override(AutoMapping<Patient> mapping)
        {
             mapping.Component(
                x => x.Visits,
                part =>
                {
                    part.HasMany(Reveal.Member<VisitsCollection, IEnumerable<Visit>>("backingFieldName")) // this is the backing field name for collection inside the VisitsCollection class
                    .KeyColumn("PatientId")
                    .Inverse(); // depends on your use case whether you need it or not
                });
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM