NimaAra / Easy.MessageHub

No need for .NET Events! A thread-safe, high performance & easy to use cross platform implementation of the Event Aggregator Pattern.
MIT License
258 stars 41 forks source link

Unflexible detection of the given type #24

Closed ValResnick closed 3 years ago

ValResnick commented 3 years ago

Hi,

i think my issue is related to https://github.com/NimaAra/Easy.MessageHub/issues/11.

In my scenario, every message implements the interface IMessage. The problem is, that the type detection doesn't use the real type of an object to detect the subscription, instead it uses the compiled generic parameter T.

Example:

hub.Subscribe<MyMessage>(...)
hub.Subscribe<MyOtherMessage>(...)

IMessage message;

if(something)
 message = new MyMessage();
else
 message = new MyOtherMessage();

hub.Publish(message)

The message will not be recognized for my subscriptions, because of the type 'IMessage'. The only workarround i see, is to use reflection. Is it possible to change this behaviour?

NimaAra commented 3 years ago

This is the expected behaviour. In your case you should be subscribing to IMessage.

image

ValResnick commented 3 years ago

But i dont want to subscribe to IMessage, because i dont want to have different messages in one subscription.

Only the subscription for MyMessage should be "fired". Because that is the real type. I think you should use GetType in publish instead of typeof(T)?

NimaAra commented 3 years ago

Why not? You can simply do: image

Calling GetType is slow compared to the current solution. Once again the library is behaving as expected. It is not the intention of the library to do runtime type checking. That kind of specific behaviour can be done by yourself.

ValResnick commented 3 years ago

Well, then i could only register to the global hook and check every possible messsage there...

In a normal, non high performance, scenario the GetType call will not hurt.

Thanks for your feedback. I will use Reflection to call the "correct" publish method or i take a different Api.