I have been studying the manager templates in the template repository and have a question on how people handle saving and validation of data in a manager model.
For example, say I am creating Recipe database that consists of RecipeEntities, IngredientEntities and so on. I like to cook, so it is something easy for me to think of. I could have a ReceipeManager with a save method on it that accepts a RecipeEntity, such as RecipeManager.Save(RecipeEntity recipe).
My confusion lies in how to apply rules to recipes and ingredients.
Say I have a crazy rule that states a Recipe can only have 3 Ingredients.
I could create an AddIngredient method on the RecipeManager that accepts an IngredientEntity and let that method take care of it. That makes the most sense to me, but nothing would prevent the consumer from creating a new RecipeEntity, a new IngredientEntity, adding the Ingredient to the Recipe via Recipe.Ingredients.Add(Ingredient) and then using RecipeManager.Save(Recipe).
To handle this, I could in the Save method, check the count in the Ingredients collection and apply the rules there. This is fine for one or two relations, but what about when you have numerous relations?
What if later on new rules were defined for IngredientEntities. Maybe they can only be green or something even more absurd.
Maybe I am horribly over complicating things, but it seems like the Save method would have to know about the complete object graph of a RecipeEntity.
The only solution I can think of is to not pass Entities through the layers.
So that the methods would look like:
RecipeManager.Save(name, description, image etc.) and
RecipeManager.AddIngredient(recipeId, name, measure, quantity etc.).
My question is:
How do you handle the saving of data?
Do you pass entities, if so, how do you deal with the object graph, or do you just pass raw data?
Many thanks.
-Joe