Domain model modeling
How would you create a domain model for this simple example? A recipe can contain many ingredients, and an ingredient can be used in many recipes. The amount of each ingredient used in each recipe is also saved. I created the following three database tables to store this data and relationships.
Now I am trying to create a domain model to represent this. I have a basic example with two classes. Then I ran into the problem of this model when I thought about creating a new ingredient. There must be a class with no quantity property. How should this be modeled?
Database tables
alt text http://img190.imageshack.us/img190/340/databasex.png
Domain Model
source to share
If you're trying to design with a domain name, don't start with tables. First, develop a conceptual model that reflects your base domain. I agree with ndp: RecipeIngredient is a bit of an awkward name / concept, from a DDD perspective.
I think the model needs the following concepts: recipe, ingredient, dimension and recipe.
A recipe is a collection of ingredients. Each Ingredient belonging to a Recipe requires an associated Measure as a specification for preparation. You also need to model the RecipePreparation to relate the actual amount of each ingredient used during a specific recipe preparation.
A Measurement consists of unit and quantity (e.g. 2 cups, 0.5 oz, 250 g, 2 tbsp).
I see two different things here that can be mixed during analysis and should be separated: Recipe / Ingredient / Measure as a BOM to prepare something (one copy for each recipe) and RecipePreparation / Ingredient / Measure as a specific preparation one recipe, made by a specific person at a specific point, and can be used in many different ways (doubles all ingredients, because the recipe spec is for two plates and you have four guests ... something like that).
You can go deeper and start modeling things like some ingredients that have a set of exchangeable ingredients (e.g. if you don't have goat cheese, mozzarella), a cookbook that collects recipes from the same category, cooking times for a recipe, etc. ...
source to share
In your domain model, create a RecipeIngredient class containing a reference to a specific ingredient and quantity.
Then, modify the Recipe.Ingredients list to contain RecipeIngredient objects. Finally, remove the quantity from the Ingredient class.
Just a tip: most purist domain moderators will say that you should create your domain model first and not relate to the database much later.
source to share
Since you have data in your join table (count), the answer is that you need a class to represent it. (There are other alternatives, but not worth considering.)
As your model grows, you will undoubtedly need to add more data. For example, how do you order the ingredients in a recipe?
RecipeIngredient is a bit of an awkward name (and concept) in terms of domain based design. You can come up with different names that feel better. But overall, this is a necessary implementation detail. Sorry, I don't have an Evan DDD book handy for providing reference.
source to share
I think you guys are all lost. The original poster had the right momentum but went wrong. In fact, the mapping table he shows using the old Riehl heuristic (combining the relationship names l and r) seems to address the fact that this is a many-to-many mapping. But what really happens is that you need a role class (Coad Domain Modeling approach used a lot of them). Here's the thing though: what ingredient is already there! The abstraction that is missing here opens up a can of worms: this is what is being added. One could argue that it would be a base class, for example. Food (since we literally, by definition, cannot add inedible items to a recipe), but then you have the burden of keeping track of all the foods, or you can just name them.
So the correct model, I think, is a recipe that contains ingredients that have a certain amount of a particular food.
source to share