Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use EntityResolver with Azure Storage?

I am writing below code to retrieve all entities from Azure table. But I am kind of stuck up in passing entity resolver delegate. I could not find much reference on MSDN.

Can some one please point out, how to use EntityResover in below code?

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;
    }
}
like image 445
Abhijeet Avatar asked Oct 01 '14 20:10

Abhijeet


People also ask

How do I connect to Azure table storage?

Creating a connectionProvide Azure Storage account name (or table endpoint) and Access Key to access your Azure Table Storage. Provide Azure Storage account name (or table endpoint) and Access Key to access your Azure Table Storage. Use Azure Active Directory to access your Azure Table storage.

Is Azure table storage key value pair?

Azure Table Storage is a highly scalable, Key-Value pair NoSQL storage system.

What is RowKey in Azure Table Storage?

The row key is a unique identifier for an entity within a given partition. Together the PartitionKey and RowKey uniquely identify every entity within a table. The row key is a string value that may be up to 1 KiB in size. You must include the RowKey property in every insert, update, and delete operation.


1 Answers

Here is a nice article describing the Table Storage in deep. It also includes couple of samples for EntityResolver.

Ideal would be to have one Generic Resolver, that produces the desired result. Then you can include it in your call. I will just quote here one example from the provided article:

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

Read the full article to better understand the deal with resolvers.

like image 86
astaykov Avatar answered Oct 06 '22 07:10

astaykov