Registering explicit Event Types
Events require a unique type to identify their corresponding class when loading them from the EventSourcingDB. This type is assigned once, when publishing the event.
The EventRepository is responsible for
resolving the type identifier using a configurable EventTypeResolver,
both for reading and writing events.
Derived Class-Based Type Identifiers
By default, when using the framework's Spring Boot Starter, ClassNameEventTypeResolver is registered
automatically. This implementation uses the fully-qualified Java classname as type identifier.
Interoperability of Event Classes
The use of this type resolver is strongly discouraged, especially with respect to interoperability with other programming languages. Also package or classname refactoring using this type resolver, requires additional upcasting.
Explicit Type Registration
For explicit type registration a PreconfiguredAssignableClassEventTypeResolver
can be defined as Spring Bean within the application context (1), for instance within a dedicated OpenCqrsFrameworkConfiguration, as follows:
- Refer to manual configuration if you want to register it manually with the
EventRepository.
package com.opencqrs.example.configuration;
import com.opencqrs.example.domain.book.events.*;
import com.opencqrs.framework.types.PreconfiguredAssignableClassEventTypeResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
@Configuration
public class OpenCqrsFrameworkConfiguration {
@Bean
public PreconfiguredAssignableClassEventTypeResolver eventTypeResolver() {
return new PreconfiguredAssignableClassEventTypeResolver(
Map.of(
"com.opencqrs.library.book.purchased.v1", BookPurchasedEvent.class,
"com.opencqrs.library.book.lent.v1", BookLentEvent.class,
"com.opencqrs.library.book.returned.v1", BookReturnedEvent.class,
"com.opencqrs.library.book.page.damaged.v1", BookPageDamagedEvent.class
)
);
}
}
This bean maps type identifiers to Java classes and is used when:
- writing the event object to persist the type identifier within the raw event
- reading the event to hint the
EventDataMarshallerhow to deserialize the event
As shown in the highlighted lines, the type identifier may include a version to be able to evolve events using upcasters.
Tip
Make sure to register all of your event classes, as any manually defined
EventTypeResolver bean supersedes the auto-configured ClassNameEventTypeResolver.
In other words, there is (for obvious reasons) no fallback to class-based types,
once the bean has defined explicitly.