Search code examples
entitydomain-driven-designcqrs

DDD and CQRS - Define an entity for Scheduling use case


I have a use case of scheduling a person to some work for some time range. Eg. A service for assigning a person A to work for time range X to Z in location C.

The only constraint it has, is one person cannot work on 2 things at same time. Eg. if person A is assigned to work in time 2019-07-21 to 2010-07-25, then person A cannot be assigned to any other work in that time. Eg. Person A for time range 2019-07-23 to 2019-07-27 should not be possible.

I am trying to make a service for it using domain driven design which would assign a person to some work. The entity I thought would be something like:

class Assignment {
    PersonId,
    startTime,
    endTime,
    location
}

Now, I wanted to make sure that if I found a entry in my database for Person A in some time range, then the call to create an entry for Person A in time range that is overlapping with the existing ones should fail.

Since, I am using CQRS model with DDD, so I don't want to make a query to my database asking for all the assignments for that person. This may not always be recent data because of eventual consistency in CQRS model. I know in my primary key, PersonId would be there but I am not sure how can I use start and end time in it.

Any suggestions what I can do to achieve my goal for this? Is doing DDD and CQRS not a good idea in this? Or there is a better way to model this entity so that I can achieve my goal.


Solution

  • Since, I am using CQRS model with DDD, so I don't want to make a query to my database asking for all the assignments for that person.

    CQRS is just the separation between the read and write (commands and queries). When executing commands, you can query the write model.

    If you have the assignments as a value object of the Person aggregate, checking if there is more than 2 assignments at the same time is easy. And if the assignments should be a separate aggregate then the Person aggregate should keep reference of the assignments.