Пример #1
0
 public TResult Execute(TQuery query)
 {
     try
     {
         return(_decorated.Execute(query));
     }
     catch (Exception ex)
     {
         Logger logger = LogManager.GetLogger(_decorated.GetType().ToString());
         logger.Error(ex, "Exception occured");
         throw;
     }
 }
        private void CacheHandler(IQueryHandler handler)
        {
            Contract.Requires(handler != null);
            Contract.Ensures((handler.Reusable == false && GetCachedHandler(handler.GetType()) == null) ||
                             (handler.Reusable && _handlers[handler.GetType()] != null));

            // if handler is reusable, cache
            if (handler.Reusable)
            {
                _handlers[handler.GetType()] = handler;
            }
            else
            {
                Contract.Assume(GetCachedHandler(handler.GetType()) == null);
            }
        }
 public CacheQueryHandlerDecorator(IQueryCacheHandler <TQuery, TResult> cacheHandler,
                                   IQueryHandler <TQuery, TResult> decorated)
 {
     this.cacheHandler    = cacheHandler;
     this.decorated       = decorated;
     this.cachedAttribute = decorated.GetType().GetCustomAttribute <CachedAttribute>();
 }
        public async Task <TQueryResult> GetAsync(TQuery query, CancellationToken token)
        {
            var attr = _decoratee.GetType().GetMethod("GetAsync")?.GetCustomAttribute <CacheAttribute>();

            if (attr == null)
            {
                return(await _decoratee.GetAsync(query, token));
            }

            if (_cacheProvider is null)
            {
                return(await _decoratee.GetAsync(query, token));
            }

            var cacheKey = $"{query.GetType().Name}_{query.AsDictionary(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).ToContentString()}";
            //var cacheKey = CacheResultInterceptor.GetCacheKey(_decoratee.GetType(), "GetAsync", new object[] { query });

            var result = _cacheProvider.Value.Get <TQueryResult>(cacheKey, attr.Region);

            if (result != null)
            {
                return(result);
            }


            result = await _decoratee.GetAsync(query, token);

            _cacheProvider.Value.Set(cacheKey, attr.Region, result, attr.Duration, attr.Slide);
            return(result);
        }
Пример #5
0
        public async Task <TResult> Dispatch <TParameter, TResult>(TParameter query)
            where TParameter : class
        {
            IQueryHandler <TParameter, TResult> handler = provider.GetService(typeof(IQueryHandler <TParameter, TResult>)) as IQueryHandler <TParameter, TResult>;

            Log.Information(handler.GetType().ToString());

            return(await handler.Retrieve(query));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ReceiveMessageQueryDecorator" /> class.
        /// </summary>
        /// <param name="metrics">The metrics factory.</param>
        /// <param name="handler">The handler.</param>
        /// <param name="connectionInformation">The connection information.</param>
        public ReceiveMessageQueryDecorator(IMetrics metrics,
                                            IQueryHandler <ReceiveMessageQuery, RedisMessage> handler,
                                            IConnectionInformation connectionInformation)
        {
            Guard.NotNull(() => metrics, metrics);
            Guard.NotNull(() => handler, handler);

            var name = handler.GetType().Name;

            _counter = metrics.Counter($"{connectionInformation.QueueName}.{name}.HandleAsync.Expired", Units.Items);
            _handler = handler;
        }
        public async Task <TProjection> HandleAsync(TQuery query, CancellationToken cancellationToken)
        {
            var queryName        = query.GetType().FullName;
            var queryHandlerName = _decorated.GetType().FullName;

            var stopwatch = new Stopwatch();

            try
            {
                stopwatch.Start();

                return(await _decorated.HandleAsync(query, cancellationToken));
            }
            catch (Exception exception)
            {
                var exceptionName = exception.GetType().FullName;

                _logger.LogWarning
                (
                    exception,
                    "Exception {ExceptionName} thrown with message {ExceptionMessage} when handling query {QueryName} using handler {QueryHandlerName}",
                    exceptionName,
                    exception.Message,
                    queryName,
                    queryHandlerName
                );

                throw;
            }
            finally
            {
                stopwatch.Stop();

                var elapsed = stopwatch.Elapsed.TotalMilliseconds;

                _logger.LogInformation
                (
                    "Handled query {QueryName} using handler {QueryHandlerName} in {ElapsedMilliseconds}ms",
                    queryName,
                    queryHandlerName,
                    elapsed
                );

                _logger.LogDebug
                (
                    "{@Query}",
                    query
                );
            }
        }
Пример #8
0
        public TResult Handle(TQuery query)
        {
            var result = _innerHandler.Handle(query);

            string resultString;

            try
            {
                resultString = result.ToJson();
            }
            catch (Exception)
            {
                resultString = "{}";
            }

            _logger.Info($"{query.GetType().Name}  ====>  {_innerHandler.GetType().Name}: {query.ToJson()} => {resultString}");

            return(result);
        }