【问题标题】:How to use EntityResolver with Azure Storage?如何将 EntityResolver 与 Azure 存储一起使用?
【发布时间】:2014-11-26 18:45:26
【问题描述】:

我正在编写以下代码以从 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;
    }
}

【问题讨论】:

    标签: c# azure azure-storage


    【解决方案1】:

    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);
    

    阅读全文以更好地了解与解析器的交易。

    【讨论】:

    猜你喜欢
    • 2013-11-06
    • 2013-03-10
    • 2018-09-07
    • 2016-05-15
    • 1970-01-01
    • 2016-02-02
    • 2021-05-14
    • 2014-07-16
    • 1970-01-01
    相关资源
    最近更新 更多