本文介绍了示例将通用对象写入Azure存储吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有多个继承自TableEntity的实体.

Suppose i have multiple entities that inherit from TableEntity.

我想使用Cachemanager之类的通用类,并将不同的T写入天蓝色存储

i want to use a generic class , like Cachemanager , and write to azure storage different T

下面的链接提到了TableEntityAdapter

This link below mentions TableEntityAdapter

https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.windowsazure.storage.table.tableentityadapter-1.-ctor?view=azure-dotnet

我正在寻找代码示例.谢谢彼得

I am looking for a Code examples. Thanks,Peter

推荐答案

我写了TableEntityAdapter类,所以我将尝试提供用法示例,尽管我一个月前问到了问题,希望它仍然有用.

I wrote TableEntityAdapter class so I will try to provide usage example, though I see question is asked a month ago, hopefully it is still usefull.

首先看一下如何通过SDK的单元测试对它进行测试: https://github.com/Azure/azure-storage-net/blob/master/Test/ClassLibraryCommon/Table/TableOperationUnitTests.cs

Firstly have a look at how it is tested by the unit tests by the SDK: https://github.com/Azure/azure-storage-net/blob/master/Test/ClassLibraryCommon/Table/TableOperationUnitTests.cs

搜索TableEntityAdapter以找到相关的测试..请注意,单元测试当然不会写入实际的表存储服务,它们会模拟您在使用api调用进行读取和写入时会发生的情况,但是它们应该给您带来良好的效果.想法.

search for TableEntityAdapter to find relevant tests.. Note that unit tests of course do not write to actual table Storage service, they simulate what would happen when you read and write in terms of api calls, but they should give you a good idea.

您原来的ShapeEntity对象甚至不需要实现ITableEntity接口.它还可以包含复杂的嵌套属性. TableEntityAdapter类支持这些情况.

Your original ShapeEntity object does not even need to implement ITableEntity interface. It also can contain complex nested properties. TableEntityAdapter class supports these scenarios.

下面是TableEntityAdapter的简化示例代码:

Below is a simplified sample code for TableEntityAdapter:

要将自定义.Net对象写入表存储:

To write your custom .Net object to Table Storage:

      // 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类支持这些方案.

Your original ShapeEntity object does not even need to implement ITableEntity interface. It also can contain complex nested properties. TableEntityAdapter class supports these scenarios.

注意: TableEntityAdapter api不支持具有集合类型属性(即)的对象. ListArrayIEnumerableICollection等.

Note: TableEntityAdapter api does not support objects with collection type properties ie. List, Array, IEnumerable, ICollection etc..

如果您的对象包含这些类型的属性(直接在对象图的根目录下或某个位置),则应考虑使用我编写的原始Nuget包,该包也支持集合类型属性类型.

If your objects contain these types of properties (directly under the root or somewhere in their object graph) then you should consider using the original Nuget package that I wrote which supports collection type property types as well.

ObjectFlattenerRecomposer Api版本2.0,该版本支持Collection,Enumerable类型属性: https://www.nuget.org/packages/ObjectFlattenerRecomposer/

ObjectFlattenerRecomposer Api version 2.0 that supports Collection, Enumerable type properties:https://www.nuget.org/packages/ObjectFlattenerRecomposer/

,最近上传的.Net Core版本: https://www.nuget.org/packages/ObjectFlattenerRecomposer.Core/1.0 .0/

and recently uploaded .Net Core version:https://www.nuget.org/packages/ObjectFlattenerRecomposer.Core/1.0.0/

这篇关于示例将通用对象写入Azure存储吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 02:48