An Rx Event Aggregator in C#

I have a web project that is made up of several modules, each of which generates DDD domain events. Right now my C# project is small and divided into well-defined modules which run in a single process, so using a message queue like Kafka is a bit of overkill. But as the program scales, I may break these modules out and run them as full microservices in their own processes. At that point, I will need to use a real messaging system like Kafka or the Azure Event Hub.

In the meantime, I need a simple Event Hub/Event Aggregator/Messaging Hub that can be easily replaced by something else later on. I found several libraries that were overly complicated or overly-opinionated, but ultimately came across this super-simple Reactive-Extension code from Reactive.EventAggregator. I modified the subscription slightly so that a caller doesn’t need to know about Rx.

The Simplest Thing that Could Possibly Work to handle Domain Events:

It maintains one aggregated stream of events as a Subject and makes use of OfType to divide it by type.

The tests show how to use it—multiple subscribers can listen for the same event, and you can unsubscribe from events by calling Dispose (e.g. via using):

To make things easy, I wrapped the EventAggregator in a static class that can be accessed from anywhere rather than using Dependency Injection, similar to Udi Dahan’s solution.