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
  • 603
  • 5
  • 9
  • 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
  • 1
    @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 07:02

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
  • 199,015
  • 33
  • 367
  • 565
  • 1
    Great answer! But I currently have the reverse situation: a number of different commands that each trigger the same event(s) because they result in very similar effects but, from the client's perspective, with different intent. That intent was of sufficient interest to domain experts to warrant separate commands, so presumably it should also be of sufficient interest to be recorded in the arising event(s)? But that requires adding either an "intent" type field to those event(s); or else an intent-conveying event per command. Neither approach feels very satisfactory. Am I approaching it wrong? – eggyal Nov 16 '22 at 00:54
  • @eggyal: sounds reasonable. At least your event will require some parameter (which is not uncommon). Without more context, however, I fear it is quite impossible to say if that parameter should be an "intent" field or something different. If you want an answer, ask a new question on this site with some real-world context, not a contrived example or way-too-abstract description. – Doc Brown Nov 16 '22 at 06:45
  • 1
    Thanks, I've posted https://softwareengineering.stackexchange.com/q/442283 – eggyal Nov 16 '22 at 11:01
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
  • 1
  • 5
  • What happens if you later decide to have `UserWasAddedToCrm`? Rewrite your entire stream? – mcintyre321 Aug 01 '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.

Euphoric
  • 36,735
  • 6
  • 78
  • 110