【问题标题】:Using POCOs when persisting to Azure Table Storage持久化到 Azure 表存储时使用 POCO
【发布时间】:2016-02-27 23:42:36
【问题描述】:

我计划在我的 ASP.NET 5 (MVC 6) 应用程序中使用 Azure 表存储,并添加了 WindowsAzure.Storage NuGet 包,但是当我注意到我的所有实体模型都需要继承自Microsoft.WindowsAzure.Storage.Table.TableEntity。现在我认为最好的解决方案是拥有 2 组实体并在我的主要域对象和用于持久存储到表存储的实体对象之间创建映射。我不想将WindowsAzure.Storage 包添加到我的所有项目中。

已弃用的 azure-sdk-for-net 曾一度支持 POCO,但我在当前的 WindowsAzure.Storage 中看不到这一点。

这里的最佳做法是什么?

【问题讨论】:

  • 对于表存储需要类从 TableEntity 继承,所以绝对需要 storage namespace 。您可以将表实体视为 edmx,并且需要具有映射器类来将表实体转换为纯 POCO 模型类。虽然你可以使用继承自 TableEntity 的模型类
  • 是的,正是梅什!但这是我想要避免的。当然,我需要在我正在实现持久性机制的项目中引用 WindowsAzure.Storage,但我有一个不同的项目/程序集来保存我的模型对象。我希望这些模型对象是干净的 POCO,而不是以任何方式与 TableEntity 或任何其他库绑定。
  • 同意 OP。我认为 "stores structured NoSQL data in the cloud" 的东西实际上需要 结构化数据

标签: c# asp.net azure asp.net-core azure-table-storage


【解决方案1】:

您没有提供有关您尝试写入 Azure 表存储的实体类型的详细信息,但是如果您的实体包含嵌套的复杂属性,并且您想要编写包括复杂嵌套属性的整个对象图(它们本身可能包含嵌套属性),这些建议的解决方案都不起作用。

我遇到了类似的问题,并实现了一个通用的对象扁平化器/重构器 API,它将你的复杂实体扁平化为扁平的 EntityProperty 字典,并以 DynamicTableEntity 的形式使它们可写入表存储。

然后,相同的 API 将从 DynamicTableEntityEntityProperty 字典中重新组合整个复杂对象。

看看:https://www.nuget.org/packages/ObjectFlattenerRecomposer/

我正在与 Azure 团队合作将此 API 集成到 Azure 存储 SDK。您可以在此处查看拉取请求和代码:

https://github.com/Azure/azure-storage-net/pull/337/commits

用法:

//Flatten object of type Order) and convert it to EntityProperty Dictionary
 Dictionary<string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(order);

// Create a DynamicTableEntity and set its PK and RK
DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(partitionKey, rowKey);
dynamicTableEntity.Properties = flattenedProperties;

// Write the DynamicTableEntity 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.
 Order order = EntityPropertyConverter.ConvertBack<Order>(entity.Properties);

就是这样:)

nuget 包的最新版本还支持 IEnumerable、ICollection 等类型属性。

该软件包的 .Net Core 版本在这里: https://www.nuget.org/packages/ObjectFlattenerRecomposer.Core/

CosmosDb Table api 版本包在这里: https://www.nuget.org/packages/ObjectFlattenerRecomposer.CosmosDb.Table.Core/

【讨论】:

  • 指向 MSDN Flatten 和 ConvertBack 的链接已损坏。新链接:docs.microsoft.com/en-us/dotnet/api/…
  • 我从答案中删除了过时的链接。合并到 Azure 存储 SDK 中的是我的 nuget 包的旧版本。我在 nuget 中添加了指向 .core 和 cosmosdb.table api 版本的链接。
【解决方案2】:

您可以避免从 TableEntity 继承,但要这样做,您最终会编写一些映射代码。在实际将与表存储交互的代码中,您可以使用DynamicTableEntity 将更多原始表数据映射到您的对象,以完全控制序列化。

有几篇文章可以帮助你:

如果您查看第二篇文章,它会显示在 Azure 表存储中保存和更新的特定 POCO 对象的代码是什么样的。第三篇文章扩展了第一篇的工作,包括 ETag 支持。

【讨论】:

    【解决方案3】:

    我制作的库就是这样做的:

    TableStorage.Abstractions.TableEntityConverters 将 POCO 转换为 DynamicTableEntity,反之亦然。它具有让您指定分区键、行键和忽略字段的功能。

    TableStorage.Abstractions.POCO 建立在此之上和一个表存储存储库库 (TableStorage.Abstractions)。结合起来,它为您提供了一种使用 POCO 对表存储进行 CRUD 的非常简单的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 2018-12-28
      相关资源
      最近更新 更多