简体   繁体   English

Azure:如何保存IDictionary <String> 在TableStorage上?

[英]Azure: How to save IDictionary<String> at TableStorage?

I have a class with an IDictionary. 我有一堂IDictionary。 The size of this object isn't fix to any particular size. 此对象的大小不固定为任何特定大小。 The idea with this is to have a Dynamic Schema of my object. 这样做的想法是拥有我的对象的动态模式 I want to store this object in the TableStorage. 我想将此对象存储在TableStorage中。 How can I do this? 我怎样才能做到这一点? And how can I retrieve this information from store after this inside my object with the IDictionary again?? 在使用IDictionary在我的对象内部之后,又如何从存储中检索此信息呢?

Thanks!! 谢谢!!

DynamicTableEntity takes IDictionary<string,EntityProperty> as a param. DynamicTableEntityIDictionary<string,EntityProperty>作为参数。

This code executes in LinqPad : 此代码在LinqPad执行:

void Main()
{
var account = "";
var key = "";
var tableName = "";

var storageAccount = GetStorageAccount(account, key);
var cloudTableClient = storageAccount.CreateCloudTableClient();
var table = cloudTableClient.GetTableReference(tableName);

var partitionKey = "pk";
var rowKey = "rk";

//create the entity
var entity = new DynamicTableEntity(partitionKey, rowKey, "*", 
    new Dictionary<string,EntityProperty>{
            {"Prop1", new EntityProperty("stringVal")},
            {"Prop2", new EntityProperty(DateTimeOffset.UtcNow)},
        });

//save the entity
table.Execute(TableOperation.InsertOrReplace(entity));

//retrieve the entity
table.Execute(TableOperation.Retrieve(partitionKey,rowKey)).Result.Dump();
}

static CloudStorageAccount GetStorageAccount(string accountName, string key, bool useHttps = true)
{
var storageCredentials = new StorageCredentials(accountName, key);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps: useHttps);
return storageAccount;
}

You can use the Read and Write events from the TableStorageContext to perform this. 您可以使用TableStorageContext中的Read和Write事件来执行此操作。 You need to store the IDictionary content in other Field or as a BLOB file. 您需要将IDictionary内容存储在其他字段中或作为BLOB文件存储。 In the Read event you create the IDictionary from the given field or retrive directly from the BLOB file. 在Read事件中,您可以从给定的字段创建IDictionary或直接从BLOB文件中检索。 In the Write event you store the IDictionary to the field or as a BLOB file. 在Write事件中,您将IDictionary存储到字段中或存储为BLOB文件。

Important: The Write event occurs after the Entity traslation step then if you choose the field way you may need to write your changes directly to the XML serialization of the entity. 重要提示:Write事件在实体转换步骤之后发生,然后,如果您选择字段方式,则可能需要将更改直接写入实体的XML序列化。

THIS sounds hard but is very useful in many scenarios 这听起来很难,但在许多情况下非常有用

Dictionaries by default are not serializable. 默认情况下,字典不可序列化。 An object must be serializable in order to be saved to TableStorage. 对象必须可序列化才能保存到TableStorage。 I suggest you use List or Array type of objects or write your own serializer for Dictionary if List or Arrays are not good enough for you. 我建议您使用List或Array类型的对象,或者如果List或Arrays对您而言不够好,请为Dictionary编写自己的序列化程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM