I’m trying to apply CQRS in a pet project: in my example there is Food and Course.
A Course is composed by many item of different Food.
In my design I have those commands:
AddFoodToCourse ChangeFoodQuantityInCourse etc…
If I create a CourseCommandHandler I will get a ICourseRepository as dependency in order to modify courses but keeping commands simple I’ll have CourseId and FoodId properties instead of Food.
In the handler I suppose to manage only Ids and get all the relevant data from a repository or not?
If I have understood correctly my app must follow this workflow:
Use ReadModel => Create a command and send to bus => CommandHandler consume command, operate with domain object and save objects with repositories => Domain objects raise events and publish into bus => EventHandler consume event and modify ReadModel object
Correct. The command handler is the place to inject repositories and other dependencies required to handle commands. As you stated, commands should reference entities using IDs not the entity itself. This is because the commands are meant to be serialized and passed across application boundaries. Your workflow is also correct. Take a look here for a more in-depth treatment on the subject.