NHibernate上的值对象的单独表 [英] Separate table for Value Objects on NHibernate

查看:81
本文介绍了NHibernate上的值对象的单独表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是DDD和NHibernate的新手.

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

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

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

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

我应该如何使用NHibernate设计它?我应该已经将我的Address对象设为Entity吗?

对不起,我什至不知道我的问题是否很清楚,我真的在这里迷路了.

解决方案

在我们正在构建的系统中,我们将Value-Objects放置在单独的表中.据我所知,NHibernate要求必须在对象中添加id,但是我们忽略了这一点,并将该对象视为系统中的Value-Object.您可能知道,Value-Object是不需要跟踪的对象,因此我们只是忽略了该对象中的id.这样一来,我们就可以自由地以所需的方式对数据库进行建模,并以所需的方式对域模型进行建模.

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.

解决方案

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.

这篇关于NHibernate上的值对象的单独表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆