Use Event store to pre subscribe to events

Is it possible to pre-subscribe to events from an event stream? Something like the folllowing:

public sealed class BonusingEventBus : IEventBus

{

private readonly IDomainEventHandlerFactory _eventHandlerBuilder;

private IDictionary<Type, EventHandlerInvoker> _eventHandlerInvokers;

public BonusingEventBus(IEnumerable eventHandlerTypes, IDomainEventHandlerFactory eventHandlerBuilder)

{

_eventHandlerBuilder = eventHandlerBuilder;

BuildEventInvokers(eventHandlerTypes);

}

public void PublishEvent(DomainEvent domainEvent)

{

var domainEventType = domainEvent.GetType();

**List invokers = **

**(from eventHandlerInvoker in _eventHandlerInvokers **

**where eventHandlerInvoker.Key.IsAssignableFrom(domainEventType) **

select eventHandlerInvoker.Value).ToList();

invokers.ForEach(i => i.Publish(domainEvent));

}

I basically want to move all subscription code to the beginning of the program so that I do not want other developers to specifically subscribe to events. I am currently doing this with an in-memory event bus and was wondering if this was possible using the event store. Basically use the event store as a bus and persist the events too.

-Arun

Try subscribetostream ....

Then write an event to it.

If you are doing for many types I would probably subscribe to all and
dispatch myself as opposed to having lots of subscriptions.

eg subscribetoall x => publish(x)

I actually just implemented something with the same purpose today.
What we do, is we inject our handler-instances, that implement a collection of { Type EventType, Action HandlerMethod } each.

Then order that in a Dictionary<Type,IEnumerable<Action>, before starting subscriptions, 1 subscription for each type, to the $ef-{your EventType} projection, and just catch up those subscriptions. We do have an in-memory concurrent queue in the bounded contexts, and im looking into mapping these subscriptions via another projection, into a stream per Bounded Contexts, to preserve ordering and clarify the bounderies of the contexts.

The code kinda like yours goes i in a “SubscriptionManager”, and the “IEventstreamer” is easily switched from a real Eventstore, to an InMemory implementation.

Instead of “IsAssignable from” or explecit casts (except one isolated place), we used the {Type EventType, Action HandlerMethod} like this:

Subscription : ISubscription

{

public Type EventType {get; private set;}

public Action HandlerMethod {get; private set;}

private Subscription(Type eventType, Action handlerMethod) //encapsulated constructor

{ you know what goes here }

public static For(TEvent e, Action h)

{

return new Subscription(typeof(TEvent), (o) => h((TEvent)o);

}

}

That way, we can subscribe (pre-?) like this:

public class SomeHandler : ISubscribeToEvents

{

public IEnumerable Subscriptions {get; private set;}

public SomeHandler(dependencies)

{

//dependencies

Supscriptions = new[]

{

Subscription.For(ICanNameMyMethodAnything),

}

}

public void ICanNameMyMethodAnything(MyEventHappened myEvent)

{ //suff goes here }

}

Hope that helpsfgrt

/Julian

Ps.: Eventstore is awesome, really good work indeed, Mr. Young and team!

Looks good.

Thanks, will try this out.