I’m working on the domain model for a recipe application and running into an issue.
The application has multiple entities capable of acting as an ingredient, two of which are: Product and Recipe (recipes can be ingredients in other recipes). Normally I would encapsulate the ingredient-related functionality into an interface that each of these entities can implement. The problem is that, while all Product instances can be ingredients, only a subset of Recipe instances can be ingredients.
interface IIngredient
{
void DoIngredientStuff();
}
class Product : IIngredient
{
void DoIngredientStuff()
{
// all Products are ingredients - do ingredient stuff at will
}
}
class Recipe : IIngredient
{
public IEnumerable<IIngredient> Ingredients { get; set; }
void DoIngredientStuff()
{
// not all Recipes are ingredients - this might be an illegal call
}
}
How can I restructure this model to support the requirement that only some Recipe instances should be capable of acting as an ingredient?
Another option if it works in your classes tree to have separate classes for these 2 types of Recipes. Note that this approach does not work well if there more properties you want to distinguish objects by.