23

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"...

Ludovic C
  • 613

3 Answers3

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 happened" 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.

Doc Brown
  • 218,378
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
synthomat
  • 256
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.

Euphoric
  • 38,149