Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CQRS - can EventListener invoke Command?

I want to use elements of CQRS pattern in my project. I wonder if i do it right with Command and Events. The thing that I'm not sure is if event can invoke command. To better show what i want to do I will use diagram and example.

This is an example:

User invoke TripCreateCommand. TripCreateCommandHandler do his job and after success publish TripCreatedEvent.

Now we have two listener to TripCreatedEvent (the order of listener execution does not matter)

First listener (can be execute after the second listener):

for each user in trip.author.friends invoke two Command (the order of commands is important)

  1. PublishTripOnUserWallCommand
  2. SendNewTripEmailNotificationCommand
  3. SendNewTripPlatformNotification

Second listener (can be execute before the first listener):

  1. PublishTripOnUserSocials

And this is sample diagram:

enter image description here

Is this a good way ? Can EventListener invoke Command, or maybe I should do it in some other way ?

like image 408
geek Avatar asked Jan 20 '14 19:01

geek


People also ask

Does CQRS have to include Event Sourcing?

By the way, as long as you treat reads (queries) and writes (commands) using separate classes and models, it's still CQRS. It's often a misunderstanding that CQRS has to include event sourcing, eventual consistency, DDD... IMHO, it just happens to work very well with these patterns.

What is CQRS and how does it work?

Starting with CQRS, CQRS is simply the creation of two objects where there was previously only one. The separation occurs based upon whether the methods are a command or a query (the same definition that is used by Meyer in Command and Query Separation, a command is any method that mutates state and a query is any method that returns a value).

How to do validation in a CQRS application?

Working from the domain out to the client, here are 4 ways you could do validation in a CQRS application. The right way for your application maybe 1 of the four or more likely a combination. 1) Throw an exception in the domain. 2) Publish an event detailing validation problems 3) Throw an HTTP error code

What are the challenges of implementing the CQRS pattern?

Some challenges of implementing this pattern include: Complexity. The basic idea of CQRS is simple. But it can lead to a more complex application design, especially if they include the Event Sourcing pattern. Messaging. Although CQRS does not require messaging, it's common to use messaging to process commands and publish update events.


1 Answers

Your question is about Mesage Driven Architecture which works together with but otherwise unrelated to CQRS.

Anyway, your diagram is almost correct. The event subscriber/handler (I prefer this terminology) can send new Commands via the service bus, but it's not a rule that you should always do this. I implement quite a lot of functionality directly in the event handler, although probalby would be more clean and reliable to send a new command. It really depends on what I want to do.

Note that the message handlers (commands or events) should not know about other handlers. They should know about the bus and the bus takes care of handling. This means that in your app, the event handlers would take the bus as dependency, create the command and send it via the bus. The event handler itself doesn't know what command handler generated the event and can 'reply' to it.

Usually the commands would be handled independently and you can't guarantee the order (unless they're handled synchronously) so maybe you want the second command to be issued as a result of the first command's handling. Indeed, it can be the case for a Saga.

AFAIK you are talking only about doing things synchronously, so your approach works in this case but it's probably not scalable. Moving to async handling will break this execution flow. However your application can be fine with it, not everyhting needs to be twitter.

A message driven architecture is not that straightforward and for some cases (like you want an immediate response from the backend) it's quite complicated to implement, at least more complicated than with the 'standard' approach. So maybe for those particular cases you might want to do it the 'old' way.

If you're worried about decoupling and testing, you can still design the services as they were message handlers but use them directly, instead of a service bus.

like image 70
MikeSW Avatar answered Sep 18 '22 15:09

MikeSW