/// <summary> /// Remove the instance from table storage /// </summary> /// <param name="tableName">Name of the table</param> /// <param name="instance">the instance to delete</param> /// <param name="conflictHandling">Method for handling ETag conflicts</param> public void Delete(string tableName, dynamic instance, ConflictHandling conflictHandling) { TableItem tableItem = TableItem.Create(instance, _reservedPropertyBehavior); if (tableItem.ETag == null) { Delete(tableName, tableItem.PartitionKey, tableItem.RowKey); } else { _context.DeleteItem(tableName, tableItem, conflictHandling); } }
public static TableItem Create(dynamic entity, ReservedPropertyBehavior reservedPropertyBehavior = ReservedPropertyBehavior.Throw) { bool throwOnReservedPropertyName = reservedPropertyBehavior == ReservedPropertyBehavior.Throw; TableItem item = entity is IDynamicMetaObjectProvider? CreateFromDynamicMetaObject(entity, throwOnReservedPropertyName) : CreateFromType(entity, throwOnReservedPropertyName); if (item.PartitionKey == null) { throw new ArgumentException("Missing PartitionKey property"); } if (item.RowKey == null) { throw new ArgumentException("Missing RowKey property"); } return(item); }
public static TableItem Create(dynamic entity, string partitionKey, string rowKey, ReservedPropertyBehavior reservedPropertyBehavior = ReservedPropertyBehavior.Throw) { bool throwOnReservedPropertyName = reservedPropertyBehavior == ReservedPropertyBehavior.Throw; TableItem item = entity is IDynamicMetaObjectProvider? CreateFromDynamicMetaObject(entity, throwOnReservedPropertyName) : CreateFromType(entity, throwOnReservedPropertyName); if (item.PartitionKey != null && item.PartitionKey != partitionKey) { throw new ArgumentException(string.Format("Entity defines PartitionKey: {0} but it conflicts with partitionKey argument: {1}", item.PartitionKey, partitionKey)); } item.PartitionKey = partitionKey; if (item.RowKey != null && item.RowKey != rowKey) { throw new ArgumentException(string.Format("Entity defines RowKey: {0} but it conflicts with rowKey argument: {1}", item.RowKey, rowKey)); } item.RowKey = rowKey; return(item); }
/// <summary> /// Add instance to the given table /// </summary> /// <param name="tableName">name of the table</param> /// <param name="instance">instance to store</param> /// <remarks> /// This method assumes that T has string properties decorated by the /// PartitionKeyAttribute and RowKeyAttribute, which the framework uses to determine /// the partition and row keys for instance. /// </remarks> /// <exception cref="ArgumentException">if T does not have properties PartitionKey and or RowKey</exception> public void Add(string tableName, dynamic instance) { _context.AddNewItem(tableName, TableItem.Create(instance, _reservedPropertyBehavior)); }
/// <summary> /// Add entity to the given table /// </summary> /// <param name="tableName">Name of the table</param> /// <param name="instance">the instance to store</param> /// <param name="partitionKey">The partition key to use when storing the entity</param> /// <param name="rowKey">The row key to use when storing the entity</param> public void Add(string tableName, dynamic instance, string partitionKey, string rowKey) { _context.AddNewItem(tableName, TableItem.Create(instance, partitionKey, rowKey, _reservedPropertyBehavior)); }
/// <summary> /// Merge the entity /// </summary> /// <param name="tableName">Name of the table</param> /// <param name="instance">the instance to merge</param> /// <param name="conflictHandling">Method for handling ETag conflicts</param> public void Merge(string tableName, dynamic instance, ConflictHandling conflictHandling) { _context.Merge(tableName, TableItem.Create(instance, _reservedPropertyBehavior), conflictHandling); }
/// <summary> /// Update the entity with specified partition key and row key in table storage /// </summary> /// <param name="tableName">Name of the table</param> /// <param name="instance">the instance to update</param> /// <param name="partitionKey">The partition key to use when storing the entity</param> /// <param name="rowKey">The row key to use when storing the entity</param> /// <param name="conflictHandling">Method for handling ETag conflicts</param> public void Update(string tableName, dynamic instance, string partitionKey, string rowKey, ConflictHandling conflictHandling) { _context.Update(tableName, TableItem.Create(instance, partitionKey, rowKey, _reservedPropertyBehavior), conflictHandling); }
private static TableItem CreateFromStaticType( object entity, bool throwOnReservedPropertyName ) { var properties = new Dictionary<string, Tuple<object, Type>>(); foreach ( var property in entity.GetType().GetTypeInfo().GetProperties().Where( p => p.ShouldSerialize() ) ) { properties[property.Name] = new Tuple<object, Type>( property.GetValue( entity, null ), property.PropertyType ); } var item = new TableItem( properties, throwOnReservedPropertyName ); if ( entity.HasPropertyDecoratedWith<PartitionKeyAttribute>() ) { item.PartitionKey = entity.ReadPropertyDecoratedWith<PartitionKeyAttribute, string>(); } if ( entity.HasPropertyDecoratedWith<RowKeyAttribute>() ) { item.RowKey = entity.ReadPropertyDecoratedWith<RowKeyAttribute, string>(); } if ( entity.HasPropertyDecoratedWith<ETagAttribute>() ) { item.ETag = entity.ReadPropertyDecoratedWith<ETagAttribute, object>(); } return item; }
private static TableItem CreateFromDynamicMetaObject( IDynamicMetaObjectProvider entity, bool throwOnReservedPropertyName ) { var properties = new Dictionary<string, Tuple<object, Type>>(); IEnumerable<string> memberNames = ImpromptuInterface.Impromptu.GetMemberNames( entity ); foreach ( var memberName in memberNames ) { dynamic result = ImpromptuInterface.Impromptu.InvokeGet( entity, memberName ); properties[memberName] = new Tuple<object, Type>( (object) result, result.GetType() ); } Tuple<object, Type> key; string partitionKey = null, rowKey = null; if ( !throwOnReservedPropertyName && properties.TryGetValue( "PartitionKey", out key ) ) { properties.Remove( "PartitionKey" ); partitionKey = key.Item1 as string; } if ( !throwOnReservedPropertyName && properties.TryGetValue( "RowKey", out key ) ) { properties.Remove( "RowKey" ); rowKey = key.Item1 as string; } var item = new TableItem( properties, throwOnReservedPropertyName ); if ( partitionKey != null ) { item.PartitionKey = partitionKey; } if ( rowKey != null ) { item.RowKey = rowKey; } return item; }