Sunday, April 21, 2024
HomeRuby On RailsUpcasting occasions in RailsEventStore | Arkency Weblog

Upcasting occasions in RailsEventStore | Arkency Weblog

Understanding the area you might be working with usually results in the necessity to redesign a number of the fashions. Generally you’ll
want so as to add or change an idea. Different occasions, you’ll have to take away a technique or occasion produced by the mixture. This was
the case for us.

Our aim was to take away an occasion from the system. To do that, we needed to cope with the truth that this occasion was within the
combination stream.

It’s attention-grabbing how we bought there.

We began discussing the way to implement a brand new enterprise function within the aforementioned mannequin.
After tossing round a couple of concepts it felt prefer it didn’t belong within the combination itself.
We realized that it belonged within the utility layer, which is liable for dealing with completely different enterprise use instances.
It’s usually a dilemma that may come up. The place does this enterprise logic go? The mixture, the appliance layer that’s
liable for the use instances? Elsewhere?
From a code perspective, it’s all about the place to place this if assertion.

Have you ever ever skilled an analogous conundrum?

After some dialogue, we determined to implement this function within the utility layer. Nevertheless it felt hacky.
Writing a couple of check instances helped us notice that the mixture class has two strategies that principally do the identical factor.
The idea is represented in the identical manner within the learn mannequin. Nonetheless, these two strategies produce completely different occasions.

Lengthy story brief, it turned out that our combination was somewhat too feature-driven. It labored effective, all of the enterprise
guidelines had been revered. Nevertheless it felt prefer it duplicated a part of the enterprise logic.

Function-driven design of an combination deserves its personal weblog submit. It’s not a nasty place to begin. Studying area is a
course of. With new insights, you’ll have to regulate the mannequin.

On the finish, of this considerably lengthy introduction, we realized that we had two occasions representing the identical idea.
And we determined to take away one in all them.

Then the query stays. What to do with the occasions which might be already within the stream?

There are a number of methods to cope with that scenario.

We determined to upcast these occasions. Why? Occasions are immutable and shouldn’t be eliminated. Having these in stream will make it
simpler to entry them and perceive the total image every time wanted. It’s particularly vital when one thing goes

What’s upcasting?

Upcasting is the method of changing an occasion to a more recent model of the occasion. In our case, the occasion was
As I discussed earlier, we found this whereas implementing a brand new enterprise use case. It turned out that two of the
symbolize the identical idea.

In our case, upcasting will convert the outdated occasion that was duplicated to the opposite one which was initially there.
and must be the one one which represents that enterprise idea.

upcast occasions in RailsEventStore?

There are most likely a number of methods to implement the main points. Nonetheless, the overall thought is to make use of a
to the pipeline. Within the transformation, we setup the RailsEventStore to transform the outdated occasion to the brand new one.

Check out the instance under.
  # ... different transformations
      "Module::RemovedEvent" => ->(document) do
          event_id: document.event_id,
          metadata: document.metadata,
          timestamp: document.timestamp,
          valid_at: document.valid_at,
          event_type: "Module::TheOtherEvent",
          information: document.information

The pipeline mapper, referred to by the key phrase mapper, is a spot the place you possibly can add numerous transformations.

The Upcast transformation takes hash as an argument. The hot button is the identify of the outdated occasion. The worth is an lambda,
that takes the outdated occasion document and returns a brand new one. Or in our case, present one which was duplicated.

There are transformations accessible within the RailsEventStore. You may check out them for reference. For instance,
the SymbolizeMetadataKeys
or WithIndifferentAccess
It’s additionally price familiarizing your self with
the JSONClient.

When to make use of upcasting?

In case you are utilizing occasion sourcing, it’s not really helpful that you simply delete occasions. Occasions are immutable and
must be left as they had been. This makes plenty of sense. It makes the appliance extra dependable. It’s good for auditing.

Alternatively, if you recognize the occasion shouldn’t be within the stream, you could possibly rewrite the stream and solely embrace the
occasions that must be there. On this case, it felt pointless. The occasion was legitimate, it was simply duplicated.

It’s good to be taught completely different methods and know when to make use of them. On this case, upcasting appeared to be the very best



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments