Event Sourcing (5): How to Store Event Types

Original link: https://teddy-chen-tw.blogspot.com/2022/07/5.html

June 30 18:41~19:43

截圖 2022-06-30 下午7.06.54

▲Figure 1: Each domain event needs to store its type (Type) data

foreword

Whether it is Event Sourcing or Outbox, domain events must be “serialized” before they can be stored in the database, and when read from the database, they are “deserialized” and become domain event objects. This serialization and deserialization process can only be completed by knowing the type of the domain object. Therefore, domain objects stored in the Event Store must record the event type, as shown in Figure 1. The question to be discussed today is: “How to record the domain object type in the Event Store?”

***

Record Package Name + Class Name

At first glance, this question seems very simple, ah, why not just store the complete type of Domain Event? For example, to store TagEvents.TagCreated field events, just call:

TagEvents.TagCreated.class.getCanonicalName()

Just store the following return value in the event type field of the database.

ntut.csie.sslab.ezkanban.kanban.tag.entity.TagEvents.TagCreated

When deserializing, use class.forName() to generate domain event objects.

If you never modify the domain event or the package name, nor move the domain event to a different package, then storing the full type of the domain event directly is fine. However, as long as the domain event is renamed or moved to a different package, a class not found exception will occur when reading the old domain event stored in the Event Store.

When you see this, you may think: “Can’t I update the data in the event type field in the database?” But the Event Sourcing system has a feature that basically developers should not modify the existing domain events. Generally speaking, this method is not recommended.

***

Type Mapping

In general, it is recommended to use type comparison . During serialization, a unique and fixed string is stored in the database for each field event, and deserialization is based on this fixed string. Table” to find out the corresponding domain event type.

The implementation method is very simple, first declare a DomainEventTypeMapper interface, as shown in Figure 2.

截圖 2022-06-30 下午7.16.02

▲Figure 2: DomainEventTypeMapper interface

Next, write a DomainEventTypeMapper for all domain events of each Aggregate. Figure 3 is the DomainEventTypeMapper used for Tag. For the four domain objects TageCreated, TagRenamed, TagColorChanged, and TagDeleted, four unique and unchanged strings are declared as the event types written to the Event Store (Lines 55~58) ).

Lines 60 to 66 set which real Java field object category the four domain object type strings represent, that is, the table required to establish type comparison.

截圖 2022-06-30 下午7.20.51

▲Figure 3: DomainEventTypeMapper of Tga

ezKanban will call the DomainEventMapper category in Figure 4 during the serialization and deserialization process. The toMappintType() method on line 28 converts the domain event into a fixed type string, and line 38 is used during deserialization. Realm object type string to find the real realm object.

截圖 2022-06-30 下午7.35.07

▲Figure 4: DomainEventMapper category

***

next episode preview

Event Sourcing is very convenient for write operations, no OR-Mapping is required, just write domain events. But what to do when you want to inquire about data? For example, TagRepository can only find a single Tag based on tagId. What should I do if I want to find all the tags in a certain Board? The next episode discusses this issue.

***

Yuzo’s inner monologue: This episode is much simpler.

This article is reprinted from: https://teddy-chen-tw.blogspot.com/2022/07/5.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment