-
Notifications
You must be signed in to change notification settings - Fork 2
Storage Engine: EF Core
As with EF6, most of the work is already done for you, but you'll need to create specializations of a few types to indicate to RdbmsEventStore what behavior you want.
In addition to the IEvent<TStreamId>
interface and its default implementation Event<TStreamId>
, which had a Type
and an object
representing your event payload, you also need an implementation of IPersistedEvent<TId, TStreamId>
, which instead has a string
and a byte[]
, representing the serialized payload. The generic type parameter TId
in IPersistedEvent<TId, TStreamId>
indicates the primary key type in the table where you'll store your events.
There is a default implementation EFCoreEvent<TId, TStreamId>
in the library, but since EF Core doesn't support generic entity types, you must create a non-generic subclass for it:
public class YourPersistedEvent : EFCoreEvent<Guid, string> { } // or EFCoreEvent<long, long>, etc
The id types can be the same or not, it's entirely up to you. If you want to add metadata, such as the user id of the user who triggered the event, you can include that in YourPersistedEvent
too.
This can also inherit directly from the built-in EFCoreEventStoreContext<TStreamId, TEvent>
:
public class YourEventStoreContext : EFCoreEventStoreContext<string, YourPersistedEvent>
{
public YourEventStoreContext(DbContextOptions<YourEventStoreContext> options) : base(options) { }
}
The constructor parameter is just passed along to the underlying DbContext
.
The EFCoreEventStore
needs a couple of auxiliary types to function. If you're not customizing your event, you can use the default implementations of all of these, but if not, read more on each respective page.
Usually you'll let your DI container handle this, but here's a raw sample so you know what you need.
var eventRegistry = new AssemblyEventRegistry(typeof(SomeEventPayload));
var serializer = new DefaultEventSerializer<string, Event<string>, YourPersistedEvent>(eventRegistry);
var eventFactory = new DefaultEventFactory<Guid, string, YourEvent>();
var writeLock = new WriteLock();
var context = new YourEventStoreContext();
var eventStore = new EFCoreEventStore<Guid, string, YourEventStoreContext, Event<string>, IEventMetadata<string>, YourPersistendEvent>(
context,
eventFactory,
writeLock,
serializer);