Why doesn’t this work?!
public interface IBus
{
void Subscribe<T>(ISubscribe<T> subscriber) where T : class, IEvent;
void Send<T>(IEvent @event) where T : class, IEvent;
}
class InMemoryEventBus : IBus
{
private readonly IDictionary<ISubscribe<IEvent>, Type> _subscribers;
public InMemoryEventBus()
{
_subscribers= new Dictionary<ISubscribe<IEvent>, Type>();
}
public void Subscribe<T>(ISubscribe<T> subscriber) where T : class, IEvent
{
_subscribers.Add(subscriber, typeof(T));
}
public void Send<T>(IEvent @event) where T : class, IEvent
{
foreach (var subscriber in _subscribers.Where(subscriber => subscriber.Value == typeof(T)))
{
subscriber.Key.Handle(@event);
}
}
}
public interface IEvent
{
Guid EventId { get; set; }
}
public interface ISubscribe<T> where T : IEvent
{
void Handle(T @event);
}
public class StockLevelDroppedBellowMinimumLevelEvent : IEvent
{
public Guid EventId { get; set; }
public string Message { get; set; }
}
I get:
cannot convert from 'IHandle<T>' to 'IHandle<IEvent>'
EDIT: It was actually
ISubscribewhich needed to be covariant – but now that we can see the declaration ofISubscribe, you can’t make it covariant – only contravariant.In general, types aren’t generically variant. For example, an
ICollection<string>isn’t anICollection<object>– which is a good job, otherwise this would compile:In your case, I believe you’ll need to change your dictionary – you’ll be able to guarantee that the “right” type of
ISubscribeis used in each case. However, you’re using the dictionary in a very odd way – you should reverse the key and value types, like this: