Assuming that store is an aggregate and it has foods and options, food can have many options and option can be appeared many foods.The business rule are: “Food and option couldn’t have a duplicated name in the same store” and “food couldn’t have duplicated option”.
Here is sample implementation:
public class Store : AggregateRoot<StoreId>{
private readonly List<Food> _foods = new();
private readonly List<FoodOption> _foodOptions = new();
}
My first approach is making the _foods contain an id list of FoodOption, that will make Food become an aggregate itself and I need to use domain event or domain service to enforce the invariant above.
The second approach is changing FoodOption to a value object and making it being stored in Food, in another case, which is basketItem, basketItem need to store the whole FoodOption VO instead of the id with the first approach. FoodOption could be an entity but it’ll encounter the same problem as the first approach.
How could I model my aggregate with those business rule? Or my stated business rule isn’t actually that important. Thanks