2

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?

Bart Weber
  • 297
  • 1
  • 2
  • 8

2 Answers2

7

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.

-1

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.

gbjbaanb
  • 48,354
  • 6
  • 102
  • 172
  • "I thought the difference was the linkage with the database." But what about this: [The distinction between Entity and Value object should be based around the question: If I have two objects with the same contents (two AdvertisementEvents linking to the same Banner with the same parameters), should I treat them differently or can one be replaced by the other without affecting how the software works?](http://programmers.stackexchange.com/a/196936/125534) Then I would say: surname prefixes could be handled as value objects. – Bart Weber Oct 06 '14 at 09:06
  • The difference in the semantics in the Domain Model. A value object needs to be immutable, but can be persisted. It can be a field, a group of field or a separate table too. The way it's persisted is independent from its behaviour. – ZioBrando Oct 06 '14 at 17:20