Last active
March 29, 2017 13:26
-
-
Save DaniCCardenas/848628864ba26a30a6b1aa2eab36e6e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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