so far I put functions into objects, if those functions work exclusively on the object’s variables/state. Basically I’m following a non-anemic/rich domain model approach. However, it becomes more difficult, if the behaviour is only partially based on the object’s state. In those cases I see two options
Non-anemic approach: pass the additional information as argument to the object
obj.doSomeCalculationsAndSetAValue(someAdditionalInformation)
Anemic approach: Create a “service object” that lives on top of the domain object. The service object has all the information it needs, namely the domain object itself and the “additional information”.
class ServiceObject{ AdditionalInformationWrapper additionalInfos; //uses additionalInfos and gets information from domain object to do calculations; //use obj-setter to set the calculated value public void doSomeCalculationsAndSetAValue(DomainObject obj); }
When should I keep the logic inside the domain object and when inside the service object? Is there another alternative?