public void AddNewItem(string tableName, TableItem tableItem)
        {
            GenericTableEntity genericTableEntity = GenericTableEntity.HydrateFrom(tableItem);
            var operation = TableOperation.Insert(genericTableEntity);

            _operations.Enqueue(new ExecutableTableOperation(tableName, operation, TableOperationType.Insert, tableItem.PartitionKey, tableItem.RowKey));
        }
        public void Upsert(string tableName, TableItem tableItem)
        {
            // Upsert does not use an ETag (If-Match header) - http://msdn.microsoft.com/en-us/library/windowsazure/hh452242.aspx
            GenericTableEntity genericTableEntity = GenericTableEntity.HydrateFrom(tableItem);
            var operation = TableOperation.InsertOrReplace(genericTableEntity);

            _operations.Enqueue(new ExecutableTableOperation(tableName, operation, TableOperationType.InsertOrReplace, tableItem.PartitionKey, tableItem.RowKey));
        }
        public void DeleteItem(string tableName, TableItem tableItem, ConflictHandling conflictHandling)
        {
            var genericTableEntity = GenericTableEntity.HydrateFrom(tableItem);

            if (ShouldForceOverwrite(conflictHandling, genericTableEntity))
            {
                genericTableEntity.ETag = "*";
            }
            var operation = TableOperation.Delete(genericTableEntity);

            _operations.Enqueue(new ExecutableTableOperation(tableName, operation, TableOperationType.Delete, tableItem.PartitionKey, tableItem.RowKey));
        }
Beispiel #4
0
        public void CreateInstanceFromProperties_TargetTypeDecoratedWithETagAttribute_ETagSetCorrectly()
        {
            var genericEntity = new GenericTableEntity
             {
            ETag = "tag"
             };
             var entityProperties = new Dictionary<string, EntityProperty>();
             entityProperties["Age"] = new EntityProperty( 42 );
             genericEntity.ReadEntity( entityProperties, null );

             var item = genericEntity.ConvertTo<DecoratedItemWithETag>();
             Assert.AreEqual( "tag", item.ETag, "Incorrect ETag" );
        }
Beispiel #5
0
        public void CreateInstanceFromProperties_TargetTypeDecoratedWithRowAndPartitionKeyAttributes_RowAndPartitionKeySetCorrectly()
        {
            var genericEntity = new GenericTableEntity
                             {
                                PartitionKey = "foo", RowKey = "bar"
                             };
             var entityProperties = new Dictionary<string, EntityProperty>();
             entityProperties["Age"] = new EntityProperty( 42 );
             genericEntity.ReadEntity( entityProperties, null );

             var item = genericEntity.ConvertTo<DecoratedItem>();
             Assert.AreEqual( "foo", item.Id, "Incorrect partition key" );
             Assert.AreEqual( "bar", item.Name, "Incorrect row key" );
        }
Beispiel #6
0
        public void CreateInstanceFromProperties_TargetTypeDecoratedWithTimestampAttribute_TimestampSetCorrectly()
        {
            var dt = DateTime.Now;
             var genericEntity = new GenericTableEntity
             {
            Timestamp = dt
             };
             var entityProperties = new Dictionary<string, EntityProperty>();
             entityProperties["Age"] = new EntityProperty( 42 );
             genericEntity.ReadEntity( entityProperties, null );

             var item = genericEntity.ConvertTo<DecoratedItemWithTimestamp>();
             Assert.AreEqual( dt, item.Timestamp, "Incorrect Timestamp" );
        }
Beispiel #7
0
 public void Add( GenericTableEntity entity )
 {
     AzureKeyValidator.ValidatePartitionKey( entity.PartitionKey );
     AzureKeyValidator.ValidateRowKey( entity.RowKey );
     lock ( _entities )
     {
        if ( _entities.ContainsKey( entity.RowKey ) )
        {
       throw new EntityAlreadyExistsException();
        }
        entity.ETag = GetNewETag();
        entity.Timestamp = DateTimeOffset.UtcNow;
        _entities[entity.RowKey] = entity;
     }
 }
Beispiel #8
0
        public static GenericTableEntity HydrateFrom(TableItem tableItem)
        {
            Dictionary <string, EntityProperty> properties = GetProperties(tableItem.Properties);

            var genericEntity = new GenericTableEntity();

            foreach (KeyValuePair <string, EntityProperty> keyValuePair in properties)
            {
                genericEntity._properties.Add(keyValuePair.Key, keyValuePair.Value);
            }

            genericEntity.PartitionKey = tableItem.PartitionKey;
            genericEntity.RowKey       = tableItem.RowKey;
            genericEntity.ETag         = tableItem.ETag == null ? null : tableItem.ETag.ToString();

            return(genericEntity);
        }
Beispiel #9
0
 internal override T ConvertResult(GenericTableEntity e)
 {
     return(e.ConvertTo <T>());
 }
Beispiel #10
0
 internal override object ConvertResult(GenericTableEntity e)
 {
     return(e.ConvertToDynamic(IncludeETagForDynamic));
 }
Beispiel #11
0
 internal abstract T ConvertResult(GenericTableEntity e);
Beispiel #12
0
            public void Merge( GenericTableEntity entity )
            {
                lock ( _entities )
                {
                   if ( !_entities.ContainsKey( entity.RowKey ) )
                   {
                  throw new EntityDoesNotExistException();
                   }
                   if ( EntityHasBeenChanged( entity ) )
                   {
                  throw new EntityHasBeenChangedException();
                   }

                   var currentEntity = _entities[entity.RowKey];
                   foreach ( var property in entity.WriteEntity( null ) )
                   {
                  currentEntity.SetProperty( property.Key, property.Value );
                   }
                   currentEntity.ETag = GetNewETag();
                   currentEntity.Timestamp = DateTimeOffset.UtcNow;
                }
            }
Beispiel #13
0
 private bool EntityHasBeenChanged( GenericTableEntity entity )
 {
     var hasETagProperty = !string.IsNullOrEmpty( entity.ETag );
     if ( hasETagProperty && entity.ETag.Equals( "*" ) )
     {
        return false;
     }
     var entityHasChanged = false;
     if ( hasETagProperty )
     {
        entityHasChanged = !entity.ETag.Equals( _entities[entity.RowKey].ETag );
     }
     return entityHasChanged;
 }
Beispiel #14
0
 public void Upsert( GenericTableEntity entity )
 {
     lock ( _entities )
     {
        entity.ETag = GetNewETag();
        entity.Timestamp = DateTimeOffset.UtcNow;
        _entities[entity.RowKey] = entity;
     }
 }
Beispiel #15
0
 public void Update( GenericTableEntity entity )
 {
     lock ( _entities )
     {
        if ( !_entities.ContainsKey( entity.RowKey ) )
        {
       throw new EntityDoesNotExistException();
        }
        if ( EntityHasBeenChanged( entity ) )
        {
       throw new EntityHasBeenChangedException();
        }
        entity.ETag = GetNewETag();
        entity.Timestamp = DateTimeOffset.UtcNow;
        _entities[entity.RowKey] = entity;
     }
 }
 private static bool ShouldForceOverwrite(ConflictHandling conflictHandling, GenericTableEntity genericTableEntity)
 {
     return(string.IsNullOrEmpty(genericTableEntity.ETag) || conflictHandling.Equals(ConflictHandling.Overwrite));
 }
Beispiel #17
0
 public void Delete( GenericTableEntity entity )
 {
     lock ( _entities )
     {
        if ( _entities.ContainsKey( entity.RowKey ) )
        {
       if ( EntityHasBeenChanged( entity ) )
       {
          throw new EntityHasBeenChangedException();
       }
       _entities.Remove( entity.RowKey );
        }
     }
 }