public static void SubscribeWith <TResponse>(this ISubscriptionManager manager, R.ResponsesQuery <TResponse> dto, Q.QueriesQuery <TResponse> query, string session)
        {
            var key = query.GetCacheKey();

            manager.Manage(dto.Payload, key, query.SubscriptionId, query.SubscriptionType ?? ChangeType.All, TimeSpan.FromSeconds(query.SubscriptionTime ?? 3600), session);
        }
        public static async Task <R.ResponsesQuery <TResponse> > RequestToElastic <T, TResponse>(this IMessageSession bus, T message, Q.QueriesQuery <TResponse> query) where T : IQuery where TResponse : class
        {
            var options = new SendOptions();

            options.SetDestination(RiakEndpoint);
            options.SetHeader(Aggregates.Defaults.RequestResponse, "1");

            var response = await bus.Request <IMessage>(message, options).ConfigureAwait(false);

            return(response.RequestQuery(query));
        }
        public static async Task <R.ResponsesQuery <TResponse> > ToOptimizedCachedAndSubscribedResult <TResponse>(this IRequest request, Q.QueriesQuery <TResponse> query, ICacheClient cache, ISubscriptionManager manager, Func <Task <R.ResponsesQuery <TResponse> > > factory)
        {
            var key    = query.GetCacheKey();
            var cached = cache.Get <R.ResponsesQuery <TResponse> >(key);

            if (cached == null)
            {
                cached = await factory().ConfigureAwait(false);

                cache.Add(key, cached);
            }
            if (!query.SubscriptionId.IsNullOrEmpty())
            {
                manager.SubscribeWith(cached, query, request.GetSessionId());
            }

            return(cached);
        }
        public static R.ResponsesQuery <TResponse> ToOptimizedCachedAndSubscribedResult <TResponse>(this IRequest request, Q.QueriesQuery <TResponse> query, ICacheClient cache, ISubscriptionManager manager, Func <R.ResponsesQuery <TResponse> > factory)
        {
            var result = request.ToOptimizedCachedResult(query, cache, () =>
            {
                var response = factory();
                return(response);
            });

            if (!query.SubscriptionId.IsNullOrEmpty())
            {
                manager.SubscribeWith(result, query, request.GetSessionId());
            }
            return(result);
        }
        public static async Task <R.ResponsesQuery <TResponse> > ToOptimizedCachedResult <TResponse>(this IRequest request, Q.QueriesQuery <TResponse> query, ICacheClient cache, Func <Task <R.ResponsesQuery <TResponse> > > factory)
        {
            var key    = query.GetCacheKey();
            var cached = cache.Get <R.ResponsesQuery <TResponse> >(key);

            if (cached == null)
            {
                cached = await factory().ConfigureAwait(false);

                cache.Add(key, cached);
            }

            return(cached);
        }
        public static R.ResponsesQuery <TResponse> ToOptimizedCachedResult <TResponse>(this IRequest request, Q.QueriesQuery <TResponse> query, ICacheClient cache, Func <R.ResponsesQuery <TResponse> > factory)
        {
            var key    = query.GetCacheKey();
            var cached = cache.GetOrCreate(key, factory);

            return(cached);
        }
        public static R.ResponsesQuery <TResponse> RequestQuery <TResponse>(this IMessage message, Q.QueriesQuery <TResponse> query = null) where TResponse : class
        {
            if (message == null || message is Reject)
            {
                var reject = (Reject)message;
                Logger.WarnFormat("Query was rejected - Message: {0}\n", reject.Message);
                if (reject != null)
                {
                    throw new QueryRejectedException(reject.Message);
                }
                throw new QueryRejectedException();
            }
            if (message is Error)
            {
                var error = (Error)message;
                Logger.WarnFormat("Query raised an error - Message: {0}", error.Message);
                throw new QueryRejectedException(error.Message);
            }

            var package = (IReply)message;

            if (package == null)
            {
                throw new QueryRejectedException($"Unexpected response type: {message.GetType().FullName}");
            }

            var payload = package.Payload.ConvertTo <TResponse>();

            return(new R.ResponsesQuery <TResponse>
            {
                Payload = payload,
                Etag = package.ETag,
                SubscriptionId = query?.SubscriptionId,
                SubscriptionTime = query?.SubscriptionTime,
            });
        }