假设我有多个从TableEntity继承的实体。
我想使用泛型类,如Cachemanager,并写入不同的天文存储T
以下链接提及TableEntityAdapter
我正在寻找代码示例。 谢谢,彼得
答案 0 :(得分:4)
我编写了TableEntityAdapter类,所以我会尝试提供一些用法示例,虽然我看到一个月前问的问题,希望它仍然有用。
首先看看SDK如何通过单元测试进行测试: https://github.com/Azure/azure-storage-net/blob/master/Test/ClassLibraryCommon/Table/TableOperationUnitTests.cs
搜索TableEntityAdapter以查找相关测试..请注意,单元测试当然不会写入实际的表存储服务,它们会模拟在读取和写入api调用时会发生什么,但是它们应该给你一个好处想法。
您的原始ShapeEntity对象甚至不需要实现ITableEntity接口。它还可以包含复杂的嵌套属性。 TableEntityAdapter
类支持这些方案。
以下是TableEntityAdapter
的简化示例代码:
将自定义.Net对象写入表存储:
// 1. ShapeEntity is the custom .Net object that we want to write and read from Table Storage.
ShapeEntity shapeEntity = new ShapeEntity(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), "square", 4, 4);
OperationContext operationContext = new OperationContext();
// 2. Instantiate a TableEntityAdapter object, passing in the custom object to its constructor. Internally this instance will keep a reference to our custom ShapeEntity object.
TableEntityAdapter<ShapeEntity> writeToTableStorage = new TableEntityAdapter<ShapeEntity>(shapeEntity, partitionKey, rowKey);
// 3. Now you can write the writeToTableStorage object to Table Storage just like any other object which inherits from ITableEntity interface. The TableAdapter generic class handles the boiler plate code and hand shaking between your custom .Net object and table storage sdk / service.
To read your custom object from Table Storage:
// 1. Read your entity back from table storage using the same partition / row key you specified (see step 2 above). You can use the Retrieve<T> table operation, specify T as TableEntityAdapter<ShapeEntity>
// 2. This should return you the TableEntityAdapter<ShapeEntity> object that you wrote to TableStorage at step 3 above.
// 3. To access the original ShapeEntity object, just refer to the OriginalEntity property of the returned TableEntityAdapter<ShapeEntity> object.
您的原始ShapeEntity对象甚至不需要实现ITableEntity接口。它还可以包含复杂的嵌套属性。 TableEntityAdapter类支持这些方案。
注意: TableEntityAdapter
api不支持具有集合类型属性的对象,即。 List
,Array
,IEnumerable
,ICollection
等。
如果您的对象包含这些类型的属性(直接位于根目录或其对象图中的某个位置),那么您应该考虑使用我编写的原始Nuget包,它也支持集合类型属性类型。
ObjectFlattenerRecomposer
Api版本2.0,支持Collection,Enumerable类型属性:
https://www.nuget.org/packages/ObjectFlattenerRecomposer/