Search code examples
nhibernatedomain-driven-designentityvalue-objects

Separate table for Value Objects on 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.


Solution

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