Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Sourcing Commands vs Events

I understand the difference between commands and events but in a lot of cases you end up with redundancy and mapping between 2 classes that are essentially the same (ThingNameUpdateCommand, ThingNameUpdatedEvent). For these simple cases can you / do you use the event also as a command? Do people serialise to a store all commands as well as all events? Just seems to be a little redundant to me.

like image 268
user842807 Avatar asked Jul 13 '11 14:07

user842807


People also ask

What is the difference between event and command?

Both are messages. They convey specific information; a command about the intent to do something, or an event about the fact that happened.

What are command events?

Command events are notifications that an MQSC, or PCF command has run successfully. comprises the queue manager from where the command was issued, the ID of the user that issued the command, and how the command was issued, for example by a console command.

What is the difference between Event Sourcing and event-driven?

To be clear, Event Sourcing is about using events to represent state. In Event Driven Architecture, events are used to communicate with other service boundaries.

What is the difference between Saga and Event Sourcing?

The two are compatible patterns that address different problems, Sagas handle workflow processes where as event sourcing addresses how state is stored. Sagas provide a mechanism for handling a multi-step process and rolling back should steps fail (like a workflow).


1 Answers

All lot of this redundancy is for a reason in general and you want to avoid using the same message for two different purposes for a number of reasons:

  1. Sourced events must be versioned when they change since they are stored and re-used (deserialized) when you hydrate an aggregate root. It will make things a bit awkward if the class is also being used as a message.
  2. Coupling is increased, the same class is now being used by command handlers, the domain model and event handlers now. De-coupling the command side from the event can simplify life for you down the road.
  3. Finally clarity. Commands are issued in a language that asks something to be done (imperative generally). Events are representations of what has happened (past-tense generally). This language gets muddled if you use the same class for both.

In the end these are just data classes, it isn't like this is "hard" code. There are ways to actually avoid some of the typing for simple scenarios like code-gen. For example, I know Greg has used XML and XSD transforms to create all the classes needed for a given domain in the past.

I'd say for a lot of simple cases you may want to question if this is really domain (i.e. modeling behavior) or just data. If it is just data consider not using event sourcing here. Below is a link to a talk by Udi Dahan about breaking up your domain model so that not all of it requires event-sourcing. I'm kind of in line with this way of thinking now myself.

http://skillsmatter.com/podcast/design-architecture/talk-from-udi-dahan

like image 195
Chris Nicola Avatar answered Sep 21 '22 13:09

Chris Nicola