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

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

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...