[英]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.我已经用TableEntity
和ITableEntity
尝试过这个,但我不断收到错误。 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.