Skip to content

Instantly share code, notes, and snippets.

@DaniCCardenas
Last active March 29, 2017 13:26
Show Gist options
  • Save DaniCCardenas/848628864ba26a30a6b1aa2eab36e6e5 to your computer and use it in GitHub Desktop.
Save DaniCCardenas/848628864ba26a30a6b1aa2eab36e6e5 to your computer and use it in GitHub Desktop.
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