2014-10-01 55 views
4

我正在编写代码以从Azure表中检索所有实体。但我有点卡在传递实体解析器委托。 MSDN我找不到多少参考。如何在Azure存储中使用EntityResolver?

有人可以指出,如何在下面的代码中使用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; 
    } 
} 

回答

7

Here is a nice article描述表格存储在深处。它还包含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); 

阅读完整的文章,以更好地理解与解析器的交易。

相关问题