Get access to an event's context (metadata) in a processor

The basic pattern for an event processor is to implement the ICanProcessEvents -interface and write Process -methods that take an event as an argument (and give the method an EventProcess attribute).

Like so:

    namespace your.namespace
    {
        using Dolittle.Events.Processing;

        [EventProcessor("your-guid-here")]
        public void Process(event_type evt)
        {
            // processing goes here...
        }
    }

But, what if you want more information about the event, like when it happened and such? That’s really easy - you just add another argument to your process-method - the EventMetadata (which is in Dolittle.Runtime.Events):

        [EventProcessor("your-guid-here")]
        public void Process(
            event_type evt, 
            Dolittle.Runtime.Events.EventMetadata metadata
        )
        {
            // now you can know when the event was created
            var time_of_event = metadata.Occurred;
        }

I use this all the time - particularly since I often write new processors for existing events and therefore shouldn’t use System.DateTime.UtcNow() as that will be the time of processing not the time of the event.

Here’s the EventMetadata's source if you’re wondering what is in there: https://github.com/dolittle-runtime/Runtime/blob/master/Source/Events/EventMetadata.cs

3 Likes