2

I built an app where the app owner can build custom forms for each of his clients. The forms have default fields like "name" and "message" as well as custom fields for each client requirement, like "whatsapp", which must trigger a switch in the app to send the message via Whatsapp instead of a Mailer.

So custom form fields are mapped to a use case.

I have used the Command Handler pattern before, so I was thinking that this might be a good use case for a Command Bus implementation. But I would hate to dive into this and find out that this is not what it's meant to be.

My approach would be to turn the custom form data into a command and pass it on to the Command Bus, possibly using the form field name for the command name itself. Any suggestions?

Christophe
  • 74,672
  • 10
  • 115
  • 187
Hans
  • 406
  • 1
  • 3
  • 13
  • 1
    If I were doing it, I'd find a way to reflect over the form's controls and turn the data input into a JSON object. You can then take that JSON object and do whatever you want to with it: post it to a website, put it on a command bus, etc. – Robert Harvey Sep 11 '18 at 17:37
  • What would be the benefit of turning the input data into JSON first - it is not passed to anywhere outside the app? Another thing that is confusing me right now: sending the form triggers an event from the framework - I would therefore attach a Command Bus to this single event, simply because this event is very likely to grow in complexity over time; I would think that CBs are meant to cover the commands of an entire app. – Hans Sep 11 '18 at 17:47
  • The benefit of turning it into JSON is that it's a standardized format that can be read by a wide array of pre-existing software that doesn't have to know anything about your form or the technologies that is uses. This "design pattern" is called "Separation of Concerns." Your idea that a command bus might be used suggests that your application is larger than a single form, and the JSON reflector I suggested could be used on all of your forms, not just one. – Robert Harvey Sep 11 '18 at 17:55
  • A Command Bus usually receives a command, not a JSON object, so I would have to turn this JSON object into a command first, the way I see it. While I think I get the benefits of JSON/XML data objects, I fail to see why I would profit from doing this with data that is not meant to circulate outside of the application scope. It feels like YAGNI, or creating an interface for a service or a controller, which is kind of a [controversial question in itself](https://softwareengineering.stackexchange.com/questions/150045/what-is-the-point-of-having-every-service-class-have-an-interface). – Hans Sep 11 '18 at 18:13
  • 1
    Well, you haven't told us much about the technologies you're using, the architecture you have chosen or the problems it all tries to solve, so its difficult to offer cogent advice. I don't think this is necessarily a software patterns problem per se. Normally, wouldn't command buses be used in larger projects? – Robert Harvey Sep 11 '18 at 18:14
  • Also, you're right; the JSON object isn't a Command; it is *the payload for a command.* Given that we're only talking about one form here, I'm not sure what benefits a command bus would provide, though. – Robert Harvey Sep 11 '18 at 18:17
  • It's a form builder in a WordPress backend. The forms are displayed in the frontend and trigger an event/hook upon submit. I attach a callback function to this hook which could then invoke the command bus. That was my inital thought, at least. There are several forms, each representing a client requirement with increasing complexity - like pushing form data to different APIs (WebSMS, Zendesk, etc.). So my problem is how to trigger the different commands, like `SendWhatsApp`, that represent the client requirements of the form. – Hans Sep 11 '18 at 18:25
  • Is about [PHP CommandBusses](https://medium.com/web-engineering-vox/building-a-php-command-bus-a65e6ae6a6ac), where a command is not the command pattern, but a DTO ? – Christophe Sep 11 '18 at 18:29
  • @Christophe Right, maybe it is called [command handler](https://www.packtpub.com/mapt/book/application_development/9781787284944/11/ch11lvl1sec83/command-handlers) pattern? The way I understand it, Command Busses are based on Commands and Command Handlers. – Hans Sep 11 '18 at 18:31
  • @Michael yes, that's exactly what the article describes. So you're using the correct terminology. But it creates confusion, because for most of us, the command pattern is something different (an object with the behavior). I don't know enough PHP to give you a reliable answer, but browsing quickly through a couple of articles, I understand that it's trendy for the time being, because it allows to implement some kind of abstraction with dynamic dispatching. – Christophe Sep 11 '18 at 18:38

0 Answers0