c# – 如何在Azure存储中使用EntityResolver?

前端之家收集整理的这篇文章主要介绍了c# – 如何在Azure存储中使用EntityResolver?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写下面的代码来从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;
    }
}

解决方法

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

阅读完整的文章,以更好地了解解析器的交易.

原文链接:https://www.f2er.com/csharp/99973.html

猜你在找的C#相关文章