public async Task <TResult> QueryAsync <TResult>(IQuery <TResult> query) { if (query == null) { throw new ArgumentNullException(nameof(query)); } var queryType = query.GetType(); var handlerType = typeof(IQueryHandler <,>).MakeGenericType(queryType, typeof(TResult)); var handlers = handlerResolver.GetHandlers(handlerType); if (handlers.None()) { throw new MissingRegistrationException(queryType); } if (handlers.Multiple()) { throw new DuplicateRegistrationException(queryType); } var handler = handlers.Single() as dynamic; return(await handler.HandleAsync(query as dynamic)); }
public async Task CommandAsync <TCommand>(TCommand command) where TCommand : ICommand { if (command == null) { throw new ArgumentNullException(nameof(command)); } var commandType = typeof(TCommand); var handlerType = typeof(ICommandHandler <>).MakeGenericType(commandType); var handlers = handlerResolver.GetHandlers(handlerType); if (!configuration.AllowVoidCommandDispatch) { if (handlers.None()) { throw new MissingRegistrationException(commandType); } } if (!configuration.AllowMultipleCommandDispatch) { if (handlers.Multiple()) { throw new DuplicateRegistrationException(commandType); } } foreach (dynamic handler in handlers) { await handler.HandleAsync(command as dynamic); } }