コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: ObjectExtensions.cs プロジェクト: yonglehou/Mantle
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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>());
        }
コード例 #10
0
        public static T FromAzureDictionaryEntity <T>(this AzureDictionaryEntity dictionaryEntity)
        {
            var serializer = new DataContractSerializer(typeof(T));

            return((T)(serializer.ReadObject(new MemoryStream(dictionaryEntity.Data))));
        }