Replies: 1 comment 1 reply
-
Hello @elsurudo! I will try to answer your question:
Command bus is conceptually similar to Rails routing, but on a different layer. Routing maps HTTP request to a single controller action that can handle it, but it can map multiple HTTP requests to a single controller action. Same goes for the command bus — the handler can be run by various commands or by single one. Controller action must succeed or fail — command will also succeed or fail. You mentioned decoupling argument which is pretty valid, since the controllers no longer need to know what service are they calling. I find this quite good and easy to test. I can now assert what commands given request produced instead of getting into mocking hell or checking side effects in an ActiveRecord model. Another reason: chain of responsibility pattern. You can make each "chain link" responsible for a single thing. You can add additional handlers without changing current implementation or reorder them freely. Speaking of infrastracture, you can pass the dependencies via command bus and not pollute your domain code with those. Please, have a look at this seasoned blogpost as it explains one of the way of doing this. Last thing which comes to my mind, but not least are transactions. You might want to rollback everything when command fails, or you can react to failure in a different way — you decide here. Hope this answer gives you a bit of light! |
Beta Was this translation helpful? Give feedback.
-
Using events allows having multiple subscribers, thus allowing a kind of fan-out. This itself is enough of a reason to avoid the coupling that would otherwise happen without events.
But what is the point of using the command bus? As far as I can see all it really does it add another layer of indirection – why not just call the "command" directly?
Admittedly I am an events noob, so perhaps someone can point out some of the benefits? I get the theoretical "decoupling" argument, but I just don't find it compelling in this case.
Beta Was this translation helpful? Give feedback.
All reactions