Beispiel #1
0
        private object GetHandler <TResult>(IQuerySpec <TResult> querySpec)
        {
            if (_dict.ContainsKey(querySpec.GetType()))
            {
                return(_dict[querySpec.GetType()]);
            }
            var handlerType = typeof(IQueryHandler <,>).MakeGenericType(querySpec.GetType(), typeof(TResult));
            var handler     = _serviceProvider.GetRequiredService(handlerType);

            _dict.Add(querySpec.GetType(), handler);
            return(handler);
        }
Beispiel #2
0
        public async Task <TResult> ExecuteQuery <TResult>(IQuerySpec <TResult> querySpec)
        {
            var handler = GetHandler(querySpec);

            try
            {
                _logger.LogInformation($"Handling query {querySpec.GetType()}");

                var result = await(Task <TResult>)((dynamic)handler).Handle((dynamic)querySpec);
                return(result);
            }
            catch (Exception e)
            {
                _logger.LogError(e,
                                 $"Query exception occured during handling of {querySpec.GetType()} query in {handler.GetType()}");

                throw; //TODO: Decide if throwing is required
            }
        }