private IEnumerable <CommandHandlerWrapper> GetWrappedHandlers(Domain.Bus.Command.Command command) { var handlerType = typeof(CommandHandler <>).MakeGenericType(command.GetType()); var wrapperType = typeof(CommandHandlerWrapper <>).MakeGenericType(command.GetType()); var handlers = (IEnumerable)_provider.GetService(typeof(IEnumerable <>).MakeGenericType(handlerType)); var wrappedHandlers = _commandHandlers.GetOrAdd(command.GetType(), handlers.Cast <object>() .Select(handler => (CommandHandlerWrapper)Activator.CreateInstance(wrapperType))); return(wrappedHandlers); }
public async Task Dispatch(Domain.Bus.Command.Command command) { var wrappedHandlers = GetWrappedHandlers(command); if (wrappedHandlers == null) { throw new CommandNotRegisteredError(command); } foreach (var handler in wrappedHandlers) { await handler.Handle(command, _provider); } }