Adding properties to our tevents
You may have noticed that the tevents on the last page were missing a little something, namely properties.
Now the natural expectation would be for a slightly more realistic version of something like this:
interface IUserRegistered : IUserTevent, ITaggregateCreatedTevent;
interface IUserChangedEmail : IUserTevent;
to be something like this:
interface IUserRegistered : IUserTevent, ITaggregateCreatedTevent
{
Email Email { get; }
}
interface IUserChangedEmail : IUserTevent
{
Email Email { get; }
}
However, doing that does not work well at all.
The dilemma of Fine-grained vs Coarse-grained events
The reason the above code is a terrible idea, is that some tevent listeners care only about updated data, and others care about semantics. If we do things like above, the code that cares only about data will not only have to know about and manually listen to every single concrete tevent in the system that updates Email, we must keep track of every such listener for every single taggregate property in our whole system, and update the affected ones every time a new tevent that updates any taggregate property is created 😨
Classically the above problem has led to projects being forced to choose between using fine-grained property-updated events and coarse-grained semantically meaningful domain events. The problem is that both choices are maintainability disasters. If you go with property-updated style events you lose virtually all ability to understand the semantics of what happened, as a single user interaction is exploded into a number of individual data atoms 😬 If you go with coarse-grained events, we run head first into the issues described in the previous paragraph. You cannot win as long as you accept the choice.
Unifying Fine-grained and Coarse-grained events
With tevents you don't need to choose. If you do this:
interface IUserEmailPropertyUpdated : IUserTevent
{
Email Email { get; }
}
interface IUserRegistered : IUserEmailPropertyUpdated, ITaggregateCreatedTevent;
interface IUserChangedEmail : IUserEmailPropertyUpdated;
The whole problem disappears like it never existed ☺️
When we design tevents like that, a listener like this ...
registrar
.ForTevent<IUserEmailPropertyUpdated>(emailUpdated => WriteLine($"User: {emailUpdated.TaggregateId} Email: {emailUpdated.Email}"));
... will never need to change throughout the whole lifetime of the system. It will be called whenever a user's email is updated. Period.
Likewise, listeners that care about when users are registered will listen to IUserRegistered and receive the same benefits. No matter how many new ways of registering users we add, no matter how many new subtypes of IUserRegistered are added. That code will always be called and does not need to change.
A tevent declares all it means
A listener that cares only about data cares about one part of what a tevent means. That registering a user sets the user's Email is part of what IUserRegistered means, so IUserRegistered declares it, by implementing IUserEmailPropertyUpdated. A tevent that leaves part of its meaning undeclared is what made the listeners above so hard to keep right.