NHibernate 上值对象的单独表
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我们正在构建的系统中,我们将值对象放在单独的表中。据我所知,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 theid
in the object. This makes us freer to model the database the way we want and model the domain model the way we want.您可以加入并使其成为一个组件,允许 nHibernate 将其映射为正确的值对象而不是实体。
这样您就不需要任何虚拟属性,也不需要空的受保护的构造函数(它可以是私有的)。
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).