views:

704

answers:

3

Value objects got no identity. ORM needs identity to update database.

How to trick ORM?

(marking Id for value object as internal won't work, cause ORM lives in different assembly and moving it to the same assembly is not acceptable).

Thanks in advance.

+1  A: 

Personally I have the Id field in the value object - I treat it as another attribute of the value object (such as name, location etc).

It may not be true DDD but it works for me.

TWith2Sugars
+6  A: 

As far as my understanding of DDD goes value objects are just a way to partition your entities. If a value object should be stored with an ID in the database it's not a value object.

Example:

The domain model looks like this (C#):

public class Customer : Entity
{
    public Guid CustomerID { get; }

    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address : ValueObject
{
    public string Street { get; set; }

    public string City { get; set; }

    public string ZipCode { get; set; }
}

The corresponding database table would look something like this (Pseudo-SQL):

CREATE TABLE Customers
(
    CustomerID,

    LastName,

    HomeAddress_Street,

    HomeAddress_City,

    HomeAddress_ZipCode,
)

To store the addresses in a seperate table you would make it an entity which has an ID.

Albic
But then the domain model is just 1:1 of the database, the address can still be a value object and still have a separate table.
TWith2Sugars
No, it is not a 1:1 of the Database.You have a Customer class and an Adress class (which is the value object). In NHibernate, a value object is mapped as a component.As soon as you have an entity which needs an Id, it is no longer a value object.
Frederik Gheysels
But if you need to save the address in a table it will require an id by the database. Just because the database requires an id doesn't mean the object is instantly an entity.
TWith2Sugars
Hopefully this question ma provide more information: http://stackoverflow.com/questions/679005/how-are-value-objects-stored-in-the-database
TWith2Sugars
In this case - how to "hide" that id of VO. I`m not looking for way to completely get rid of it, cause then i wouldn`t be able to store VO data in another table.
Arnis L.
Make it private and have a function called "GetPersistantData()" that returns it?
TWith2Sugars
+6  A: 

When Eric Evans talks about "entities have identity, Value Objects do not", he's not talking about an ID column in the database - he's talking about identity as a concept.

VOs have no conceptual identity. That doesn't mean that they shouldn't have persistence identity. Don't let persistence implementation cloud your understanding of Entities vs VOs.

See my post here.

Vijay Patel
But i would like to show that lack of conceptual identity in code. Btw, i've seen that post before.
Arnis L.
To clarify your requirement: you want to hide the ID property on your VO object, but you need the ORM to see the ID property?Two questions:1) Can your ORM access private/internal fields? (like NHibernate)2) How much benefit do you get by 'hiding' the VO's ID property?
Vijay Patel
Forgot to mention - i am using NHibernate... So - how to do it with NHibernate?
Arnis L.
As an idea, you could try using Explicit Interface Implementations (see http://blog.briandicroce.com/2007/11/13/explicit-iterface-members-implementation-in-c).Create an interface that with the ID property, then implement the ID property explicitly on your entity class. I've not tried it myself - let me know if it works!
Vijay Patel

related questions