Windows Azure Table storage 之 动态Table类 DynamicTableEntity详解程序员

在一般情况下,当我们在.net中使用Azure table storage的时候都会为该表建立一个TableEntity的派生类,如下所示。

public class CustomerEntity : TableEntity 
{ 
    public CustomerEntity(string lastName, string firstName) 
 
    { 
        this.PartitionKey = lastName; 
 
        this.RowKey = firstName; 
    } 
    public CustomerEntity() { } 
    public string Email { get; set; } 
    public string PhoneNumber { get; set; } 
} 
 
  

  

这是建立在我们知道表结构的基础上的。

但在某些时候我们无法知道Table storage里的表的结构的时候这种办法就无法奏效了。

例如我们需要写一个软件帮助用户管理他们的table storage, 这时我们无法知道用户的表结构所以不能够使用上面这种结构。

这时使用Azure storage提供的另外一个类能够很好的解决我们的问题那就是DynamicTableEntity类,该类包含在Microsoft.WindowsAzure.Storage.Table之中。如TableEntity一样提供了Table 必须包含的几种属性,同时提供了一个

IDictionary<string, EntityProperty> Properties 属性。

该属性能够将除开Azure table storage必须包含的几个属性之外的其他属性以key value的形式包含在这个Properties之中。

这样我们就能够无需声明特定的Entity类也能够用这个类来处理相关的操作。

以下代码是通过使用这个类来获取一个表中的所有信息,从而将它展示在Gridview中。

protected void btnQuery_Click(object sender, EventArgs e) 
       { 
           CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); 
           CloudTableClient client = account.CreateCloudTableClient(); 
           CloudTable table = client.GetTableReference("DynamicEntityTable"); 
  
           var query = new TableQuery(); 
           var tableResults = table.ExecuteQuery(query); 
  
           DataTable propertiesTable = new DataTable("DynamicEntity"); 
  
           //A Dynamic Entity Table must have the properties in ITableEntity. 
           DataColumn partitionKeyColumn = new DataColumn(); 
           partitionKeyColumn.DataType = Type.GetType("System.String"); 
           partitionKeyColumn.ColumnName = "Partition Key"; 
           propertiesTable.Columns.Add(partitionKeyColumn); 
  
           DataColumn rowKeyColumn = new DataColumn(); 
           rowKeyColumn.DataType = Type.GetType("System.String"); 
           rowKeyColumn.ColumnName = "Row Key"; 
           propertiesTable.Columns.Add(rowKeyColumn); 
  
           //Dynamic Entity Table have a property called Proerties which include other table column as KeyValue pair. 
           foreach (var entity in tableResults) 
           { 
               DataRow row; 
               row = propertiesTable.NewRow(); 
               row["Partition Key"] = entity.PartitionKey; 
               row["Row Key"] = entity.RowKey; 
               if (entity.Properties != null) 
               { 
                   foreach (var kvp in entity.Properties) 
                   { 
                       if (!propertiesTable.Columns.Contains(kvp.Key)) 
                       { 
                           DataColumn column = new DataColumn(); 
                           column.ColumnName = kvp.Key; 
                           column.DataType = Type.GetType("System." + kvp.Value.PropertyType.ToString()); 
                           propertiesTable.Columns.Add(column); 
                       } 
  
                       switch (kvp.Value.PropertyType) 
                       { 
                           case EdmType.Binary: 
                               row[kvp.Key] = kvp.Value.BinaryValue; 
                               break; 
                           case EdmType.Boolean: 
                               row[kvp.Key] = kvp.Value.BooleanValue; 
                               break; 
                           case EdmType.DateTime: 
                               row[kvp.Key] = kvp.Value.DateTimeOffsetValue; 
                               break; 
                           case EdmType.Double: 
                               row[kvp.Key] = kvp.Value.DoubleValue; 
                               break; 
                           case EdmType.Guid: 
                               row[kvp.Key] = kvp.Value.GuidValue; 
                               break; 
                           case EdmType.Int32: 
                               row[kvp.Key] = kvp.Value.Int32Value; 
                               break; 
                           case EdmType.Int64: 
                               row[kvp.Key] = kvp.Value.Int64Value; 
                               break; 
                           case EdmType.String: 
                               row[kvp.Key] = kvp.Value.StringValue; 
                               break; 
                           default: 
                               break; 
                       } 
  
                   } 
               } 
               propertiesTable.Rows.Add(row); 
           } 
           gvwAzureTable.DataSource = propertiesTable; 
           gvwAzureTable.DataBind(); 
       }

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/6342.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论