Skip to content

The Event Factory

Tomas Lycken edited this page Oct 30, 2017 · 1 revision

Since you can create your own event type and add whatever metadata you need, the implementation depends on having an event factory available, which will take your event payloads, add all the metadata and return an instance of the event type. In most cases, though, you'll be fine with an event type that's just a plain implementation of the IEvent<TStreamId> interface, and for that case the default event factory will be enough:

var eventFactory = new DefaultEventFactory<TStreamId, TEvent>();

If you're using extra metadata, you can subclass DefaultEventFactory<TStreamId, YourEventType> and override the CreateSingle method, relying on base.CreateSingle() to create an instance with the default metadata already set, and then add your own before returning the instance:

public class UserAwareEventFactory : DefaultEventFactory<long, UserAwareEvent>
{
    public override CreateSingle(long streamId, long version, object payload)
    {
        var event = base.CreateSingle(streamId, version, payload);
        event.TriggeredBy = GetTriggeringUser();
        return event;
    }
}