-
Notifications
You must be signed in to change notification settings - Fork 39
Events
An "event" can be many things, but when we're talking about event sourcing, an "event" is an object that describes something that has happened in the past.
In order to avoid confusion, you would usually name them accordingly, i.e. you would pick names that describe something in the past tense, like e.g. "meatballs were made", "someone drank a beer", etc.
With Cirqus, events must be derived from the generic DomainEvent
class, which brings with it one single property: Meta
. The Meta
property is a dictionary of stuff that provides a context for the event, which is needed for the domain event to make sense.
An example could be this:
public class SomethingBadHappened : DomainEvent<SomeRoot>
{
public double BadnessFactor { get; set; }
}
which is an event that describes that "something bad happened" including some esoteric information on how bad it was.
A domain event should, as the name implies, make sense in your domain. It is usually best to use some nice domain (ubiquitous) language to name it, and thus stay away from generic meta-jargon like "entity property set", "collection item added" etc.
Therefore, it is assumed in the above example that "something bad happened" is an event that makes sense in that particular domain (which could be e.g. about surveying stuff).
More about the domain/ubiquitous language
An example of some useful data in the Meta
collection is e.g. agg_id
(the ID of the aggregate root that emitted this event), time_utc
(the UTC time of when the event was emitted), and some sequence numbers that are needed to position the event in both some particular aggregate root's timeline as well as the global timeline.
It can also be used for your own meta-like concerns, like e.g. equipping all events with
- the username of the user who caused the event to be emitted, if you're in the happy situation that you have information about an authenticated user
- the ID of the tenant to which the event belongs, if your system is handling multiple tenants with one single instance of the system
- the time zone of the user who caused the event to be emitted, so that you can grab the UTC time already on the event and figure out what time it was in the user's time zone
and whatever you think could be useful at a later point in time.
An event is nothing more than a class with a few properties and the Meta collection. It doesn't make sense to have events with methods. The name of an event should say enough about the nature of the event and the Meta collection provides context. Everything else in the event should be data specific to that event. It's recommended to set all the properties in the constructor and the constructor only.
public class MyEvent: DomainEvent<MyRoot>
{
public int MyProperty { get; }
public string AnotherProperty { get; }
public MyEvent(int someData, string moreData)
{
MyProperty = someData;
AnotherProperty = moreData;
}
}
Events will be processed by Aggregate roots and View managers.