0

I want to add a items into the generic dictionary. Here is my example:

public class EventHandlerService : IEventHandlerService
    {
        private readonly Dictionary<EventType, IEventHandler<IEvent>> handlerDictionary = new Dictionary<EventType, IEventHandler<IEvent>>();
        public EventHandlerService(IAzureBlobStorage azureBlobStorage)
        {
            handlerDictionary.Add(EventType.CARD_BLOCK, new CardBlockedEventHandler(azureBlobStorage));
        }

        public void HandleCommand(IEvent @event, ILogger log)
        {
            var commandHandler = handlerDictionary[@event.EventType];
            commandHandler.HandleAsync(@event, log);
        }
    }

IEvent handler:

public interface IEventHandler<TEvent> where TEvent : IEvent
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="event"></param>
        Task HandleAsync(TEvent @event, ILogger logger);
    }

Handler:

public class CardBlockedEventHandler : IEventHandler<CardBlockedEvent>
    {
        private readonly IAzureBlobStorage _azureBlobStorage;

        public CardBlockedEventHandler(IAzureBlobStorage azureBlobStorage)
        {
            _azureBlobStorage = azureBlobStorage;
        }

        public async Task HandleAsync(CardBlockedEvent cardBlockedEvent, ILogger log)
        {
            log.LogInformation($"Card blocked event received for account - {cardBlockedEvent.Message}");

            throw new NotImplementedException();

        }
    }

IEvent interface:

public interface IEvent
    {
        EventType EventType { get; }
    }

Can I create a kind of generic dictionary? Or I can only create with non-generic IEventHandler.

Thoughts?

I have already tried with non-generic IEventHandler, but in that case i am not able access the properties of CardBlockedEvent in the event handler:

public class CardBlockedEventHandler : IEventHandler
    {
        private readonly IAzureBlobStorage _azureBlobStorage;

        public CardBlockedEventHandler(IAzureBlobStorage azureBlobStorage)
        {
            _azureBlobStorage = azureBlobStorage;
        }


        public Task HandleAsync<CardBlockedEvent>(CardBlockedEvent cardBlockedEvent, ILogger log) where CardBlockedEvent:IEvent
        {
            log.LogInformation($"Card blocked event received for account - {cardBlockedEvent.Message}");

            throw new NotImplementedException();
        }
    }
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Rakesh Kumar
  • 2,701
  • 9
  • 38
  • 66
  • 2
    You seem to want to add an instance of `CardBlockedEventHandler`, which is an `IEventHandler` as the value of a `Dictionary>`. But `CardBlockedEventHandler` is not a `IEventHandler`. An `IEventHandler` needs to be able to handle _any_ `IEvent`, whereas `CardBlockedEventHandler` can only handle `CardBlockedEvent`. – Sweeper May 23 '20 at 18:59
  • The previous comment explains the fundamental issue. More generally, the interface type is invariant on the type parameter `TEvent`, so you can't just replace one with another. See duplicate, and all the other questions with answers on the site that discuss this exact aspect of generic type variance. – Peter Duniho Mar 01 '21 at 07:02

0 Answers0