Skip to content

Instantly share code, notes, and snippets.

@plaurin
Last active December 17, 2015 02:39
Show Gist options
  • Save plaurin/5537764 to your computer and use it in GitHub Desktop.
Save plaurin/5537764 to your computer and use it in GitHub Desktop.
DocumentStore Repository using Blob storage
public class ProjectRepository
{
private CloudTable table;
private CloudBlobContainer container;
public ProjectRepository()
{
var connectionString = "...";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
var tableClient = storageAccount.CreateCloudTableClient();
this.table = tableClient.GetTableReference("Project");
this.table.CreateIfNotExists();
var blobClient = storageAccount.CreateCloudBlobClient();
this.container = blobClient.GetContainerReference("project");
this.container.CreateIfNotExists();
}
public void Insert(Project project)
{
project.Id = Guid.NewGuid();
var document = JsonConvert.SerializeObject(project, Newtonsoft.Json.Formatting.Indented);
var partitionKey = project.Owner.ToString();
var rowKey = project.Id.ToString();
UploadDocument(partitionKey, rowKey, document);
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = partitionKey;
entity.RowKey = rowKey;
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 listItems = this.container.GetDirectoryReference("project/" + partitionKey).ListBlobs();
return listItems.OfType<CloudBlockBlob>()
.Select(x => this.DownloadDocument(x.Name))
.Select(document => JsonConvert.DeserializeObject<Project>(document));
}
public IEnumerable<Project> ListWithTasks(string partitionKey)
{
var query = new TableQuery<ElasticTableEntity>()
.Select(new [] { "RowKey" })
.Where(TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey",
QueryComparisons.Equal, partitionKey),
TableOperators.And,
TableQuery.GenerateFilterConditionForInt("TotalTasks",
QueryComparisons.GreaterThan, 0)));
dynamic entities = table.ExecuteQuery(query).ToList().Dump();
foreach (var entity in entities)
yield return this.Load(partitionKey, entity.RowKey);
}
public Project Load(string partitionKey, string rowKey)
{
var blobName = string.Format(@"project\{0}\{1}.json", partitionKey, rowKey);
var document = this.DownloadDocument(blobName);
return JsonConvert.DeserializeObject<Project>(document);
}
public void Update(Project project)
{
var document = JsonConvert.SerializeObject(project, Newtonsoft.Json.Formatting.Indented);
var partitionKey = project.Owner.ToString();
var rowKey = project.Id.ToString();
UploadDocument(partitionKey, rowKey, document);
dynamic entity = new ElasticTableEntity();
entity.PartitionKey = partitionKey;
entity.RowKey = rowKey;
entity.ETag = "*";
entity.Name = project.Name;
entity.StartDate = project.StartDate;
entity.TotalTasks = project.Tasks != null ? project.Tasks.Count() : 0;
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));
this.DeleteDocument(entity.PartitionKey, entity.RowKey);
}
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));
this.DeleteDocument(partitionKey, rowKey);
}
private void UploadDocument(string partitionKey, string rowKey, string document)
{
var filename = string.Format(@"project\{0}\{1}.json", partitionKey, rowKey);
var blockBlob = this.container.GetBlockBlobReference(filename);
using (var memory = new MemoryStream())
using (var writer = new StreamWriter(memory))
{
writer.Write(document);
writer.Flush();
memory.Seek(0, SeekOrigin.Begin);
blockBlob.UploadFromStream(memory);
}
blockBlob.Properties.ContentType = "application/json";
blockBlob.SetProperties();
}
private string DownloadDocument(string blobName)
{
var blockBlob = this.container.GetBlockBlobReference(blobName);
using (var memory = new MemoryStream())
using (var reader = new StreamReader(memory))
{
blockBlob.DownloadToStream(memory);
memory.Seek(0, SeekOrigin.Begin);
return reader.ReadToEnd();
}
}
private void DeleteDocument(string partitionKey, string rowKey)
{
var blobName = string.Format(@"project\{0}\{1}.json", partitionKey, rowKey);
var blockBlob = this.container.GetBlockBlobReference(blobName);
blockBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment