Chain of commands and aggregate reference

Hi,

I have two aggregates AR1 and AR2.
Suppose AR1 is Folder aggregate and AR2 is File aggregate.
There may be certain operations which may impact both aggregates e.g Delete folder command needs to delete folder as well as the files contained in it. For this AR1 will need to hold references to AR2.

What is the best practice to hold this information? Aggregate AR1 itself or Command Handler should query, query model to find out all the child file aggregates of folder aggregate and invoke Delete File Command.

In some of threads I had read its not a good practice to send a command from a command handler and Saga or Event Handlers should be used instead. But in this scenario we also have to maintain transaction i.e if Delete File fails then Delete Folder should fail too.

Can anyone share some insights on this please?

Thanks & Regards,
Prachi Mujumdar

Hi,

if File and Folder are different aggregates, and deleting a folder should trigger the deletion of all files, there are a few approaches.

The cleanest would be to have a saga to listen to FolderDeletedEvent and send out DeleteFileCommand for each file known to be in that folder.
Alternatively, you could perform a query for all files in a folder and send delete commands for all files and folders.
Third option is to create a batch command that does the query for you, instead of the client. Based on that batch command, the handler will send out new commands.

This might al be a little overkill for files and folders. Probably the real case is a bit more complicated than that. Do realize that splitting the aggregates may not have been the best choice. In this case, it might be better to put both Files and Folder in a ‘Folder’ aggregate.

Cheers,

Allard