Search code examples
azure-service-fabricservice-fabric-stateful

How to upload data in IReliable dictionary from IEnumerable<T>


I am retrieving data from Azure Table Storage and storing them in IEnumerable, but I don't know how to convert this IEnumerable<T> into IReliableDictionary<string, Store>, so that I can save this store data into state manager of stateful service.

var storesRepo = await StateManager.GetOrAddAsync<IReliableDictionary<long, Store>>(""); 

So how can I insert this IEnumerable<Store> into storesRepo?

Store Class:

    public class Store : TableEntity
    {
    public string Name { get; set; }
    public double Longitude { get; set; }
    public double Latitude { get; set; }
    public string Address { get; set; }
    public double Distance { get; set; }
    public string Email { get; set; }
    public string City { get; set; }
    }


    ReliableDictionary<string, Store>

where key is store name.


Solution

    • Create a dictionary, enumerate over all stores in the collection, add them one by one, inside a transaction.
    • Make sure the set of stores doesn't get too large to avoid a long running transactions & locks.
    • Make sure to think about partitioning first
    • Add retry support.
    • Apply best practices from here.

    meta code below:

    var storesRepo = await StateManager.GetOrAddAsync<IReliableDictionary<string, Store>>("someName");    
    IEnumerable<Store> stores = await GetFromTableStorage();
    using (ITransaction tx = base.StateManager.CreateTransaction()) 
    {
       foreach(var store in stores)
       {
          await storesRepo.AddAsync(tx, store.Name, store, cancellationToken);
       }
       await tx.CommitAsync();
    }