سؤال

We're switching over from Lokad to CloudFx to handle putting things in/out of table storage.

With Lokad, we had 3 entities:

1) Object 1, which added partition key and row key to
2) Object 2 which had a bunch of properties as well as an instance of 
3) Object 3, which had its own set of properties

As far as I can tell, the only way for CloudFx to input that info into table storage is to flatten that whole thing out with one massive object that has all the properties of the previous three objects. With Lokad, we could just useSkinny=true.

Thoughts?

هل كانت مفيدة؟

المحلول

This is the method we ended up implementing. Short version: serialize object 2 and stuff it into the Value property of Object 1, then put in table storage.

Object 1:

public class CloudEntity<T>
{

    public CloudEntity()
    {
        Timestamp = DateTime.UtcNow;
    }

    public string RowKey { get; set; }

    public string PartitionKey { get; set; }

    public DateTime Timestamp { get; set; }

    public T Value { get; set; }
}

Object 2:

public class Store
{
    public string StoreId { get; set; }
    public string StoreName { get; set; }
    public string StoreType { get; set; }
    public Address MailingAddress { get; set; }
    public PhoneNumber TelephoneNumber { get; set; }
    public StoreHours StoreHours { get; set; }
}

Object 3 can be whatever...the address in this case perhaps...it all gets serialized.

So in code you can get the table as follows (more than 1 way to do this):

var tableStorage = new ReliableCloudTableStorage(connection string you're using);

Then let's say you have an instance of store() you want to put in table storage:

var myStore = new Store(
    {
        storeId = "9832",
        storeName = "Headquarters"
        ...
    });

You can do so in the following way:

    var cloudEntity = new CloudEntity<string>
        {
            PartitionKey = whatever you want your partition key to be,
            RowKey = whatever you want your row key to be,
            Value = JsonConvert.SerializeObject(myStore) // THIS IS THE MAGIC
        };

    tableStorage.Add<CloudEntity<string>>(name of table in table storage, cloudEntity);

The entity put in table storage will have all the properties of the CloudEntity class (row key, partition key, etc) and in the "Value" column there will be the json of the object you wanted to store. It's easily readable through Azure Storage Explorer which is nice too.

To get the object back out, use something like this:

var cloudEntity = tableStorage.Get<CloudEntity<string>>(name of your table, partitionKey: whatever the partition key is);

Then you can deserialize the "Value" field of those into the object you're expecting:

var myStore = JsonConvert.DeserializeObject<Store>(cloudEntity.Value);

If you're getting a bunch back, just make myStore a list and loop through the cloudEntities, deserializing and adding each to your list.

Hope this helps!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top