【问题标题】:Non-derived POCO and Azure Storage非派生 POCO 和 Azure 存储
【发布时间】:2013-03-28 15:12:48
【问题描述】:

是否可以为 Azure 表存储提供非派生 POCO?

换句话说,不是从TableEntity派生或实现ITableEntity的POCO?

必须拥有一个依赖于接口或基类的模型似乎是一种倒退,因为这会导致链中的引用向上泄漏 - 我无法在另一个层中设置模型而无需了解 Azure接口或基类的存储!

【问题讨论】:

    标签: azure-table-storage


    【解决方案1】:

    看看DynamicTableEntity (ctrl+f)。它可用于查询和插入实体。

    使用这种类型,您不会在您的领域模型中引入任何依赖项,但是您必须自己将 POCO 转换为 DynamicTableEntity - 如果您愿意将 POCO 标记为一个自定义接口并编写一个映射器(基本上你只需要一个属性字典 + 需要知道哪些是 Partition/RowKey)。

    不能只在 Azure 表存储中保存任何实体的原因是它需要知道哪个属性充当分区键,哪个属性充当行键。必须在“较低级别”上使用 DynamicTableEntity 的好处是您可以创建仅返回属性子集的查询,从而减少资源消耗。这对您的情况可能有益,也可能无益。

    【讨论】:

      【解决方案2】:

      看看我实现并放入 Nuget 的包: https://www.nuget.org/packages/ObjectFlattenerRecomposer/

      它也将被添加到 Azure Storage SDK 的下一个版本中: https://github.com/Azure/azure-storage-net/pull/337/files

      说明:

      提供将复杂对象展平到 EntityProperty 字典中的功能,以及从展平的属性字典中重构原始复杂对象的功能。一种用法是 API 允许将任何具有嵌套属性的复杂对象以扁平形式写入 Azure 表存储,这通常无法通过使用 Azure 存储客户端 SDK 实现。

      2.0 版现在还支持将 IEnumerable 类型属性(如列表、数组、字典)写入和读取到 Azure 表存储。

      博客:https://doguarslan.wordpress.com/2016/02/03/writing-complex-objects-to-azure-table-storage/

      用法:

      //Flatten object and convert it to EntityProperty Dictionary
      
      Dictionary<string, EntityProperty> flattenedProperties = ObjectFlattenerRecomposer.Flatten(complexObject);
      
      // Create a DynamicTableEntity and set its PK and RK
      DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(partitionKey, rowKey);
      
      dynamicTableEntity.Properties = flattenedProperties;
      
      // Write the DynammicTableEntity to Azure Table Storage using client SDK
      
      //Read the entity back from AzureTableStorage as DynamicTableEntity using the same PK and RK
      DynamicTableEntity entity = [Read from Azure using the PK and RK];
      
      //Convert the DynamicTableEntity back to original complex object.
      Imagine original complexObject was of type Order.
      
      Order order = ObjectFlattenerRecomposer.ConvertBack<Order>(entity.Properties);
      

      【讨论】:

      • 我想使用它,但 NuGet 包无法安装用于 .NET 4.5 目标项目...您可以更改包要求还是硬性要求?
      • 抱歉回复晚了。我用 .NET 4.5 重建了这个包,还添加了对写入和读取 Nullable、TimeSpan、DateTimeOffset、Enum 属性到表存储的支持。包在 Nuget:nuget.org/packages/ObjectFlattenerRecomposer/1.1.1
      【解决方案3】:

      首先,TableEntity 和 ITableEntity 还有一种替代方法,就是使用 DataServiceKey 属性来装饰你的类,如下例所示:

      [DataServiceKey("PartitionKey", "RowKey")]
      public class MyEntity
      {
          public string PartitionKey {get; set;}
          public string RowKey {get; set;}
          public DateTime Timestamp {get; set;}
          //other properties
      }
      

      但是,这并不能真正解决您不想将 Azure 实现泄漏到模型类中的问题。在这种情况下,我认为您可能想考虑使用像 LOKAD Fat Entities 这样的包装器实现。 Lokad 会将模型对象的序列化/反序列化处理成一个包装器,而该包装器又存储在表存储中。然而,Lokad 的一个缺点是您的对象在存储中变得不透明,您无法使用 Azure Storage Explorer 之类的方式浏览它们。

      【讨论】:

      • 这在当前版本的 SDK 中不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2016-02-27
      • 2019-01-31
      • 1970-01-01
      • 2012-01-02
      • 2016-05-21
      • 2015-10-26
      相关资源
      最近更新 更多