DDD - If an object is saved as a whole to the database, is it per definition an entity, or is it still possible that it is a value object?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/258170

Question

I'm learning about Domain Driven Design and struggling with the question if a particular object should be handled as entity or value object.

All possible surname prefixes are kept in a database table. (The data is used in a combo box). Since there are records kept of all these prefixes, should Prefix be seen as an entity? Or is it still possible to handle it as a value object?

Was it helpful?

Solution

You already gave the answer yourself in your comment to the other question: If two Prefix objects with the same content are interchangeable, then Prefix is (probably) a value object.

Another rule of thumb is: "Domain first, infrastructure second", i.e., don't let infrastructure concerns (here: primary keys in database tables) influence your domain model.

OTHER TIPS

I thought the difference was the linkage with the database. So an Entity object has state that maintains its place in the DB, whereas a value object simply contains the data.

Writing the object back to the DB ends up as a update call in both cases, however the entity will use the internally-held key to refer back to the DB row, whereas the value object needs to get the key from its data... not much difference in practice ultimately!

I'd prefer a value object as I'm old-fashioned that way, I'd read only what was needed and update just that data. Less data and less state is always good.

Licensed under: CC-BY-SA with attribution
scroll top