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:
What's best practice? Thank you in advance.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With