Reputation: 341
I am trying to convert a code sample from AzureTableStorage to the new Azure.Data.Tables sdk.
Here is the code sample I am working from Azure Table Storage Sample
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
Now I know I can't return a AsyncPageable<T> queryResults
as a List<T>
, but I am not even to that point yet.
I would love some generic assistance. Thank you.
Upvotes: 0
Views: 1780
Reputation: 134
I successfully Implemented Azure.Data.Tables With Generic base class, You can Pass the T class and table name and get the appropriate results.
My Generic Class using Azure; using 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
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.
Upvotes: 4