public async Task <TResponse> Ask <TResponse>(Domain.Bus.Query.Query query) { var handler = GetWrappedHandlers <TResponse>(query); if (handler == null) { throw new QueryNotRegisteredError(query); } return(await handler.Handle(query, _provider)); }
private QueryHandlerWrapper <TResponse> GetWrappedHandlers <TResponse>(Domain.Bus.Query.Query query) { Type[] typeArgs = { query.GetType(), typeof(TResponse) }; var handlerType = typeof(QueryHandler <,>).MakeGenericType(typeArgs); var wrapperType = typeof(QueryHandlerWrapper <,>).MakeGenericType(typeArgs); var handlers = (IEnumerable)_provider.GetService(typeof(IEnumerable <>).MakeGenericType(handlerType)); var wrappedHandlers = (QueryHandlerWrapper <TResponse>)_queryHandlers.GetOrAdd(query.GetType(), handlers.Cast <object>() .Select(handler => (QueryHandlerWrapper <TResponse>)Activator.CreateInstance(wrapperType)) .FirstOrDefault()); return(wrappedHandlers); }