Skip to main content
added 541 characters in body
Source Link
Nikita B
  • 13.1k
  • 1
  • 26
  • 57

Maybe I misunderstood your intent, but MultipleProvidersCache looks a lot like an overly complex (API-wise) event aggregator (aka message hub) implementation. Even though different implementations have different flavors, normally when writing one, you should try to let go of event keyword. For example, the most basic API can look like this:

interface IEventAggregator
{
    IDisposable Subsribe<TMessage>(Action<TMessage> action);
    void Publish<TMessage>(TMessage message);
}

Here TMessage acts as EventArgs replacement. The idea is that any object can subscribe to "events" by calling Subsribe method and any object can invoke "events" by calling Publish. IEventAggregator acts as a mediator and passes messages from publishers to the list of subscribed Actions.

P.S. Note, that some commonly used frameworks (e.g. Prism, MvvmLight, and most other MVVM based frameworks) got you covered and already have some messaging system in place. Nuget has multiple standalone implementations available as well, if you are working on, say, Unity project. However implementing your own event aggregator is a great exercise (I have actually shared my implementation on SR, when I was playing around with Dataflow).

Maybe I misunderstood your intent, but MultipleProvidersCache looks a lot like an overly complex (API-wise) event aggregator (aka message hub) implementation. Even though different implementations have different flavors, normally when writing one, you should try to let go of event keyword. For example, the most basic API can look like this:

interface IEventAggregator
{
    IDisposable Subsribe<TMessage>(Action<TMessage> action);
    void Publish<TMessage>(TMessage message);
}

Here TMessage acts as EventArgs replacement. The idea is that any object can subscribe to "events" by calling Subsribe method and any object can invoke "events" by calling Publish. IEventAggregator acts as a mediator and passes messages from publishers to the list of subscribed Actions.

Maybe I misunderstood your intent, but MultipleProvidersCache looks a lot like an overly complex (API-wise) event aggregator (aka message hub) implementation. Even though different implementations have different flavors, normally when writing one, you should try to let go of event keyword. For example, the most basic API can look like this:

interface IEventAggregator
{
    IDisposable Subsribe<TMessage>(Action<TMessage> action);
    void Publish<TMessage>(TMessage message);
}

Here TMessage acts as EventArgs replacement. The idea is that any object can subscribe to "events" by calling Subsribe method and any object can invoke "events" by calling Publish. IEventAggregator acts as a mediator and passes messages from publishers to the list of subscribed Actions.

P.S. Note, that some commonly used frameworks (e.g. Prism, MvvmLight, and most other MVVM based frameworks) got you covered and already have some messaging system in place. Nuget has multiple standalone implementations available as well, if you are working on, say, Unity project. However implementing your own event aggregator is a great exercise (I have actually shared my implementation on SR, when I was playing around with Dataflow).

Source Link
Nikita B
  • 13.1k
  • 1
  • 26
  • 57

Maybe I misunderstood your intent, but MultipleProvidersCache looks a lot like an overly complex (API-wise) event aggregator (aka message hub) implementation. Even though different implementations have different flavors, normally when writing one, you should try to let go of event keyword. For example, the most basic API can look like this:

interface IEventAggregator
{
    IDisposable Subsribe<TMessage>(Action<TMessage> action);
    void Publish<TMessage>(TMessage message);
}

Here TMessage acts as EventArgs replacement. The idea is that any object can subscribe to "events" by calling Subsribe method and any object can invoke "events" by calling Publish. IEventAggregator acts as a mediator and passes messages from publishers to the list of subscribed Actions.