简体   繁体   English

Azure.Data.Tables 通用基础 class 问题

[英]Azure.Data.Tables generic base class issue

I am trying to convert a code sample from AzureTableStorage to the new Azure.Data.Tables sdk.我正在尝试将 AzureTableStorage 中的代码示例转换为新的 Azure.Data.Tables sdk。

Here is the code sample I am working from Azure Table Storage Sample这是我正在使用Azure 表存储示例的代码示例

Here is my interface这是我的界面

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);
}

Here is the implementation I am having issues with.这是我遇到问题的实现。

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;
    }

The error I am getting is: Type T must be a reference type in order to be used as a parameter "T"... Compiler Error CS0452我得到的错误是:类型 T 必须是引用类型才能用作参数“T”... 编译器错误 CS0452

Now I know I can't return a AsyncPageable<T> queryResults as a List<T> , but I am not even to that point yet.现在我知道我不能将AsyncPageable<T> queryResults作为List<T>返回,但我什至还没有到那一点。

I would love some generic assistance.我会喜欢一些通用的帮助。 Thank you.谢谢你。

I successfully Implemented Azure.Data.Tables With Generic base class, You can Pass the T class and table name and get the appropriate results.我成功地实现了 Azure.Data.Tables 与通用基础 class,您可以通过 T class 和表名并获得适当的结果。

My Generic Class using Azure;我的通用 Class使用 Azure; using Azure.Data.Tables;使用 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;
    }


}

My Interface我的界面

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);
}

How you can use it in your service如何在服务中使用它

Suppose you having Otp Service假设您有 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
        });
        
    }


}

It's fully working code I am also using in my project.这是我在我的项目中也使用的完全有效的代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM