Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class AzureTableStorage<T> where T : TableEntity, IAzureStorageTable, new()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudTableClient tableClient;
CloudTable table;
public AzureTableStorage(string tableName)
{
tableClient = storageAccount.CreateCloudTableClient();
table = tableClient.GetTableReference(tableName);
table.CreateIfNotExists();
}
public T Insert(T entity)
{
TableOperation insertOperation = TableOperation.Insert(entity);
table.Execute(insertOperation);
return entity;
}
public List<T> GetAll(string partitionKey)
{
List<T> entities = new List<T>();
TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
entities = table.ExecuteQuery(query).ToList();
return entities;
}
public T Find(string partitionKey, string rowKey)
{
T entity = new T();
TableQuery<T> query = new TableQuery<T>().Where(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey),
TableOperators.And,
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, rowKey)));
var tableSet = table.ExecuteQuery(query).ToList();
if (tableSet.Count >= 1)
{
return tableSet.First();
}
return null;
}
}
public interface IAzureStorageTable
{
void SetPartitionKey(string key);
void SetRowKey(string id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment