NHibernate 自定义集合类型

发布于 2024-08-23 13:22:31 字数 1283 浏览 5 评论 0原文

我有一个名为 Patient 的实体对象,该实体有一个名为 Visits 的属性,其类型为 VisitsCollection

VisitsCollectionsIList 的子类,但它还向集合添加了一些自定义逻辑(例如自动排序、一些验证、通知等)。

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

现在我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你对谁都笑 2024-08-30 13:22:31

我从不使用自定义集合类型,主要是因为我很懒。我相信 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.

傾旎 2024-08-30 13:22:31

我也投票不使用自定义集合。无论如何,您可以通过组件来完成。

<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>

如何将 NHibernate 自定义集合与 FluentNHibernate 映射?

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?

暖阳 2024-08-30 13:22:31

仅供参考,以下是如何使用 FluentNHibernate 来做到这一点,恕我直言,

我们是否应该创建自定义集合类型是一个单独的主题

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
                });
        }
}

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

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
                });
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文