A thread-safe, high performance & easy to use cross platform implementation of the Event Aggregator Pattern.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Easy.MessageHub.Benchmarker.Core * Fixed issue: #7; * Minor refactoring. Sep 19, 2017
Easy.MessageHub.Benchmarker * Exposed an override for Subscribe in the interface.\r\n * Migrated … May 20, 2017
Easy.MessageHub.Tests.Unit Minor refactoring. Jun 9, 2018
Easy.MessageHub Improved doc Jun 9, 2018
.gitignore * Exposed an override for Subscribe in the interface.\r\n * Migrated … May 20, 2017
Easy.MessageHub.sln * Exposed an override for Subscribe in the interface.\r\n * Migrated … May 20, 2017
Easy.MessageHub.v3.ncrunchsolution
LICENSE Happy New Year! Apr 17, 2017
NuGet-Pack.bat Cleaned up csproj and fixed assembly versioning. Dec 25, 2017
README.md Improved exception handling. Jun 9, 2018

README.md

NuGet Build status

Easy.MessageHub

An implementation of the Event Aggregator Pattern.

Supports .Net Core (.Net 3.5 & netstandard1.0) running on:

  • .Net Core
  • .Net Framework 3.5 and above
  • Mono & Xamarin
  • UWP
  • Windows 8.0
  • Windows Phone 8.1
  • Windows Phone Seilverlight 8.0

Usage example:

Start by getting the singleton instance of the hub:

var hub = MessageHub.Instance;

You can now use the hub to subscribe to any publication of a given type:

var token = hub.Subscribe<Person>(p => Console.WriteLine($"Id is: {p.Id}"));
// or    
Action<string> action = message => Console.WriteLine($"Message is: {message}");
var anotherToken = hub.Subscribe(action);

You can then use the token to do:

hub.IsSubscribed(token); // returns true
hub.Unsubscribe(token);
hub.IsSubscribed(token); // returns false

Or you can clear all subscriptions by:

hub.ClearSubscriptions();

Publication is as easy as:

hub.Publish(new Person { Id = "Foo" });
hub.Publish("An important message");

Error handling:

The hub catches any exception thrown at the time of publication and exposes them via:

hub.RegisterGlobalErrorHandler((token, e) => Console.WriteLine($"Error Publishing, Token: {token} | Exception: {e}"));

Global handler:

The hub allows the registration of a single handler which will receive every message published by the hub. This can be useful in scenarios where every message published should be logged or audited.

hub.RegisterGlobalHandler((type, eventObject) => Console.WriteLine($"Type: {type} - Event: {eventObject}"));

Event throttling:

The hub allows each subscriber to throttle the rate at which it receives the events:

hub.Subscribe<string>(msg => Console.WriteLine($"Message is: {msg}"), TimeSpan.FromSeconds(1));

In the above example, if the subscriber receives more than one message within 1 second of another, it drops them.

Inheritance support:

The hub supports inheritance by allowing to subscribe to a base class or an interface and receiving all the publications of types that inherit or implement the subscribed type. For example, given:

public class Order {}
public class NewOrder : Order{}
public class BigOrder : Order{}

A subscriber registering against Ordrer will also receive events of type NewOrder and BigOrder.

More details HERE