CloudFx相当于SkinnyEntity

时间:2013-09-09 14:49:48

标签: azure azure-storage lokad-cqrs cloudfx

我们正在从Lokad切换到CloudFx以处理将内容放入/放出表存储。

有了Lokad,我们有3个实体:

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

据我所知,CloudFx将该信息输入到表存储中的唯一方法是用一个具有前三个对象的所有属性的大型对象来展平整个事物。有了Lokad,我们可以使用Skinny = true。

思想?

1 个答案:

答案 0 :(得分:0)

这是我们最终实施的方法。简短版本:序列化对象2并将其填充到对象1的Value属性中,然后放入表存储。

对象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; }
}

对象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; }
}

对象3可以是任何......在这种情况下的地址可能......它都被序列化了。

因此,在代码中,您可以按如下方式获取表格(超过1种方法):

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

然后假设您有一个store()实例要放入表存储:

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

您可以通过以下方式执行此操作:

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

放入表存储的实体将具有CloudEntity类的所有属性(行键,分区键等),并且在“值”列中将存在您要存储的对象的json。它可以通过Azure Storage Explorer轻松读取,这也很不错。

要将对象取出,请使用以下内容:

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

然后你可以将那些“Value”字段反序列化为你期望的对象:

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

如果你回来了,只需让myStore成为一个列表并循环遍历cloudEntities,反序列化并将每个列表添加到列表中。

希望这有帮助!

相关问题