Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将通用对象写入Azure存储的示例?_C#_Azure Storage - Fatal编程技术网

C# 将通用对象写入Azure存储的示例?

C# 将通用对象写入Azure存储的示例?,c#,azure-storage,C#,Azure Storage,假设我有多个从TableEntity继承的实体 我想使用一个通用类,比如Cachemanager,并将其写入azure存储 下面的链接提到TableEntityAdapter 我正在寻找一个代码示例。 谢谢,Peter,我编写了TableEntityAdapter类,所以我将尝试提供使用示例,尽管我看到一个月前有人问这个问题,希望它仍然有用 首先看看SDK的单元测试是如何对其进行测试的: 搜索TableEntityAdapter以查找相关测试。。请注意,单元测试当然不会写入实际的表存储服务,

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

我想使用一个通用类,比如Cachemanager,并将其写入azure存储

下面的链接提到TableEntityAdapter

我正在寻找一个代码示例。
谢谢,Peter,我编写了TableEntityAdapter类,所以我将尝试提供使用示例,尽管我看到一个月前有人问这个问题,希望它仍然有用

首先看看SDK的单元测试是如何对其进行测试的:

搜索TableEntityAdapter以查找相关测试。。请注意,单元测试当然不会写入实际的表存储服务,它们模拟当您根据api调用进行读写时会发生的情况,但它们应该会给您一个好主意

原始ShapeEntity对象甚至不需要实现ITableEntity接口。它还可以包含复杂的嵌套属性
TableEntityAdapter
class支持这些场景

下面是
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. 
//1。ShapeEntity是我们要从表存储中写入和读取的自定义.Net对象。
ShapeEntity ShapeEntity=新的ShapeEntity(Guid.NewGuid().ToString(),Guid.NewGuid().ToString(),“square”,4,4);
OperationContext OperationContext=新的OperationContext();
// 2. 实例化TableEntityAdapter对象,将自定义对象传递给其构造函数。在内部,此实例将保留对自定义ShapeEntity对象的引用。
TableEntityAdapter writeToTableStorage=新的TableEntityAdapter(shapeEntity、partitionKey、rowKey);
// 3. 现在,您可以将writeToTableStorage对象写入表存储,就像从ITableEntity接口继承的任何其他对象一样。TableAdapter泛型类处理自定义.Net对象和表存储sdk/服务之间的锅炉板代码和握手。
要从表存储中读取自定义对象,请执行以下操作:
// 1. 使用指定的相同分区/行键从表存储中读回实体(请参见上面的步骤2)。您可以使用检索表操作,将T指定为TableEntityAdapter
// 2. 这将返回您在上述步骤3中写入表存储的TableEntityAdapter对象。
// 3. 要访问原始ShapeEntity对象,只需参考返回的TableEntityAdapter对象的OriginalEntity属性。
原始ShapeEntity对象甚至不需要实现ITableEntity接口。它还可以包含复杂的嵌套属性。TableEntityAdapter类支持这些场景

注意:
TableEntityAdapter
api不支持具有集合类型属性的对象,即
列表
数组
IEnumerable
ICollection

如果您的对象包含这些类型的属性(直接在根目录下或它们的对象图中的某个地方),那么您应该考虑使用我所写的原始NuGuT包,它支持集合类型属性类型。

ObjectFlatterRecomposer
Api 2.0版,支持集合、可枚举类型属性:

最近上传的.Net核心版本: