0
votes

I have an application architecture like this:

enter image description here

When UDP servers inside the UDP service receive different types of messages it publishes Prism events to managers. Those UDP servers have their own threads so when the events published they sometimes cause multithreading issues inside the managers.

To prevent that I want to create an event handler and queue inside the subscribing managers. When subscriber receives an event, its only job is to try to Enqueue the event payload to ConcurrentQueue inside the managers and return (I think this is called store and forward). Then I will have a worker that will read this queue and send the event parameters to related methods. Every manager will have its own event queue, event handler and worker.

My "event queue":

enter image description here

But when I try to implement this I couldn't get past some issues:

1- When you subscribe to an event, public class TestEvent1 : PubSubEvent<Class1>, like this, GetEvent<TestEvent1>().Subscribe(OnTestEvent1), callback method(OnTestEvent1) must have same type of parameters of the event, in this case Class1, OnTestEvent1(Class1 class1). I need a type to store every received data and event type.

How can I use the same callback method for all my events inside the subscriber manager that have different types?

Example:

// Events
public class TestEvent1 : PubSubEvent<Class1>
{
}
public class TestEvent2 : PubSubEvent<Class2>
{
}
public class TestEvent3 : PubSubEvent<List<Class3>>
{
}

// Subscriptions
_eventAggregator.GetEvent<TestEvent1>().Subscribe(EventHandler, true);
_eventAggregator.GetEvent<TestEvent2>().Subscribe(EventHandler, true); 
_eventAggregator.GetEvent<TestEvent3>().Subscribe(EventHandler, true);

// Callback
private void EventHandler("What's inside here?")
{
    _eventQueue.Enqueue(payload);
    return;     
}

2- Since I want to store all of the received events for a manager inside a single queue, the same type problem applies to this too, what should be my T when creating ConcurrentQueue?

3- Is this viable? Are there any other approaches, patterns(I found mediator but I didn't research it deeply enough) that I can use like this?

What I tried:

1- I tried to use object or dynamic type but then I will lose compile-time security.

2- I thought of creating an interface and implementing it on all of my custom classes so I can use it for general type but what to do about events with built-in types?

PS: I had another question about this but I felt like I didn't explain myself clearly so I tried my best again.

1

1 Answers

0
votes

How can I use the same callback method for all my events inside the subscriber manager that have different types?

You don't. The event aggregator isn't designed that way. The type of the event is used to distinguish subscriptions and to distribute the event to the subscribers that actually want it, so you can only ever subscribe to an individual event type.

As said before, you shouldn't be using the event aggregator here at all, because there are much better tools for what you're trying to achieve.

Since I want to store all of the received events for a manager inside a single queue

If, for example, the manager was an dataflow ActionBlock it would come with a fully functional queue preinstalled.