How to differentiate Entity and ValueObject?
For my recent project using DDD, I am often confused by the two.
- What's Entity and VO?
- The responsibility
- Should Entity and VO contain any logics, like date parsing, returning a bool by the fields?
How to differentiate Entity and ValueObject?
For my recent project using DDD, I am often confused by the two.
I also wish it was an easy way of rule of thumb for determining if your new model is Entity
or ValueObject
?
Personally I am using following question:
Do I have any property in my model that history of it's changes matters?
Entity
ValueObject
So using it in example, Person
model depends to our domain(logic/bussiness), history of it's FirstName
property may matters or not. if it does then Person
is Entity
.
In the other hand we must be able to set updatable = false
for our ValueObject
properties. and when update is necessary, simply we be able to delete previous record and insert new one[1].
About your other questions, I think one of the greatest thing about DDD is isolating logic into domain/model layer. "Simple POJOs which are easily testable". So feel free to add any logic into your domain/model as far as you can unit-test them with easy.
I hope my answer was some kind of useful and please let me know if you find something more convenient for distinguishing these two guys! :^)
[1] If ValueObject
is in many-side of a relation, then we need to check if update is necessary for all (one-side)references or not. if not then we leave current record as it is and we insert new record(for those references that update is needed).