2014-10-01 106 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); 

閱讀完整的文章,以更好地理解與解析器的交易。