我目前正在深入研究适用于 .NET 的 DynamoDB AWS SDK,特别是修改
AsyncSearch<T>
类来执行表查询。目标是将分页合并到我的应用程序中,根据 AWS 文档,分页令牌驻留在底层搜索对象中。该对象可通过 AsyncSearch<T>
类的 DocumentSearch 属性访问,并通过 GetNextSetAsync() 方法进行设置,如本 documentation 中所示。
您可以在此处找到 AsyncSearch 类文档。
但是,有一个问题 - 我正在努力解决 DocumentSearch 属性是私有的这一事实,这使得直接访问分页标记变得很棘手。
这是我的代码的一瞥:
public interface IAsyncSearch<T>
{
bool IsDone { get; }
string PaginationToken { get; }
Dictionary<string, AttributeValue> NextKey { get; }
Task<List<T>> GetRemainingAsync(CancellationToken token = default);
Task<List<T>> GetNextSetAsync(CancellationToken token = default);
}
public class CustomAsyncSearch<T> : IAsyncSearch<T>
{
private readonly AsyncSearch<T> _search;
public CustomAsyncSearch(AsyncSearch<T> search)
{
_search = search;
}
async Task<List<T>> IAsyncSearch<T>.GetRemainingAsync(CancellationToken token)
{
return await _search.GetRemainingAsync(token);
}
async Task<List<T>> IAsyncSearch<T>.GetNextSetAsync(CancellationToken token)
{
return await _search.GetNextSetAsync(token);
}
public bool IsDone => _search.IsDone;
public string PaginationToken => _search.DocumentSearch.PaginationToken?.ToString();
public Dictionary<string, AttributeValue> NextKey => _search.DocumentSearch.NextKey;
}
我想知道是否有一种正确的方法可以从 AsyncSearch 类获取分页标记,而无需调整其内部结构。有关访问分页令牌或确保使用
AsyncSearch
类正确实现分页的任何见解或建议都将非常有帮助。
编辑: 这是适用于 .NET 的 AWS 开发工具包中的 AsyncSearch 类基类
namespace Amazon.DynamoDBv2.DataModel;
// Summary:
// A strongly-typed object for retrieving search results (Query or Scan) from DynamoDB.
//
//
// Type parameters:
// T:
public class AsyncSearch<T>
{
private Search DocumentSearch { get; set; }
private DynamoDBContext SourceContext { get; set; }
private DynamoDBFlatConfig Config { get; set; }
//
// Summary:
// Flag that, if true, indicates that the search is done
public bool IsDone => DocumentSearch.IsDone;
internal AsyncSearch(DynamoDBContext source, DynamoDBContext.ContextSearch contextSearch)
{
SourceContext = source;
DocumentSearch = contextSearch.Search;
Config = contextSearch.FlatConfig;
}
//
// Summary:
// Initiates the asynchronous execution to get the next set of results from DynamoDB.
// If there are more items in the Scan/Query, PaginationToken will be set and can
// be consumed in a new Scan/Query operation to resume retrieving items from this
// point.
//
// Parameters:
// cancellationToken:
// Token which can be used to cancel the task.
//
// Returns:
// A Task that can be used to poll or wait for results, or both. Results will include
// the next set of result items from DynamoDB.
public async Task<List<T>> GetNextSetAsync(CancellationToken cancellationToken = default(CancellationToken))
{
List<Document> documents = await DocumentSearch.GetNextSetHelperAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
return SourceContext.FromDocumentsHelper<T>(documents, Config).ToList();
}
//
// Summary:
// Initiates the asynchronous execution to get all the remaining results from DynamoDB.
//
//
// Parameters:
// cancellationToken:
// Token which can be used to cancel the task.
//
// Returns:
// A Task that can be used to poll or wait for results, or both. Results will include
// the remaining result items from DynamoDB.
public async Task<List<T>> GetRemainingAsync(CancellationToken cancellationToken = default(CancellationToken))
{
List<Document> documents = await DocumentSearch.GetRemainingHelperAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
return SourceContext.FromDocumentsHelper<T>(documents, Config).ToList();
}
}
尝试创建一个包装类。它应该继承自AsyncSearch。
public class CustomeAsyncSearch<T> : AsyncSearch<T>, IAsyncSearch<T>
{
public CustomAsyncSearch(AsyncSearch<T> search) : base(search)
{
}
public string GetPaginationToken()
{
return DocumentSearch.PaginationToken?.ToString();
}
}
现在您可以在代码中使用“CustomAsyncSearch”类并调用“GetPaginationToken”函数来获取分页令牌。此方法提供了一个简洁的解决方案。
希望这有效。