Can command handlers create new commands?

1,309 views
Skip to first unread message

Henrik Møller Rasmussen

unread,
Apr 23, 2013, 4:00:44 PM4/23/13
to ddd...@googlegroups.com
Hi

I'm using command and a command queue in my application. I have a screen in which  a user can input information to create multiple related aggregates (Company and User).

I have a CreateUserAndCompanyCommand with all the form information. How would you handle this situation if you were to follow the rules of only modifying one aggregate per transaction?

I guess a SAGA could do the job. But would you find it an acceptable solution to fx create the company in the first command handler and then based on the success of that command create a new command (CreateAndAssignUserToCompanyCommand) - and so on? In other words - are there any drawbacks in handling one command can create new commands?  

Thanks for your input.

Best regards

Henrik

João Bragança

unread,
Apr 23, 2013, 4:07:36 PM4/23/13
to ddd...@googlegroups.com
Generally speaking, a command should do one operation on ONE aggregate. I would seriously question if you really need to do this. Would the world end if one succeeded and the other one failed?

Technically, you can always leverage polymorphism here. Make your commands interfaces and then have your Screen's DTO implement both interfaces.



Henrik

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Henrik Møller Rasmussen

unread,
Apr 23, 2013, 4:12:06 PM4/23/13
to ddd...@googlegroups.com, joao...@braganca.name
Hi Joao

Thanks for the fast response.

Would you suggest that the client converts the form into multiple commands (fx. CreateCompanyCommand, CreateUserCommand, AssignUserToCompanyCommand)?

Best regards

Henrik

Marcel Jamin

unread,
Apr 24, 2013, 12:42:27 AM4/24/13
to ddd...@googlegroups.com
I have a similar situation in that I have a command handler that essentially needs to send one or two commands. What I've done is to keep the command handler very lightweight, but take a dependency on a service component. In the command handler I just call the appropriate method on the service. The service itself has a few dependencies on other components necessary to determine which commands to send.

Disclaimer: I'm pretty new to DDD/CQRS and just starting to figure it all out for myself ;)



Henrik

--

Dan Barua

unread,
Apr 29, 2013, 6:34:10 PM4/29/13
to ddd...@googlegroups.com
I have a similar similar scenario where I need to create a new Account and then either register new users or grant existing users access to the new account - I inject the message bus into the command handler and dispatch secondary commands from there. It's easy to test because you can capture and assert on any secondary commands that are issued in response to the primary command.

Greg Young

unread,
Apr 29, 2013, 7:33:31 PM4/29/13
to ddd...@googlegroups.com
What happens if the power goes out or you have an unexpected failure on one?
--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Le doute n'est pas une condition agréable, mais la certitude est absurde.

Dan Barua

unread,
Apr 30, 2013, 7:44:10 AM4/30/13
to ddd...@googlegroups.com
For this particular application, the user will contact the admins and say "Hey I can't see Dealer X in my search results, can you give me access?" or "it says my account doesn't exist when I try to log in" and the admin will rectify - it's good enough for now. I wouldn't recommend this approach if the secondary commands were important.

On Tuesday, 30 April 2013 00:33:31 UTC+1, Greg Young wrote:
What happens if the power goes out or you have an unexpected failure on one?

On Tuesday, April 30, 2013, Dan Barua wrote:
I have a similar similar scenario where I need to create a new Account and then either register new users or grant existing users access to the new account - I inject the message bus into the command handler and dispatch secondary commands from there. It's easy to test because you can capture and assert on any secondary commands that are issued in response to the primary command.


On Tuesday, 23 April 2013 21:00:44 UTC+1, Henrik Møller Rasmussen wrote:
Hi

I'm using command and a command queue in my application. I have a screen in which  a user can input information to create multiple related aggregates (Company and User).

I have a CreateUserAndCompanyCommand with all the form information. How would you handle this situation if you were to follow the rules of only modifying one aggregate per transaction?

I guess a SAGA could do the job. But would you find it an acceptable solution to fx create the company in the first command handler and then based on the success of that command create a new command (CreateAndAssignUserToCompanyCommand) - and so on? In other words - are there any drawbacks in handling one command can create new commands?  

Thanks for your input.

Best regards

Henrik

--
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages