Kelson.Common

Collection of packages published under the Kelson.Common namespace

View the Project on GitHub KelsonBall/Kelson.Common

Request Response Eventing

Create or inject an event manager for events to be managed by. Using .Provide<T> and .Request<T> allows the event manager to work in place of a dependency injection container.

// using Kelson.Common.Events;

var events = new EventManager();

Provide/Request

var doodad = new Doodad();
events.Provide<IDoodad>(() => doodad);
events.Provide<IWidget>(() => new WidgetInstance());

var myDoodad = events.Request<IDoodad>();
var myWidget = events.Request<IWidget>();

void Handle<TRequest, TResponse>(Func<TRequest, Response> handler)

events.Handle<int, string>(i => Convert.ToString(i, 2));

Requests will return a response for every handler

events.Handle<int, string>(i => Convert.ToString(i, 2));

events.Request<int, string>(127);
> [ "7f" ]

// add another handler with the same signature
events.Handle<int, string>(i => i.ToString().Reverse());

events.Request<int, string>(127);
> [ "7f", "721" ]

Note that the order of responses is not guarantied

To unsubscribe manually from requests keep a reference to the subscription token returned by Handle or Subscribe

void ISubscription.Unsubscribe()

var token = events.Handle<int,string>(i => i.ToString());

// using System.Linq
events.Request<int, string>(127).SingleOrDefault(); 
> "127"

token.Unsubscribe();

events.Request<int, string>(127).SingleOrDefault();
> null