加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:55:36 所属栏目:百科 来源:网络整理
导读:我正在编写下面的代码来从Azure表中检索所有实体.但我有点坚持通过实体解析员代表.我在 MSDN上找不到太多参考. 有人可以指出,如何在下面的代码中使用EntityResover? public class ATSHelperT where T : ITableEntity,new(){ CloudStorageAccount storageAcc
我正在编写下面的代码来从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);

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读