I’m working on a meal planning application. I have a MealSchedule which is a list of things that I want to eat on various days. That MealSchedule will eventually get turned into a MealPlan which is a grocery list of products and recipes that will be create the things I want to eat on each of those days.
Since the MealPlan is created from a MealSchedule I would like to create a constructor for a MealPlan which takes in a MealSchedule. Unfortunately, this process is fairly complicated and I feel that would lead to the anti-pattern of creating an overly complicated constructor.
On the flip side, if I use a builder to create the MealPlan then the MealPlan becomes a rather anemic object and doesn’t contain this logic that relates so closely to the MealPlan. Which is the least of two evils, or more likely, what am I missing?
Is
MealPlana full fledged entity that can be modified and whose state must be tracked, or could it be made a Value Object ? If it could, then you don’t need to worry about it being anemic and you might delegate its construction to some kind ofMealPlanBuilder.In any case, what I would do is try to refine the ubiquitous language and talk with a domain expert to see if such a thing as a Meal Plan Generator would make sense in business terms. If the answer is yes, then you have your object. I think it would also be very practical as you’d be able to talk about the Meal Plan generation process with the stakeholders in very precise, clearly expressed terms, as in
If the concept of generating Meal Plans is not such an important thing in itself, then you may consider leaving its logic in the
MealPlanconstructor.As a side note, I’d add that anemicness is not an anti-pattern you should systematically track and eradicate in each of your domain objects. Sure, it’s better when your objects do have behavior and you should look to do it every time you can, but for some objects it’s just not possible. Just because an entirely anemic domain layer is bad doesn’t mean that you need to flog yourself for your occasional anemic object.