Esempio n. 1
0
        public IQueryResult <T> InvokeQuery <T>(IQuery query)
        {
            try
            {
                var handler       = queryHandlerFactory.GetHandler(query);
                var sendingMethod = handler.GetType().GetRuntimeMethods().FirstOrDefault(m => m.Name == "Handle");
                T   result;

                if (sendingMethod != null)
                {
                    result = (T)sendingMethod.Invoke(handler, new object[] { query });
                    return(new QueryResult <T>()
                    {
                        Data = result, QueryStatus = ActionStatus.Success
                    });
                }

                throw new Exception($"Handle method not found on {handler.ToString()} object");
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 2
0
        public TResult Query <TResult>(IQuery <TResult> query)
        {
            var handler = _queryHandlerFactory.GetHandler(query);

            if (handler == null)
            {
                throw new InvalidOperationException($"Query `{query.GetType().FullName}` handler not found.");
            }

            return(handler.Handle(query));
        }
        private IQueryHandler GetHandler <TResult>(IQuery <TResult> query)
            where TResult : class
        {
            Contract.Requires(query != null);
            Contract.Ensures(Contract.Result <IQueryHandler>() != null);

            try
            {
                // get handler from factory
                return(_handlerFactory.GetHandler(query.GetType()));
            }
            catch (Exception e)
            {
                // rethrow exception has a query handling exception
                throw new QueryHandlingException(query, e, "Could not get a query handler for query.");
            }
        }
 public void CanGetHandlerForRegisteredQuery()
 {
     Assert.IsNotNull(_factory.GetHandler(typeof(Query1)));
 }