NHibernate 上值对象的单独表

发布于 2024-08-03 02:58:57 字数 429 浏览 5 评论 0原文

我是 DDD 和 NHibernate 的新手。

在我当前的项目中,我有一个实体 Person,它包含一个值对象,比如说 Address。今天,这很好。但也许有一天我会要求我的值对象(在本例中为地址)必须成为一个实体。

在尝试以 DDD 方式对此进行建模之前,采用更以数据为中心的方法,我有一个带有 Id 的表 Person,以及另一个表 Address,其 PK 实际上是 FK,它是一个人的 Id(即,一对一的关系)。

我一直在阅读,当我将值对象映射为组件时,它的值将被映射为实体表上的列(因此,我不会有一对一的关系)。

我的想法是,当需要时,我只需向我的地址表添加一个代理键,然后它就成为一个实体。

我应该如何使用 NHibernate 来设计它?我应该已经将我的地址对象设置为实体吗?

抱歉,我什至不知道我的问题是否清楚,我真的迷失了这里。

I'm new to DDD and NHibernate.

In my current project, I have an entity Person, that contains a value object, let's say Address. Today, this is fine. But maybe one day I will have a requirement that my value object (in this case Address), will have to become an entity.

Before trying to model this on a DDD-way, in a more data-centric approach, I had a table Person, with an Id, and another table Address, whose PK was actually an FK, it was the Id of a Person (ie, a one-to-one relationship).

I've been reading that when I map a Value Object as a Component, its value will get mapped as columns on my Entity table (so, I would not have the one-to-one relationship).

My idea was that, when needed, I would simply add a surrogate key to my Address table, and then it becomes an Entity.

How should I design this using NHibernate? Should I already make my Address object an Entity?

Sorry, I don't even know if my questions are clear, I'm really lost here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

七婞 2024-08-10 02:58:58

在我们正在构建的系统中,我们将值对象放在单独的表中。据我所知,NHibernate要求必须向对象添加一个id,但我们忽略了这一点,并将该对象视为系统中的值对象。您可能知道,值对象是您不需要跟踪的对象,因此我们只需忽略对象中的 id 即可。这使我们能够更自由地按照我们想要的方式对数据库进行建模,并按照我们想要的方式对领域模型进行建模。

In the system we are building, we put Value-Objects in separate tables. As far as I know, NHibernate requires that an id must added to the object, but we ignore this and treat the object as a Value-Object in the system. As you probably know, a Value-Object is an object that you don't need to track, so we simply overlook the id in the object. This makes us freer to model the database the way we want and model the domain model the way we want.

墟烟 2024-08-10 02:58:58

您可以加入并使其成为一个组件,允许 nHibernate 将其映射为正确的值对象而不是实体。

这样您就不需要任何虚拟属性,也不需要空的受保护的构造函数(它可以是私有的)。

Join("PROPOSAL_PRODUCT", product =>
{
    product.Schema(IsaSchema.PROPOSALOWN);
    product.KeyColumn("PROPOSAL_ID");

    product.Component(Reveal.Member<Proposal, Product>("_product"), proposalProduct =>
    {
        proposalProduct.Map...
    });
});

You can Join and make it a Component allowing nHibernate to map it as a proper value object instead of an entity.

This way you won't need any virtual properties nor an empty protected ctor (it can be private).

Join("PROPOSAL_PRODUCT", product =>
{
    product.Schema(IsaSchema.PROPOSALOWN);
    product.KeyColumn("PROPOSAL_ID");

    product.Component(Reveal.Member<Proposal, Product>("_product"), proposalProduct =>
    {
        proposalProduct.Map...
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文