c# - Azure.Data.Tables通用基类问题

标签 c# azure generics azure-table-storage

我正在尝试将代码示例从 AzureTableStorage 转换为新的 Azure.Data.Tables sdk。

这是我正在使用的代码示例 Azure Table Storage Sample

这是我的界面

public interface IAzureTableStorage<T> where T : ITableEntity, new()
{
    Task Delete(string rowKey);
    Task<T> GetItem(string rowKey);
    Task<List<T>> GetList();
    Task UpSert(T item);
}

这是我遇到问题的实现。

public class AzureTableStorage<T> : IAzureTableStorage<T> where T : ITableEntity, new()
{
    private readonly AzureTableSettings Settings;
    private readonly TableClient TableClient;
    public AzureTableStorage(AzureTableSettings settings)
    {
        Settings = settings;
        TableClient = GetTableClient();
    }
    public Task<List<T>> GetList()
    {
        AsyncPageable<T> queryResults = TableClient.QueryAsync<T>(filter: $"PartitionKey eq '{Settings.TableName}'");

        //CloudTable table = await GetTableAsync();
        //TableQuery<T> query = new TableQuery<T>();
        //List<T> results = new List<T>();

        //TableContinuationToken continuationToken = null;
        //do
        //{
        //  TableQuerySegment<T> queryResults = await table.ExecuteQuerySegmentedAsync(query, continuationToken);
        //  continuationToken = queryResults.ContinuationToken;
        //  results.AddRange(queryResults.Results);
        //} while (continuationToken != null);
        //return results;
    }

我收到的错误是: 类型 T 必须是引用类型才能用作参数“T”... Compiler Error CS0452

现在我知道我无法返回 AsyncPageable<T> queryResults作为List<T> ,但我还没到那一步。

我希望获得一些一般帮助。谢谢。

最佳答案

我成功地使用通用基类实现了 Azure.Data.Tables, 您可以传递T类和表名并获得适当的结果。

我的通用类 使用 Azure; 使用 Azure.Data.Tables;

public class AzureTableStorage<TEntity> : IAzureTableStorage<TEntity> where TEntity : class, ITableEntity, new()
{ 
    private async Task<TableClient> GetTableClient(string tableName)
    { 
        TableServiceClient tableServiceClient = new TableServiceClient(Environment.GetEnvironmentVariable("StorageConnectionString"));
         
        TableClient tableClient = tableServiceClient.GetTableClient(
            tableName: tableName
        );

        await tableClient.CreateIfNotExistsAsync();
        return tableClient;
    }


    public async Task<AsyncPageable<TEntity>> QueryAllAsync(string tableName, string partitionKey)
    {
        var tableClient = await GetTableClient(tableName);

        var queryResultsFilter = tableClient.QueryAsync<TEntity>(
            filter: $"PartitionKey eq '{partitionKey}'"
            );

        return queryResultsFilter;
    }

    public async Task<AsyncPageable<TEntity>> QueryWithFilterAsync(string tableName, string filters)
    {
        var tableClient = await GetTableClient(tableName);

        AsyncPageable<TEntity> queryResults = tableClient.QueryAsync<TEntity>(filter: $"{filters}");

        //AsyncPageable<T> queryTableResults = tableClient.QueryAsync(filter: $"TableName eq '{tableName}'"); 
        //AsyncPageable<TableEntity> queryResultsSelect = tableClient.QueryAsync<TableEntity>(select: new List<string>() { "Product", "Price" }); 
        //AsyncPageable<TableEntity> queryResultsMaxPerPage = tableClient.QueryAsync<TableEntity>(maxPerPage: 10);
        return queryResults;
    }

    public async Task<TEntity> GetAsync(string tableName, string partitionKey, string rowKey)
    {
        var tableClient = await GetTableClient(tableName);

        var queryResultsFilter = await tableClient.GetEntityAsync<TEntity>(
            rowKey: rowKey,
            partitionKey: partitionKey
        );

        return queryResultsFilter;
    }

    public async Task<Response> AddOrUpdateAsync(string tableName, TEntity entity)
    {
        var tableClient = await GetTableClient(tableName);
        var entityReturn = await tableClient.UpsertEntityAsync(entity);
        return entityReturn;
    }
    public async Task<Response> InsertAsync(string tableName, TEntity entity)
    {
        var tableClient = await GetTableClient(tableName);
        var entityReturn = await tableClient.AddEntityAsync(entity);
        return entityReturn;
    }
    public async Task<Response> UpdateAsync(string tableName, TEntity entity)
    {
        var tableClient = await GetTableClient(tableName);
        var entityReturn = await tableClient.UpdateEntityAsync(entity, entity.ETag);
        return entityReturn;
    }

    public async Task<Response> DeleteAsync(string tableName, string rowKey, string partitionKey)
    {
        var tableClient = await GetTableClient(tableName);
        var entity = await tableClient.DeleteEntityAsync(rowKey: rowKey, partitionKey: partitionKey);
        return entity;
    }


}

我的界面

public interface IAzureTableStorage<TEntity> where TEntity : class, ITableEntity, new()
{
    Task<AsyncPageable<TEntity>> QueryAllAsync(string tableName, string partitionKey);
    Task<AsyncPageable<TEntity>> QueryWithFilterAsync(string tableName, string filters);
    Task<TEntity> GetAsync(string tableName, string partitionKey, string rowKey);

    Task<Response> AddOrUpdateAsync(string tableName, TEntity entity);
    Task<Response> InsertAsync(string tableName, TEntity entity);
    Task<Response> UpdateAsync(string tableName, TEntity entity);
    Task<Response> DeleteAsync(string tableName, string partitionKey, string rowKey);
}

如何在您的服务中使用它

假设您有 OTP 服务

public class OtpService : IOtpService
{
    private readonly string tableName = "Otp";

    private readonly IAzureTableStorage<Otp> _azureTableStorage;
    public OtpService(IAzureTableStorage<Otp> azureTableStorage)
    {
        _azureTableStorage = azureTableStorage;
    }

    public async Task TestingAzureTableStorage()
    {
        var To = "Mr Singh";
        //how to use queryable and get your data.
        var data = await (await _azureTableStorage.QueryWithFilterAsync(tableName, $"To eq '{To}'")).ToListAsync();
        //below is how to use insert
        await _azureTableStorage.InsertAsync(tableName, new Otp() { PartitionKey="Otp"
        //here put your rest model fields
        });
        
    }


}

这是我也在我的项目中使用的完全有效的代码。

关于c# - Azure.Data.Tables通用基类问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73262773/

相关文章:

c# - ComboBox 的值加倍 c#

azure - 为西方世界提供服务的最佳 Azure 数据中心

azure - 相同大小的 VMSS 的不同处理器

C# Generic - 任何声明 T 具有属性的方法?

c# - 为什么类型转换很昂贵?

c# - ASP.NET Kendo UI 网格计算列

c# - 正则表达式:仅限 A-Z 字符

sql-server - 使用 SQL Azure 数据库时存储过程是否有任何差异/限制

java - 在方法中创建通用实例

java - Map.get(Object key) 不是(完全)通用的原因是什么