Skip to content

Instantly share code, notes, and snippets.

@plaurin
Last active December 15, 2015 12:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plaurin/5260045 to your computer and use it in GitHub Desktop.
Save plaurin/5260045 to your computer and use it in GitHub Desktop.
Document store implementation for Windows Azure Table Storage service using the ElasticTableEntity class
public class ProjectRepository
{
private CloudTable table;
public ProjectRepository()
{
var connectionString = "...";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
var client = storageAccount.CreateCloudTableClient();
this.table = client.GetTableReference("Project");
this.table.CreateIfNotExists();
}
public void Insert(Project project)
{
project.Id = Guid.NewGuid();
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = project.Owner.ToString();
entity.RowKey = project.Id.ToString();
entity.Document = JsonConvert.SerializeObject(project, Newtonsoft.Json.Formatting.Indented);
entity.Name = project.Name;
entity.StartDate = project.StartDate;
entity.TotalTasks = project.Tasks.Count();
this.table.Execute(TableOperation.Insert(entity));
}
public IEnumerable<Project> List(string partitionKey)
{
var query = new TableQuery<ElasticTableEntity>()
.Select(new [] { "Document" })
.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
dynamic entities = table.ExecuteQuery(query).ToList();
foreach (var entity in entities)
{
var document = (string)entity.Document.StringValue;
yield return JsonConvert.DeserializeObject<Project>(document);
}
}
public Project Load(string partitionKey, string rowKey)
{
var query = new TableQuery<ElasticTableEntity>()
.Select(new [] { "Document" })
.Where(TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey),
TableOperators.And,
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, rowKey)));
dynamic entity = table.ExecuteQuery(query).SingleOrDefault();
if (entity != null)
{
var document = (string)entity.Document.StringValue;
return JsonConvert.DeserializeObject<Project>(document);
}
return null;
}
public void Update(Project project)
{
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = project.Owner.ToString();
entity.RowKey = project.Id.ToString();
entity.ETag = "*";
entity.Document = JsonConvert.SerializeObject(project, Newtonsoft.Json.Formatting.Indented);
entity.Name = project.Name;
entity.StartDate = project.StartDate;
entity.TotalTasks = project.Tasks.Count();
this.table.Execute(TableOperation.Replace(entity));
}
public void Delete(Project project)
{
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = project.Owner.ToString();
entity.RowKey = project.Id.ToString();
entity.ETag = "*";
this.table.Execute(TableOperation.Delete(entity));
}
public void Delete(string partitionKey, string rowKey)
{
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = partitionKey;
entity.RowKey = rowKey;
entity.ETag = "*";
this.table.Execute(TableOperation.Delete(entity));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment