I have recently started my first ever effort in developing a ticketing web application using domain driven design principles combined with event sourcing and CQRS.
Since it is my first attempt away from the traditional CRUD approach, and into the DDD world, I am sure I have many things designed wrongly as DDD needs a lot of effort to come up with the right separation of domains, bounded contexts etc.
In my design, I have command handlers that accept a command, initiate a Job (a unit of work) they load the aggregates that are needed from the aggregate repository (which loads aggregates from event store by replaying events), and they manipulate the aggregates through each aggregate's exposed actions and then close the Job.
The aggregates expose actions which actually issue events. For instance, company.Create(firmName, address, taxid, ...) issues a CompanyCreated event, and applies it unto itself. When the Job is about to complete, all events from all aggregates loaded within the context of that Job are gathered and persisted by the event store.
Now, I came in a situation, which I am sure is very common, where I have relations between aggregates. For instance a Customer has Contacts, or a SupportAgent is member of a Department. Those are aggregates in my design.
Lets take the Department example. A Department's state consists of title, a description, some other properties, and a list of SupportAgent ids of those agents that are members of that department. A SupportAgent's state consists of a name, surname, phone number, email, ... and a list of Department ids of those departments this agent is member of.
Now when a Command of type AddAgentToDepartment(agentId, departmentId) is handled, two events are issued. A DepartmentAdded is issued for the corresponding agent that will add the department id into the agent's state, and a SupportAgentAdded is issued for the corresponding department that will add the agent id into the department's state.
My first question is: Is it right to keep the ids of related aggregates into the state of an aggregate? By 'right' I mean is it a best practice? Or is there another way (eg keeping relationships in a kind of 'DepartmentMemberManager' entity/aggregate or something. Actually this entity or whatever is kind of a singleton here. Is there such thing in DDD world)?
My other thought is about event replay. In the previous example, two events are issued, but in order to update the views, only one of them needs to be handled because both events describe the exact same transition in the system's state (an agent and a department are linked). I choose to handle only SupportAgentAdded event to update the views. My event handler executes an SQL script to update the corresponding database Tables to reflect the current state of the system.
What happens in case we need to replay some events to bring only a certain aggregate's view in a consistent state? Specifically, when I want to replay events for a support agent, only DepartmentAdded events will be replayed, and those events are not handled by anyone, so the views won't be updated. Is it right to replay partially some events or all events in the event store should be replayed in order to bring the whole system into a consistent state?
If you are a DDD and ES expert or at least you have experience, I would like to get some hints on what you may see I am doing, or thinking, wrong and what direction should I look at.