如何在 c# .NET 中的方法中参数化 Azure 的 TableOperation.Retrieve<TElement>
How can I parameterise Azure's TableOperation.Retrieve<TElement> in a method in c# .NET
我有一个 class,如下所示:
public class Table : ITable
{
private CloudStorageAccount storageAccount;
public Table()
{
var storageAccountSettings = ConfigurationManager.ConnectionStrings["AzureStorageConnection"].ToString();
storageAccount = CloudStorageAccount.Parse(storageAccountSettings);
}
public async Task<TableResult> Retrieve(string tableReference, string partitionKey, string rowKey)
{
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference(tableReference);
TableOperation retrieveOperation = TableOperation.Retrieve<SomeDomainModelType>(partitionKey, rowKey);
TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);
return retrievedResult;
}
}
此 class 是一个包装器,用于从 Azure table 检索单个实体。它被打包并符合一个接口,以便在测试期间可以用 Microsoft Fakes 将其删除。它目前有效,但如果以下内容更通用,它会更优雅:
TableOperation retrieveOperation = TableOperation.Retrieve<SomeDomainModelType>(partitionKey, rowKey);
我的问题是如何参数化 <SomeDomainModelType>
以便我可以将方法用于域模型中的任何类型?有什么想法吗?
查看 C# Programming Guide on Generics, specifically the Generic Methods 部分。基本上,定义一个可以采用 ITableEntity 的通用方法:
public async Task<TableResult> Retrieve<T>(string tableReference, string partitionKey, string rowKey) where T : ITableEntity
{
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference(tableReference);
TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);
return retrievedResult;
}
实际上,您可以return retrievedResult.Result直接作为一个实体:
public async Task<TEntity> Retrieve<TEntity>(string tableReference, string partitionKey, string rowKey) : where TEntity : ITableEntity
{
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference(tableReference);
TableOperation retrieveOperation = TableOperation.Retrieve<TEntity>(partitionKey, rowKey);
TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);
return (TEntity)retrievedResult.Result;
}
我有一个 class,如下所示:
public class Table : ITable
{
private CloudStorageAccount storageAccount;
public Table()
{
var storageAccountSettings = ConfigurationManager.ConnectionStrings["AzureStorageConnection"].ToString();
storageAccount = CloudStorageAccount.Parse(storageAccountSettings);
}
public async Task<TableResult> Retrieve(string tableReference, string partitionKey, string rowKey)
{
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference(tableReference);
TableOperation retrieveOperation = TableOperation.Retrieve<SomeDomainModelType>(partitionKey, rowKey);
TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);
return retrievedResult;
}
}
此 class 是一个包装器,用于从 Azure table 检索单个实体。它被打包并符合一个接口,以便在测试期间可以用 Microsoft Fakes 将其删除。它目前有效,但如果以下内容更通用,它会更优雅:
TableOperation retrieveOperation = TableOperation.Retrieve<SomeDomainModelType>(partitionKey, rowKey);
我的问题是如何参数化 <SomeDomainModelType>
以便我可以将方法用于域模型中的任何类型?有什么想法吗?
查看 C# Programming Guide on Generics, specifically the Generic Methods 部分。基本上,定义一个可以采用 ITableEntity 的通用方法:
public async Task<TableResult> Retrieve<T>(string tableReference, string partitionKey, string rowKey) where T : ITableEntity
{
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference(tableReference);
TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);
return retrievedResult;
}
实际上,您可以return retrievedResult.Result直接作为一个实体:
public async Task<TEntity> Retrieve<TEntity>(string tableReference, string partitionKey, string rowKey) : where TEntity : ITableEntity
{
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference(tableReference);
TableOperation retrieveOperation = TableOperation.Retrieve<TEntity>(partitionKey, rowKey);
TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);
return (TEntity)retrievedResult.Result;
}