Search code examples
c#genericsprismeventaggregator

Prism - EventAggregator.GetEvent<>.Subscribe() - Using Generics & Contraints


I am having an issue subscribing to events with the event Aggregator that comes as part of the prism framework.

If I use something such as

eventAggregator.GetEvent<string>().Subscribe(MyMethod)

then it all works ok, my method fires when an event has been published.

However, when moving to a more complex object other than I string I run into problems.

I have a bunch of classes that all derive from an Interface (IRequest) for example

I have my event class setup as follows

public class MyEvent<TRequest> : PubSubEvent<MyClass<TRequest>> where TRequest : IRequest {}

I have a generic class (MyClass) that uses an object of IRequest inside it - again, all seems to work ok at this point.

Now, lets say I publish an event of MyClass that uses the object Profile inside it:

eventAggregator.GetEvent<MyEvent<Profile>>().Publish(myProfileObject);

In my subscription I would like a method that can catch all events of MyEvent - Irrelevant of whether T is Profile or some other object that derives from IRequest - this is where I seem to be having problems.

In the following examples, the first works, but the second doesn't - I would ideally like to use something similar to the second.

eventAggregator.GetEvent<MyEvent<Profile>>().Subscribe(test1);
void test1 (MyClass<Profile> obj)
{
  //Some stuff here
}

eventAggregator.GetEvent<MyEvent<IRequest>>().Subscribe(test2);
void test2<T>(MyClass<T> obj) where T : IRequest
{
  //never gets called
}

My assumption is that because Profile derives from IRequest then it should work??? But it doesn't!!

Any help would be appreciated.

UPDATE
If I use the following, it does work, but would require me to create a separate subscription for each type of IRequest available - I'm looking to only have the one subscription.

eventAggregator.GetEvent<MyEvent<Profile>>().Subscribe(test2);
eventAggregator.GetEvent<MyEvent<User>>().Subscribe(test2);

Solution

  • MyEvent<IRequest> and MyEvent<Profile> are not the same, so the EventAggregator doesn't see this as the same event. To allow for covariant types, you can fix it with the out modifier in C# 4. Something like this should work:

    public interface IMyClass<out T> where T : IRequest { }
    
    public class MyClass<T> : IMyClass<T> where T : IRequest { }
    
    public class MyEvent<TValue> : PubSubEvent<IMyClass<TValue>> where TValue : IRequest { }
    

    Then your event aggregator would look something like:

     _eventAggregator.GetEvent<MyEvent<IRequest>>().Subscribe(Received);
    
     private void Received(IMyClass<IRequest> obj)
     {
    
     }
    
    _eventAggregator.GetEvent<MyEvent<IRequest>>().Publish(new MyClass<Profile>());
    

    Does this help?