CRUD Operation with Azure.Data.Tables With Generic base class

If you wanted to store data on Azure data table storage then you need to write general code in your system for preventing rewriting a code.

So I created one generic class you can use this and easy to achieve Robert Program.

  1. Install Azure.Data.Tables from the NuGet package.

Create your Generic Class

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


}

2. Then Create 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);
}

3. Call your Generic in your service.

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


}

So finally we achived.