简体   繁体   English

Azure 表存储 - 用于扩展 ITableEntity 的任何类型的通用下载

[英]Azure Table Storage - Generic download for any type extending ITableEntity

I have four custom data types, each of which extends ITableEntity , which is part of the WindowsAzure.Storage package.我有四种自定义数据类型,每一种都扩展ITableEntity ,它是WindowsAzure.Storage包的一部分。

Right now, I have four different methods for downloading data from the Azure Table storage.现在,我有四种不同的方法可以从 Azure 表存储下载数据。 Each follows this format:每个都遵循以下格式:

public List<MyCustomEntity> DownloadMyCustomEntities(string tableId)
{
    // Reference the CloudTable object
    CloudTable table = tableClient.GetTableReference(tableId);

    TableQuery<MyCustomEntity> query = new TableQuery<MyCustomEntity>();
    return new List<MyCustomEntity>(table.ExecuteQuery(query));
}

Instead of having one of these methods for each of my custom entity types, I am trying to create one shared function.我没有为每个自定义实体类型使用这些方法之一,而是尝试创建一个共享函数。 I'm hoping this is possible, as all of my custom types inherit from ITableEntity .我希望这是可能的,因为我所有的自定义类型都继承自ITableEntity

Here is what I tried:这是我尝试过的:

public List<TableEntity> DownloadAnyEntity(string tableId)
{
    // Reference the CloudTable object
    CloudTable table = tableClient.GetTableReference(tableId);

    TableQuery<TableEntity> query = new TableQuery<TableEntity>();
    return new List<TableEntity>(table.ExecuteQuery(query));
}

I've tried this with TableEntity and ITableEntity , but I keep getting errors.我已经用TableEntityITableEntity尝试过这个,但我不断收到错误。 For TableEntity , my error is that no cast exists to the type I actually need (when I call the DownloadAnyEntity method), whereas I feel it should be implicit since it is an extension of ITableEntity .对于TableEntity ,我的错误是我实际需要的类型不存在TableEntity (当我调用DownloadAnyEntity方法时),而我觉得它应该是隐式的,因为它是ITableEntity的扩展。

For ITableEntity , I get an error that ExecuteQuery input must be a non-abstract type with a public parameterless constructor.对于ITableEntity ,我收到一个错误,即ExecuteQuery输入必须是具有公共无参数构造函数的非抽象类型。 All of my four custom types have public parameterless constructors.我所有的四种自定义类型都有公共无参数构造函数。

I feel like the issue I'm seeing is more to do with not fully understanding inheritance, more so that it being Azure Table Storage specific.我觉得我看到的问题更多地与没有完全理解继承有关,更多的是它是特定于 Azure 表存储的。 Any pointers much appreciated.任何指针都非常感谢。

I've largely been following this documentation , but there is no example for a non type-specific entity download method.我一直在关注这个文档,但没有非特定于类型的实体下载方法的示例。

You could make the DownloadAnyEntity method generic with constraints on type parameter您可以使用对类型参数的约束使 DownloadAnyEntity 方法通用

public List<T> DownloadAnyEntity<T>(string tableId) where T: ITableEntity, new()
{
    // Reference the CloudTable object
    var tableRef = tableClient.GetTableReference(tableId);

    var query = new TableQuery<T>();
    return tableRef.ExecuteQuery(query).ToList();
}

This method can then be called for any type that inherits from the ITableEntity and has an public emtpy constructor ( The ExecuteQuery method demands an empty construtor in order to create the entities )然后可以为从 ITableEntity 继承并具有公共 emtpy 构造函数的任何类型调用此方法( ExecuteQuery 方法需要一个空构造函数以创建实体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有来自的装箱转换或类型参数转换 <T> 到&#39;Microsoft.WindowsAzure.Storage.Table.ITableEntity&#39; - There is no boxing conversion or type parameter conversion from <T> to 'Microsoft.WindowsAzure.Storage.Table.ITableEntity' 无法转换为microsoft.azure.cosmosDB.table.itableentity - cannot convert to microsoft.azure.cosmosDB.table.itableentity 如何将字符串转换为 Microsoft.WindowsAzure.Storage.Table.ITableEntity? - How can i convert a string to Microsoft.WindowsAzure.Storage.Table.ITableEntity? Azure表存储位数据类型 - Azure Table Storage Bit Data Type DateTime的Azure存储表查询未返回任何结果 - Azure Storage Table query by DateTime not returning any results 无法将azure表存储中string类型的实体更新为null - Not able to update an entity of type string in azure table storage to null 从 Azure 存储表实体中确定值的数据类型 - Determining the data type of a value from an Azure Storage Table entity Azure 存储 - 下载带有条件的 blob - Azure Storage - Download a blob with conditions 绑定中的Azure功能表存储违反类型参数&#39;TElement&#39;的约束 - Azure Function Table Storage in binding violates the constraint of type parameter 'TElement' 为什么Guid在azure表存储中存储为字符串而不是guid类型? - Why Guid is stored as string and not guid type in azure table storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM