MessageHandler

How to configure the Event Sourcing Runtime

C

The event sourcing runtime, helps you to build aggregate roots and projections, which are both patterns that work on event streams.

Setting up the configuration

To set up the event sourcing runtime, you'll first need to configure it by creating an instance of EventsourcingConfiguration.

This class can be found in the package MessageHandler.EventSourcing, but usually you don't need to explicitly install it as the technology specific packages already depend on it.

PM> Install-Package MessageHandler.EventSourcing

You can optionally pass in a handler runtime configuration instance, to make the event sourcing runtime share the runtime environment with other parts of the framework or hosting environment.

var eventsourcingConfiguration = new EventsourcingConfiguration(runtimeConfiguration);

Setting up the event source

Once you have a configuration instance, you must set an event source.

An event source for the Azure Table Storage service can be found in the MessageHandler.EventSourcing.AzureTableStorage package.

PM> Install-Package MessageHandler.EventSourcing.AzureTableStorage
var eventSource = new AzureTableStorageEventSource("StorageConnectionString", "TableName");
eventsourcingConfiguration.UseEventSource(eventSource);

Enabling projections

To make projections known to the runtime, you can register them using the EnableProjections configuration method

eventsourcingConfiguration.EnableProjections(typeof(YourProjection), typeof(AnotherProjection));

Adding transient channels

Whenever you need to dispatch persisted messages to a transient destination, you can add the channel towards that destination using the UseChannel configuration method.

eventsourcingConfiguration.UseChannel<ForwardToSignalr>();

The Push method of this channel will be called after events have been flushed to the event store.

public class ForwardToSignalr : MessageHandler.EventSourcing.DomainModel.IChannel
{
    public Task Push(IEnumerable<SourcedEvent> events)
    {
           
    }
}

Note: Channels are only to be used for transient communication, and are not to be considered guaranteed or durable.

Adding the outbox, a transactional channel

When you need durable, transactional, communication from the event store towards a destination, use the outbox instead.

The outbox can be found in the MessageHandler.EventSourcing.Outbox package.

PM> Install-Package MessageHandler.EventSourcing.Outbox

The outbox integrates the event sourcing runtime with the transactional processing runtime, where the event store acts as an input for a message pump.

eventsourcingConfiguration.EnableOutbox("StreamType", "Owner", pipeline =>
{
    pipeline.RouteMessages(to => to.Topic("your.topic", "ServiceBusConnectionString"));
});

StreamType is the name of the stream from which events need to be pumped.

Owner represents the instance of the pump, if not specified it will default to the handler instance id.

The pipeline callback, passed in, represent an outbound transactional dispatching pipeline configuration, which can be configured using the transactional processing configuration.

Register the runtime in the hosting environment

When hosting MessageHandler in a framework where you don't control the startup point, such as asp.net, you should register the runtime to automatically start along with the framework.

eventsourcingConfiguration.RegisterEventsourcingRuntime();

Manually creating the runtime

Not all frameworks start up automatically though, for instance a console application.

In such cases, you can create and the runtime manually and resolve the main enrypoints from the runtime object.

var runtime = EventsourcingRuntime.Create(eventsourcingConfiguration);

var repository = runtime.CreateAggregateRepository();
var projectionInvoker = runtime.CreateProjectionInvoker();
var projectionRestorer = runtime.CreateProjectionRestorer();

Sign up to our newsletter to get notified about new content and releases

You can unsubscribe at any time by clicking the link in the footer of your emails. I use Mailchimp as my marketing platform. By clicking subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.