Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling one command from other in case of using CQRS

Tags:

php

cqrs

I'm developing application using CQRS. And I have on case where I'm a little confused.

I have one command CreateUser. Now I have to create command ImportUsers. I guess purposes of those commands are clear. So there are two options:

  1. Calling CreateUser command from ImportUsers command.
  2. Make kinda of copy/paste to do all thing from scratch in ImportUsers command.

What's best practice? Thank you in advance.

like image 899
akor Avatar asked Oct 15 '25 15:10

akor


1 Answers

You are putting your logic in the wrong place, and that’s what’s causing the problems. Commands are meant to be orchestrators of domain logic. In other words, they call appropriate method(s) in the domain to get their job done, they don’t do it themselves.

The logic to create a single user belongs in a factory that each of these methods calls. The factory should have a CreateUser method that accepts all data as parameters, or accepts a DTO if there are more than about 5 or so params, which I assume there are. With a factory to create users, the only difference in the two commands is that the ImportUsers will call the factory CreateUser method in a loop.

Check this posting about the idea behind commands. If you are using DDD, read Udi Dahan's post about how all object creations should be in a method on some aggregate somewhere.

like image 122
Brad Irby Avatar answered Oct 18 '25 07:10

Brad Irby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!