如何将EntityResolver与Azure存储一起使用
本文关键字:存储 一起 Azure EntityResolver | 更新日期: 2023-09-27 18:25:13
我正在编写以下代码,以从Azure表中检索所有实体。但我有点拘泥于传递实体解析器委托。我在MSDN上找不到太多参考资料。
有人可以指出,在下面的代码中如何使用EntityResover吗?
public class ATSHelper<T> where T : ITableEntity, new()
{
CloudStorageAccount storageAccount;
public ATSHelper(CloudStorageAccount storageAccount)
{
this.storageAccount = storageAccount;
}
public async Task<IEnumerable<T>> FetchAllEntities(string tableName)
{
List<T> allEntities = new List<T>();
CloudTable table = storageAccount.CreateCloudTableClient().GetTableReference(tableName);
TableContinuationToken contToken = new TableContinuationToken();
TableQuery query = new TableQuery();
CancellationToken cancelToken = new CancellationToken();
do
{
var qryResp = await table.ExecuteQuerySegmentedAsync<T>(query, ???? EntityResolver ???? ,contToken, cancelToken);
contToken = qryResp.ContinuationToken;
allEntities.AddRange(qryResp.Results);
}
while (contToken != null);
return allEntities;
}
}
这是一篇很好的文章,描述了deep中的表存储。它还包括EntityResolver的几个示例。
理想的情况是有一个通用解析器,可以产生所需的结果。然后您可以将其包含在通话中。我将在这里引用所提供文章中的一个例子:
EntityResolver<ShapeEntity> shapeResolver = (pk, rk, ts, props, etag) =>
{
ShapeEntity resolvedEntity = null;
string shapeType = props["ShapeType"].StringValue;
if (shapeType == "Rectangle") { resolvedEntity = new RectangleEntity(); }
else if (shapeType == "Ellipse") { resolvedEntity = new EllipseEntity(); }
else if (shapeType == "Line") { resolvedEntity = new LineEntity(); }
// Potentially throw here if an unknown shape is detected
resolvedEntity.PartitionKey = pk;
resolvedEntity.RowKey = rk;
resolvedEntity.Timestamp = ts;
resolvedEntity.ETag = etag;
resolvedEntity.ReadEntity(props, null);
return resolvedEntity;
};
currentSegment = await drawingTable.ExecuteQuerySegmentedAsync(drawingQuery, shapeResolver, currentSegment != null ? currentSegment.ContinuationToken : null);
阅读全文以更好地理解解决方案的处理。