internal static QueryHandlerDelegate <TResult> FromFactory <TQuery, TResult>(Func <IQueryAsyncHandler <TQuery, TResult> > queryHandlerFactory)
            where TQuery : class, IQuery <TResult>
        {
            return(new QueryHandlerDelegate <TResult>(async(inputQuery, ct) =>
            {
                TQuery query = EnsureValidQuery <TQuery, TResult>(inputQuery);
                IQueryAsyncHandler <TQuery, TResult> instance = EnsureInstanceFromFactory(queryHandlerFactory);

                return await instance.HandleAsync(query, ct).ConfigureAwait(false);
            }));
        }
Esempio n. 2
0
        /// <summary>
        /// <para>Resolves an instance of IQueryAsyncHandler<TQuery, TResult> from the container</para>
        /// <para>and converts it to a query handler delegate which can be invoked to process the query.</para>
        /// </summary>
        /// <typeparam name="TQuery">Type of query which is handled by the query handler.</typeparam>
        /// <typeparam name="TResult">Type of query's result.</typeparam>
        /// <returns>Instance of <see cref="QueryHandlerDelegate{TResult}"/> which executes the query handler processing.</returns>
        public QueryHandlerDelegate <TResult> ResolveQueryHandler <TQuery, TResult>() where TQuery : class, IQuery <TResult>
        {
            try
            {
                IQueryAsyncHandler <TQuery, TResult> queryAsyncHandler = _containerAdapter.Resolve <IQueryAsyncHandler <TQuery, TResult> >();

                if (queryAsyncHandler == null)
                {
                    // No handlers are resolved. Throw exception.
                    throw ExceptionBuilder.NoQueryHandlerResolvedException(typeof(TQuery));
                }

                return(QueryHandlerDelegateBuilder.FromQueryHandler(queryAsyncHandler));
            }
            catch (Exception ex)
            {
                // No handlers are resolved. Throw exception.
                throw ExceptionBuilder.NoQueryHandlerResolvedException(typeof(TQuery), ex);
            }
        }
 internal static QueryHandlerDelegate <TResult> FromQueryHandler <TQuery, TResult>(IQueryAsyncHandler <TQuery, TResult> queryAsyncHandler)
     where TQuery : class, IQuery <TResult>
 {
     return(new QueryHandlerDelegate <TResult>(async(inputQuery, ct) =>
     {
         TQuery query = EnsureValidQuery <TQuery, TResult>(inputQuery);
         return await queryAsyncHandler.HandleAsync(query, ct).ConfigureAwait(false);
     }));
 }