Last active
March 10, 2021 02:28
-
-
Save mikebridge/f6799ebed20160f72a3daf62f584d2ff to your computer and use it in GitHub Desktop.
A simple Event Aggregator that allows multiple subscribers per message type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reactive.Linq; | |
using System.Reactive.Subjects; | |
// inspired by https://github.com/shiftkey/Reactive.EventAggregator/blob/master/src/Reactive.EventAggregator/EventAggregator.cs | |
namespace Messaging | |
{ | |
public interface IEventAggregator : IDisposable | |
{ | |
IDisposable Subscribe<T>(Action<T> action); //where T : IDomainEvent; | |
void Publish<T>(T @event); //where T : IDomainEvent; | |
} | |
public class EventAggregator : IEventAggregator | |
{ | |
readonly Subject<object> _subject = new Subject<object>(); | |
public IDisposable Subscribe<T>(Action<T> action) | |
{ | |
return _subject.OfType<T>() | |
.AsObservable() | |
.Subscribe(action); | |
} | |
public void Publish<T>(T sampleEvent) | |
{ | |
_subject.OnNext(sampleEvent); | |
} | |
public void Dispose() | |
{ | |
_subject.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment