21

I am looking for a way to design a ddd application with convention over configuration.

Say an aggregate "Client" has a command defined "FillProfile". It will logically raise an event "ProfileFilled".

Are there cases when a command will raise more than an event, or where a command will raise different events based on some logic? Or is this always a 1 - 1 relationship (1 command will always raise none, or a single event of a given type).

I am asking this because if this is a fact, that a command will always raise the same event, I can build my convention system on that fact. I know that "RaiseEvent" will result in "EventRaised"...

4
  • 1
    if the command causes 2 things to happen you might expect each thing to raise an event. ProfileGenerated, ProfileSaved, Also any event might cause an error which again may raise an event – Ewan Dec 20 '15 at 23:34
  • Also trivialy may systems implement BeforeCommand, AfterCommand etc events – Ewan Dec 20 '15 at 23:36
  • Or the command might loop ie FillProfiles() profileFilled,profileFilled.... – Ewan Dec 20 '15 at 23:38
  • @Ewan, just want to correct you. A domain-event cannot generate an error. Only commands can. Domain events mean that something happened, as commands mean that there is an intention of making something happen, which can be refused or accepted. In other words, an error must be raised before a domain event gets dispatched (unless that domain event is related to error logging of course). – Ludovic C Dec 21 '15 at 7:02
19

Since you tagged your question with "CQRS", I guess you mean events in a "CQRS & Event Sourcing" context, like it is described here. In this tutorial, the difference between events and commands is well explained:

  • events capture the elementary "things that can happen" in your system, from the system's point of view.

  • commands are defined by what the user considers as an operation, from his point of view

And though this often leads to a couple of commands and events with a 1:1 correspondence, these different points of view can lead to commands which fire more than one event, or different events depending on the command parameters. I can even imagine cases where a command does not raise an event at all, but that would be a very exceptional case, not a very typical one.

For example, the tutorial mentions events

  • TabOpened
  • DrinksOrdered
  • FoodOrdered

and commands

  • OpenTab
  • PlaceOrder

Here, the command "OpenTab" will lead to an event "TabOpened", but the command PlaceOrder will lead to the events "DrinksOrdered", "FoodOrdered", or both.

In fact, if you are designing a new system "from scratch", you can try to design it with a 1:1 correspondence between commands and events and look how well that scales when the system becomes bigger. You can even try a hybrid approach: a list of events and commands with a 1:1 correspondence, together with some additional, combined commands. Just try how far that leads you for the particular system you are designing.

0
11

Usually one command will lead to one event. But in some cases it can also be more than one, it depends on your implementation.

Either your command calls other commands and each of them fire own events. Or your command does different tasks on it's own and issues multiple events. For example:

RegisterUserCommand

  • User.create(email, password) → UserCreatedEvent
  • User.updateProfile(firstName, lastName, location) → UserProfileUpdatedEvent
  • User.joinDefaultGroup() → UserJoinedGroupEvent
3
  • What happens if you later decide to have UserWasAddedToCrm? Rewrite your entire stream? – mcintyre321 Aug 1 '18 at 15:07
  • @mcintyre321 search choreographed vs orchestrated events – Benten Jan 15 '20 at 22:25
  • TBH this question is a bit confused - it's talking about DDD/CQRS, when in fact it sounds like it needs to decide if it's an Event Sourcing Question or an Event Driven Architecture question. – mcintyre321 Feb 12 '20 at 11:54
11

One command can raise multiple events. It is simply logical conclusion of one fact : Composite command exists.

Lets say you have two commands, each raising an event. Then, you create a composite command of those two. From the view of one using the composite command, it seems as if the command raised two events.

So there is nothing stopping you from having single command raising multiple (or even no) events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.