public void Delete <T>(string entityId, string dictionaryId) { if (String.IsNullOrEmpty(entityId)) { throw new ArgumentException("Entity ID is required.", "entityId"); } if (String.IsNullOrEmpty(dictionaryId)) { throw new ArgumentException("Dictionary ID is required.", "dictionaryId"); } CloudTable table = GetCloudTable <T>(); AzureDictionaryEntity tableEntity = LoadTableEntity(entityId, dictionaryId, table); if (tableEntity == null) { throw new ArgumentException( "Unable to delete entity. The specified entity was not found in dictionary storage.", "entity"); } TableOperation deleteOperation = TableOperation.Delete(tableEntity); table.Execute(deleteOperation); }
public static AzureDictionaryEntity ToAzureDictionaryEntity <T>(this T dictionaryEntity) where T : DictionaryEntity { var azEntity = new AzureDictionaryEntity(); if (String.IsNullOrEmpty(dictionaryEntity.DictionaryId)) { azEntity.PartitionKey = Guid.NewGuid().ToString(); } else { azEntity.PartitionKey = dictionaryEntity.DictionaryId; } azEntity.RowKey = dictionaryEntity.EntityId; using (var entityStream = new MemoryStream()) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(entityStream, dictionaryEntity); entityStream.Position = 0; azEntity.Data = entityStream.ToArray(); } return(azEntity); }
public void Update <T>(T entity, string entityId, string dictionaryId) { if (String.IsNullOrEmpty(entityId)) { throw new ArgumentException("Entity ID is required.", "entityId"); } if (String.IsNullOrEmpty(dictionaryId)) { throw new ArgumentException("Dictionary ID is required.", "dictionaryId"); } CloudTable table = GetCloudTable <T>(); AzureDictionaryEntity tableEntity = LoadTableEntity(entityId, dictionaryId, table); if (tableEntity == null) { throw new ArgumentException( "Unable to update entity. The specified entity was not found in dictionary storage.", "entity"); } tableEntity.Data = entity.SerializeToBytes(); TableOperation updateOperation = TableOperation.Replace(tableEntity); table.Execute(updateOperation); }
public static AzureDictionaryEntity ToAzureDictionaryEntity <T>(this T entity, string entityId, string dictionaryId = null) { if (String.IsNullOrEmpty(entityId)) { throw new ArgumentException("Entity ID is required.", "entityId"); } var azEntity = new AzureDictionaryEntity(); if (String.IsNullOrEmpty(dictionaryId)) { azEntity.PartitionKey = typeof(T).Name; } else { azEntity.PartitionKey = dictionaryId; } azEntity.RowKey = entityId; using (var entityStream = new MemoryStream()) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(entityStream, entity); entityStream.Position = 0; azEntity.Data = entityStream.ToArray(); } return(azEntity); }
public void Insert <T>(T entity) where T : DictionaryEntity { CloudTable table = GetCloudTable <T>(); AzureDictionaryEntity tableEntity = entity.ToAzureDictionaryEntity(); TableOperation insertOperation = TableOperation.Insert(tableEntity); table.Execute(insertOperation); }
public void Insert <T>(T entity, string entityId) { if (String.IsNullOrEmpty(entityId)) { throw new ArgumentException("Entity ID is required.", "entityId"); } CloudTable table = GetCloudTable <T>(); AzureDictionaryEntity tableEntity = entity.ToAzureDictionaryEntity(entityId); TableOperation insertOperation = TableOperation.Insert(tableEntity); table.Execute(insertOperation); }
public void Delete <T>(T entity) where T : DictionaryEntity { CloudTable table = GetCloudTable <T>(); AzureDictionaryEntity tableEntity = LoadTableEntity(entity.EntityId, entity.DictionaryId, table); if (tableEntity == null) { throw new ArgumentException( "Unable to delete entity. The specified entity was not found in dictionary storage.", "entity"); } TableOperation deleteOperation = TableOperation.Delete(tableEntity); table.Execute(deleteOperation); }
public void Update <T>(T entity) where T : DictionaryEntity { CloudTable table = GetCloudTable <T>(); AzureDictionaryEntity tableEntity = LoadTableEntity(entity.EntityId, entity.DictionaryId, table); if (tableEntity == null) { throw new ArgumentException( "Unable to update entity. The specified entity was not found in dictionary storage.", "entity"); } tableEntity.Data = entity.SerializeToBytes(); TableOperation updateOperation = TableOperation.Replace(tableEntity); table.Execute(updateOperation); }
public T LoadEntity <T>(string entityId, string dictionaryId) { if (String.IsNullOrEmpty(entityId)) { throw new ArgumentException("Entity ID is required.", "entityId"); } if (String.IsNullOrEmpty(dictionaryId)) { throw new ArgumentException("Dictionary ID is required.", "dictionaryId"); } CloudTable table = GetCloudTable <T>(); AzureDictionaryEntity tableEntity = LoadTableEntity(entityId, dictionaryId, table); if (tableEntity == null) { return(default(T)); } return(tableEntity.FromAzureDictionaryEntity <T>()); }
public static T FromAzureDictionaryEntity <T>(this AzureDictionaryEntity dictionaryEntity) { var serializer = new DataContractSerializer(typeof(T)); return((T)(serializer.ReadObject(new MemoryStream(dictionaryEntity.Data)))); }