This library gives you an ability to facilitate communication between loosely coupled components in an application.
Inspired by Prism's EventAggregator.
Note: Main difference from Prism is that this library does not support different threads event publishing/handling. The event will be handled in that thread it was raised in. Also, code was refactored to use latest .net and c# features.
Register event aggregator in service collection:
serviceCollection.AddSingleton<IEventAggregator, EventAggregator>();
or use as singleton
EventAggregator.Current.GetEvent<>()
Create an event:
public class SomeEvent : PubSubEvent { }
or generic version
public class SomeGenericEvent : PubSubEvent<payload_type> { }
Subscribe to an event:
IEventAggregator _eventAggregator;
public void EventHandler() { }
_eventAggregator.GetEvent<SomeEvent>().Subscribe(EventHandler);
public void GenericEventHandler(payload_type payload) { }
_eventAggregator.GetEvent<SomeGenericEvent>().Subscribe(GenericEventHandler);
Publish an event:
IEventAggregator _eventAggregator;
_eventAggregator.GetEvent<SomeEvent>().Publish();
_eventAggregator.GetEvent<SomeGenericEvent>().Publish(payload);
Note: you may not unsubscribe from events, because it has weak reference, but better to do unsubscribe