Oskar Dudycz

Pragmatic about programming

Anti-patterns in event modelling - Property Sourcing

2021-08-18 oskar dudyczEvent Sourcing

cover

The first time I got down to work at Event Sourcing, I was very energized. Book knowledge almost fell out of me. However, when I sat down to programming, I was looking like the dog from meme.

This is usually the case when we realize that putting theory into practice is not as easy as it may seem.

When we learn a new pattern, we subconsciously try to translate our previous habits into it. It is a human way of getting familiar with the unknown. Looking for similarities isn’t bad by itself. It allows us to move forward. Worse, when we stick to these clichés and kill our curiosity. Each pattern can become an anti-pattern when used in a context other than it was invented. When you hold a hammer in your hand, it is not difficult to see nails in everything.

When we start modelling our system with events, we can easily fall into the trap. We are used to looking at our functionalities from the perspective of the data model. When you hold a relational database in your hands, you will see tables everywhere. Because we have read that events should be as small as possible the first idea for an event can be, e.g. FirstNameChanged. Another LastNameChanged, etc. We also see the use immediately, that is, a straight history of changes to our entity. Those events may look as:

public class FirstNameChanged
{
    public string FirstName { get; }
    public DateTime ChangedAt { get; }

    public FirstNameChanged(string firstName, DateTime changedAt)
    {
        FirstName = firstName;
        ChangedAt = changedAt;
    }
}

public class LastNameChanged
{
    public string LastName{ get; }
    public DateTime ChangedAt { get; }

    public LastNameChanged(string lastName, DateTime changedAt)
    {
        LastName = lastName;
        ChangedAt = changedAt;
    }
}

We may also come up with brilliant the idea of having both previous and new ones. Then we could directly create a history of changes to an audit trail in the UI.

public class FirstNameChanged
{
    public string PreviousFirstName { get; }
    public string NewFirstName { get; }
    public DateTime ChangedAt { get; }

    public FirstNameChanged(string previousFirstName,  string newFirstName, DateTime changedAt)
    {
        PreviousFirstName = previousFirstName;
        NewFirstName = newFirstName;
        ChangedAt = changedAt;
    }
}

public class LastNameChanged
{
    public string PreviousLastName { get; }
    public string NewLastName { get; }
    public DateTime ChangedAt { get; }

    public LastNameChanged(string previousLastName,  string newLastName, DateTime changedAt)
    {
        PreviousLastName = previousLastName;
        NewLastName = newLastName;
        ChangedAt = changedAt;
    }
}

Even by looking at those events, we can sniff the unpleasant smell. It’s easily visible that this approach will not be maintainable. When our model grows, we’ll get many tiny, copy/pasted, meaningless events.

The critical aspect of event modelling is to have them close to the business. Events should correspond directly to the result of business operations in the system. To achieve this, the event should be derived from a specific request/command processing. Based on the values ​​sent in the command, we know what data was transferred. Based on them and the business logic, we can fill in the event data.

The fact that the name has changed is not usually a factor affecting the business logic. Usually, we just accept the change, fill in the data, and that’s it. Therefore, we can pass this information in an event and use it in a projection to build a read model. However, even if we have a Jiralike form to edit a specific field, it is worth grouping such changes according to characteristics.

We can, among others, have PersonalDataUpdated event triggered by updating first name, last name, etc. These fields may have the Option type to check if they have been changed. An example implementation of such a type in C# might look like this:

public struct Option <T>
{
    public static Option <T> None => default;
    public static Option <T> Some (T value) => new Option <T> (value);

    readonly bool isSome;
    readonly T value;

    Option (T value)
    {
        this.value = value;
        isSome = this.value is {};
    }

    public bool IsSome (out T value)
    {
        value = this.value;
        return isSome;
    }
}

then the usage as follows:

public class PersonalDataUpdated
{
    public Option<string> FirstName { get; }
    public Option<string> LastName { get; }
    public DateTime ChangedAt { get; }

    public PersonalDataUpdated(string firstName,  string lastName, DateTime changedAt)
    {
        FirstName = firstName;
        LastName = newLastName;
        ChangedAt = changedAt;
    }
}

var onlyLastNameUpdated = 
    new PersonalDataUpdated(
        Option<string>.None, 
        Option<string>.Some("Smith"),
        DateTime.UtcNow
    );

With this, we do not have hundreds of minor events but business-significant events. They still contain details of what has changed. We can create the read models based on them. Suppose we want to build an audit trail with information about the previous and the current value. In that case, we can retrieve the last state of the model in the projection, compare it with changes in events and save the difference as a new line.

Publishing events like LastNameChanged is called Property Sourcing. This is an anti-pattern. The events themselves tell us nothing about the operation that performed them. They have no business value. Due to the number of event types we have to generate, it is also challenging to manage them. It’s also not convenient for other modules to consume them.

Of course, sometimes it makes sense to create field change events. For example, EmailUpdated, MaritialStatusChanged, AccountBalanceUpdated, InvoiceNumberSet. These are significant business fields and can trigger other workflows.

The basis of good event modelling is in cooperation with business. Discussion and understanding what we want to achieve is the foundation. Of course, sometimes, it is worth cutting the design discussions and getting coding. When we see them in action, it’s more accessible to find the weak spots. Still, we should not try to save the four hours of discussion time by two weeks of coding.

It’s also important to not treat our initial event model as set in stone. We should embrace that our model will change. We’ll understand our domain better. The business will also change as time goes. We should continue to drill down and make our event model closer to the real world.


If you’re dealing with such issues, I’m happy to help you through consulting, training or mentoring. Contact me and we’ll find a way to unblock you!

See also more in series about event modelling anti-patterns:

Check also more general considerations:

Cheers!

Oskar

p.s. If our events are only about the fields updates. If they are not related to the specific business operations. In that case, we should evaluate if simple CRUD wouldn’t be better for us (read more on that in “When not to use Event Sourcing?”).

👋 If you found this article helpful and want to get notification about the next one, subscribe to Architecture Weekly.

✉️ Join over 11500 subscribers, get the best resources to boost your skills, and stay updated with Software Architecture trends!

Loading...
Event-Driven by Oskar Dudycz

cover

Through my window, I see the result of good plans but poor execution. Opposite my flat, there is a partially completed construction place. Buildings were supposed to be eye-catching Mediterranean style apartments. Delivery date? Two years ago. Actual? More and more unknown.

Some time ago, I heard that using Event Sourcing makes creating Event-Driven Architecture easier. The arguments were correct, that if we’re already publishing events to trigger business workflows, then at some point, we may want to also store events to not lose information. Agreed. However, I also heard that keeping the state as events will simplify things. We’ll have a source of truth with a record of the system behaviour. This will allow, e.g. to confront the results of the operations with the recorded state. I’d agree with that, with one distinction. It’s easier as long as you already know Event Sourcing.

Many people in the DDD community claim that the essential is to properly break down the system into autonomous parts called bounded contexts. Once we have it, the rest is secondary and will sort itself out. For sure.

Many seasoned programmers speak similarly about new technologies. They claim that they can translate past experience into new technologies. That’s true that by analogy, they can catch the big picture quicker. But isn’t it a bold assumption to say that Win.Forms specialist will learn Angular quickly?

The end result may differ a lot from the initial ideas. I saw the plan of those buildings next to me. Now I can see the effects of the execution. Or actually, the lack.

I believe that we should carefully acknowledge not only the point of view of our authorities but also their seating point. If we want to find out how to form a wall, do we ask an architect or a foreman? An architect may know the theory, but the practice is what we’re looking for. On the other hand, if you want to know where to put the wall, you prefer the architect to do measurements. At least if you don’t want to have the roof falling to your head.

After I had torn a ligament in my knee, I went to two qualified orthopedists. One said I should have surgery and do a reconstruction. The second stated that there is no need for that; rehabilitation should be enough. Guess which one had a specialization in surgery and which in rehabilitation?

People usually give us advice from the point where they’re currently standing. They are entitled to a biased view. An architect who rarely does programming will tend to downplay the value of implementation and tactical patterns. Midlevel developers will focus on technicalities instead of the global system impact. The team manager or consultant will emphasize the importance of soft skills (or esoteric techniques known only to them).

The truth is that we need all of them. The excellent plan will fall on the bad execution. The best execution for the wrong case will be just a waste of time. We should carefully evaluate the advice considering what we need and what an expert can give us.

Therefore, when we’re reading an article, watching a talk, let’s also pay attention to the place where the person is standing. The perspective from there may be much different from where we are right now. That can be good, as it may push us in the right direction. But it may also be misleading, as we accidentally take biases of this person without understanding the tradeoffs. Personally, I prefer to follow not only people from pedestal but also those that are closer to my position. A bit further in the journey, but not too far. That helps me to calibrate my view as those people are more relative to my daily struggles.

Polish historical leader Józef Piłsudzki reportedly used to say: “Right is like an ass, everyone has its own”.

Cheers!

Oskar