Here’s a rather dumb but perfectly valid question coming from a newbie.
Context: My “domain object” has a name (and some other attributes, but they are not important). This name is generated according to some patterns which are defined in the database.
Now… who should determine the name of the domain object? Should the domain object determine it’s own name (please note this “name” can be based on it’s own or some other object’s attributes) or should a service determine the name according to the pattern and then call something like
domainObject.setName(theActualName)
I tend to follow the “service” approach, but on the other hand it seems this task should be an object-specific behaviour.
Thanks a lot for your replies.
I think you should look at Tell Don’t Ask Principle
If object contains all the data it needs to calculate its name I would put this method on that class. If this data is based on other object’s attributes and parent class also contains this child object that contains data, I would again put on the parent class.
Just Keep it simple. I wouldn’t introduce a service class if I dont really need. In DDD terms you should put your domain related logic in domain classes .