LoadAllContextsAsync <TContext>(long botId, ChatType chatType, ISegmentedQueryContinuationToken token = null)
            where TContext : class, new()
        {
            var continuationToken = (AzureStorageServiceContinuationToken)token;

            var table = await GetTable(options.MainTableName);

            var query = BotUserOrChatContext.GetAllForBot(botId, chatType);

            var segment = await table.ExecuteQuerySegmentedAsync(query, continuationToken?.Token);

            var contexts = segment.Results.Select(
                x => string.IsNullOrEmpty(x.Value)
                    ? null
                    : JsonConvert.DeserializeObject <TContext>(x.Value, EntityPropertyExtensions.JsonSettings))
                           .ToList();

            var newToken =
                segment.ContinuationToken == null
                ? null
                : new AzureStorageServiceContinuationToken {
                Token = segment.ContinuationToken
            };

            return(Tuple.Create(contexts, (ISegmentedQueryContinuationToken)newToken));
        }
        protected async Task SaveContextAsync <TContext>(long botId, long userOrChatId, ChatType chatType, TContext userContext)
            where TContext : class, new()
        {
            var value  = JsonConvert.SerializeObject(userContext, EntityPropertyExtensions.JsonSettings);
            var entity = new BotUserOrChatContext(botId, userOrChatId, chatType, value);

            var table = await GetTable(options.MainTableName);

            await table.ExecuteAsync(TableOperation.InsertOrReplace(entity));
        }
        public async Task <TContext> LoadContextAsync <TContext>(long botId, long userOrChatId)
            where TContext : class, new()
        {
            var table = await GetTable(options.MainTableName);

            var key = BotUserOrChatContext.GetKey(botId, userOrChatId);
            var obj = await table.ExecuteAsync(TableOperation.Retrieve <BotUserOrChatContext>(key.PartitionKey, key.RowKey));

            if (obj.Result == null)
            {
                return(null);
            }

            var context = (BotUserOrChatContext)obj.Result;

            if (string.IsNullOrEmpty(context.Value))
            {
                return(null);
            }

            return(JsonConvert.DeserializeObject <TContext>(context.Value, EntityPropertyExtensions.JsonSettings));
        }