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

            query.SubscriptionType = query.SubscriptionType ?? ChangeType.All;
            foreach (var record in dto.Records)
            {
                manager.Manage(record, key, query.SubscriptionId, query.SubscriptionType ?? ChangeType.All, TimeSpan.FromSeconds(query.SubscriptionTime ?? 3600), session);
            }
            manager.Manage <TResponse>(query, query.SubscriptionId, query.SubscriptionType ?? ChangeType.All, TimeSpan.FromSeconds(query.SubscriptionTime ?? 3600), session);
        }
        public static async Task <R.ResponsesPaged <TResponse> > RequestToElastic <T, TResponse>(this IMessageSession bus, Action <T> messageConstructor, Q.QueriesPaged <TResponse> query) where T : IPaged where TResponse : class
        {
            var options = new SendOptions();

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

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

            return(response.RequestPaged(query));
        }
        public static R.ResponsesPaged <TResponse> RequestPaged <TResponse>(this IMessage message, Q.QueriesPaged <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 = (IPagedReply)message;

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

            var records = package.Records.Select(x => x.ConvertTo <TResponse>());

            return(new R.ResponsesPaged <TResponse>
            {
                Records = records,
                Total = package.Total,
                ElapsedMs = package.ElapsedMs,
                SubscriptionId = query?.SubscriptionId,
                SubscriptionTime = query?.SubscriptionTime,
            });
        }
        public static async Task <R.ResponsesPaged <TResponse> > ToOptimizedCachedAndSubscribedPagedResult <TResponse>(this IRequest request, Q.QueriesPaged <TResponse> query, ICacheClient cache, ISubscriptionManager manager, Func <Task <R.ResponsesPaged <TResponse> > > factory)
        {
            var key    = query.GetCacheKey();
            var cached = cache.Get <R.ResponsesPaged <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.ResponsesPaged <TResponse> ToOptimizedCachedAndSubscribedPagedResult <TResponse>(this IRequest request, Q.QueriesPaged <TResponse> query, ICacheClient cache, ISubscriptionManager manager, Func <R.ResponsesPaged <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.ResponsesPaged <TResponse> > ToOptimizedCachedResult <TResponse>(this IRequest request, Q.QueriesPaged <TResponse> query, ICacheClient cache, Func <Task <R.ResponsesPaged <TResponse> > > factory)
        {
            var key    = query.GetCacheKey();
            var cached = cache.Get <R.ResponsesPaged <TResponse> >(key);

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

                cache.Add(key, cached);
            }

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

            return(cached);
        }