Let’s say I have a Mail
, this mail have many properties given by the constructor (for example, 10-15 parameters).
This mail can’t be edited (immutable), the user has specifically requested to send this content. Mail has no methods but multiple constructors (for managing different scenarios), and it has few references on other entity identifiers (for example a reference on the identifier of the sender and the receiver).
Is it okay to make Mail
a value object or should it be an entity ?
Some of the issues raised :
Mail
have some references on other entity identifiers. Some developpers says a value object can have a reference on an identifier while others says only entities can have references on entity identifiers. Both sides seems to have good points.
Mail
doesn’t need an identifier. it will not be modified. However, the item will have to be stored in a database to be logged.
A Mail
is a Mail
. If we have items with same sent date, same mail subject and mail body, with same sender and receiver, in principle it is the same mail.
If Mail
is considered value object, we still have the problem that it have several constructors with many parameters (smell code), we will probably need to use builder pattern to construct this object value.
If Mail
is considered entity, we will have an anemic domain model. We could more or less add methods to fill the properties and at the same time remove the big constructors, but Mail will no longer be an immutable element.
I’ve encountered this kind of scenario several times without knowing what to do. There are obviously some subtleties specific to each case, but similar questions/issues come up.
What would be the best approach ?
Vianney Doleans is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.