Event Repository
The framework
module provides helper classes, encapsulating the persistence
of Java events. This relieves the user from:
- using the low-level ESDB Client API directly
- dealing with client-specific exceptions in favor of framework exceptions
- upcasting events older event representations if needed
- dealing with
payload
andmetaData
representation within events - mapping JSON to Java (event) objects and vice versa, by applying an appropriate
EventTypeResolver
The EventReader
interface defines operations for reading
events from the EventSourcingDB, including:
- reading raw
Event
s - reading upcasted
Event
s - reading Java event objects and their optional meta-data from
EventData
The ImmediateEventPublisher
interface defines operations for publishing
events to the EventSourcingDB, including:
- atomically publishing one or more Java event objects and their optional meta-data
- specifying additional preconditions which must be fulfilled when publishing
All these operations are implemented within the EventRepository
class.
Suitable framework exceptions are thrown in case of an error.
Configuration
An instance of EventRepository
can be obtained,
either by manually instantiating it or using Spring Boot autoconfiguration.
Tip
In either case it is required to make sure Jackson Databind is
included as dependency, unless a custom EventDataMarshaller
implementation is used.
Manual Configuration
An instance of EventRepository
can be created providing the necessary
configuration properties:
- an
EsdbClient
instance for reading and publishing events - an
EventSource
instance encapsulating thesource
to be published - an
EventTypeResolver
instance used for resolving event types - an
EventDataMarshaller
instance for serializing and deserializing event payloads and meta-data - an
EventUpcasters
instance optionally containingEventUpcaster
instances for event upcasting
The following example shows, how to instantiate an EventRepository
using
a provided EsdbClient
instance, the built-in ClassNameEventTypeResolver
,
the built-in JacksonEventDataMarshaller
, and an empty instance
of EventUpcasters
(i.e. containing no upcasters):
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opencqrs.esdb.client.EsdbClient;
import com.opencqrs.esdb.client.Option;
import com.opencqrs.framework.serialization.JacksonEventDataMarshaller;
import com.opencqrs.framework.types.ClassNameEventTypeResolver;
import com.opencqrs.framework.upcaster.EventUpcasters;
import java.util.Set;
public class EventRepositoryConfiguration {
public static EventRepository eventRepository(EsdbClient esdbClient) {
return new EventRepository(
esdbClient,
new EventSource("http://my-service.com"),
new ClassNameEventTypeResolver(EventRepositoryConfiguration.class.getClassLoader()),
new JacksonEventDataMarshaller(new ObjectMapper()),
new EventUpcasters()
);
}
}
The correct configuration can be confirmed, for instance by calling readAsObject()
as follows:
public static void main(String[] args){
EsdbClient client = ...;
EventRepositoryConfiguration
.eventRepository(client)
.readAsObject("/books", Set.of(new Option.Recursive()))
.forEach(System.out::println);
}
Spring Boot Auto-Configuration
For Spring Boot applications using the framework-spring-boot-starter
module
and Jackson Databind
EventPersistenceAutoConfiguration
provides both
an EventSource
Spring bean configured based on the
spring.application.name
and a fully configured EventRepository
Spring bean.
With that configuration in place the auto-configured EventRepository
instance
can be auto-wired within any other Spring bean, if needed. The configuration can be further customized by:
- overriding the
EventRepository
Spring bean with an application-defined one - by providing a custom
EventTypeResolver
Spring bean - by providing a custom
EventDataMarshaller
Spring bean - by defining
EventUpcaster
Spring beans to be registered withinEventUpcasters
automatically