thenativeweb / node-cqrs-domain

Node-cqrs-domain is a node.js module based on nodeEventStore that. It can be very useful as domain component if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.
http://cqrs.js.org/pages/domain.html
MIT License
269 stars 57 forks source link

Async command handler #95

Closed emmkong closed 7 years ago

emmkong commented 7 years ago

I wonder will it possible that a command handler function could be async?

for example:

module.exports = require("cqrs-domain").defineCommand(
  {
    name: "createItem",
    existing: false
  },
  (payload, aggregate, end) => {
    setTimeout(() => {
      aggregate.apply("itemCreated", { avc, result });
      end();
    });
  }
);
adrai commented 7 years ago

No, but pre-contitions or pre-load-conditions are. And worst case you can fefine your own commandHandler: https://github.com/adrai/node-cqrs-domain/blob/master/README.md#command-handler-be-careful

emmkong commented 7 years ago

Hi Adrai, is this by design? please advise the reason behind this? else I could submit a pull request to change this.

adrai commented 7 years ago

In theory handling a command should never by async... the command/aggregate should have all information to ensure the business rules. That's why I do not recommend to do it async.

emmkong commented 7 years ago

Ok, I got what you mean. I raised this issue as we has a some business logic code that written in a promise pattern that hard to integrate. Now we had rewrite it to fit this design. Thanks for your comment.

blissi commented 6 years ago

@adrai Sorry, I don't get your post that a command should never be async...what if I have to download a file and then send an event after the download has finished? I'm quite new to CQRS...so do I have wrong thinking? Thanks

adrai commented 6 years ago

I don't think downloading a file is a business behaviour... so I wouldn't implement it in the domain. You don't have to solve everything the CQRS way ;-) I would download the file in a microservice or saga and then generate a command containing the business relevant information.

blissi commented 6 years ago

Thanks for the clarification!