Saving / Validating Data in Manager Model

Posts   
 
    
jyoung
User
Posts: 33
Joined: 27-Feb-2006
# Posted on: 13-Sep-2006 22:06:56   

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

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 14-Sep-2006 08:22:43   

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.

Please refer to the LLBLGen Pro "Using the generated code -> Validation per field or per entity" check the Entity validation techniques.

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?

It depends, like if I'm having the manager classes in a webservice I would go for passing parameters rather than entities, other wise passing entities might be fine.

jyoung
User
Posts: 33
Joined: 27-Feb-2006
# Posted on: 14-Sep-2006 16:28:36   

Ok, after reading the docs a couple times I am under the impression that IEntityValidator needs to be implemented and then using an included template to inject the validator into the Entity on initialization.

This way, it is safe to pass entities through the tiers and not worry about the object graph except in the particular validator.

Thanks

exp2000
User
Posts: 68
Joined: 13-Apr-2006
# Posted on: 17-Sep-2006 03:39:50   

The way I look at it is that I have Manager for Entities (which are generated with templates) and then I have custom Managers for business processes.