Trying to work out Azure.Data.Tables SDK but seem to only find 1 trillion examples using the depricated WindowsAzure SDK which no longer seem to work in the newer SDK.
Problem: I have a number of POCO entities which implement ITableEntity and have various specific properties e.g.:
public class BenefitEntity: ITableEntity
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public DateTimeOffset? Timestamp { get; set; }
public ETag ETag { get; set; }
public string Name { get; set; }
public double Value { get; set; }
}
I have used the TableClient<TableEntity>
to query and return a Pageable<TableEntity>
without any trouble
var results = tableClient.Query<TableEntity>(filter: $"PartitionKey eq '{partitionKey}'");
I can happily iterate through the TableEntities and lift off the values I need using TableEntity.GetString("Name")
etc.
What I can't do is return a Pageable<BenefitEntity>
using var results = tableClient.Query<BenefitEntity>(filter: $"PartitionKey eq '{partitionKey}'");
or work out a simple way to convert TableEntity to BenefitEntity (or indeed BenefitEntity to TableEntity) to use the TableClient methods.
I am sure this is not a novel use case but I am flummoxed in terms of finding any documentation on a conversion.
What I had hoped was to make a generic class where I could specify a type <T>
and use that for the Type of the tableClient.Query method and thus be able to return a typed List or observable collection back to the method call. Like you can quite easily do if you use the basic HttpClient and the rest API directly to simply deserialise the response body JSON into a collection of your POCOs.
All I can find is DynamicTableEntity options to flatten POCOs to dictionaries with reflection and use the DynamicTableEntity.Properties to reform objects which seem to have disappeared as an option with Azure.Data.Tables
Any help much appreciated.
Update
Spent hours messing around and realised you can just change the TableEntity type in the tableclient.query method to BenefitEntity to get a Pageable<BenefitEntity>
in return.
Given I have 30 or so object types (in a similar form as the BenefitEntity) I then tried to make a generic version of the method:
public Pageable<T> GetItems<T>(string tableName, string partitionKey, string accountName, string storageAccountKey, string storageUri)
{
var tableClient = new TableClient(
new Uri(storageUri),
tableName,
new TableSharedKeyCredential(accountName, storageAccountKey));
return tableClient.Query<T>(filter: $"PartitionKey eq '{partitionKey}'");
}
To find that I was getting errors about using the type T on tableClient.Query<T>
and needing a reference type with a public parameterless constructor.
More time with Google and finally came across Generic Type constraints...
public Pageable<T> GetItems<T>(string tableName, string partitionKey, string accountName, string storageAccountKey, string storageUri)
becomes
public Pageable<T> GetItems<T>(string tableName, string partitionKey, string accountName, string storageAccountKey, string storageUri) where T : class, ITableEntity, new()
and It is now working - what a waste of a day! but I learned new things.