public ITableEntity AfterLoad(ITableEntity entity) { if (entity == null) { return(entity); } var type = entity.GetType(); var rowKey = this.RowKey(type); var partitionKey = this.PartitionKey(type); if (!string.IsNullOrWhiteSpace(partitionKey) && type.GetProperty(partitionKey).CanWrite) { type.GetProperty(partitionKey).SetValue(entity, entity.PartitionKey); } if (!string.IsNullOrWhiteSpace(rowKey) && type.GetProperty(rowKey).CanWrite) { type.GetProperty(rowKey).SetValue(entity, entity.RowKey); } foreach (var p in type.GetProperties()) { var jsonData = p.GetCustomAttribute <JsonDataAttribute>(); if (jsonData != null) { p.SetValue(entity, ToObject(p.PropertyType, (string)type.GetProperty(jsonData.PropertyName).GetValue(entity))); } } return(entity); }
static string ErrorMessage(ITableEntity azureEntity, ITableEntity wossEntity) { StringBuilder sb = new StringBuilder("Entity mismatch between azure and woss table:\r\n"); if (azureEntity == null) { sb.Append("Azure entity is null\r\n"); } else { sb.Append("Azure entity is " + azureEntity.GetType() + ":\r\n"); sb.Append(Serialize(azureEntity)); sb.Append("\r\n"); } if (wossEntity == null) { sb.Append("Woss entity is null\r\n"); } else { sb.Append("Woss entity is " + wossEntity.GetType() + ":\r\n"); sb.Append(Serialize(wossEntity)); } return(sb.ToString()); }
internal static IDictionary <string, object> Build(ITableEntity entity) { var properties = entity.GetType().GetProperties(Flags); var result = new Dictionary <string, object>(); for (var i = 0; i < properties.Length; i++) { if (string.Compare(properties[i].Name, "TableName", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(properties[i].Name, "Timestamp", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(properties[i].Name, "ETag", StringComparison.OrdinalIgnoreCase) == 0) { continue; } var entityInfo = new EntityPropertyInfo(properties[i].GetValue(entity), properties[i].PropertyType); result.Add(properties[i].Name, entityInfo.Value); if (entityInfo.EdmDataType != null) { result.Add(properties[i].Name + "@odata.type", entityInfo.EdmDataType); } } return(result); }
private static async Task Track(CloudTable table, ITableEntity entity, TraceWriter log) { log.Info($"Track {entity.GetType().Name}: {entity.RowKey}"); var operation = TableOperation.InsertOrReplace(entity); await table.ExecuteAsync(operation).ConfigureAwait(false); }
public void ReadEntity(ITableEntity entity, IDictionary <string, EntityProperty> properties) { Entity = new T(); Metadatas.Clear(); ReadProp(entity, entity.GetType().GetProperties(), properties); ReadProp(Entity, EntityProperties, properties); ReadMetadatas(Metadatas, EntityProperties, properties); }
public void AttachObject(ITableEntity tableEntity, bool asModified) { var method = Get_GetTable_Method(tableEntity.GetType()); var table = method.Invoke(LinqToSqlContext, null) as ITable; table.Attach(tableEntity, asModified); }
/// <summary> /// Replace (update) an existing table entity. /// </summary> /// <param name="tableEntity">Table entity to replace (update).</param> public void ReplaceEntity( ITableEntity tableEntity) { var table = GetTableReference(tableEntity.GetType().Name); var insertOperation = TableOperation.InsertOrReplace(tableEntity); table.Execute(insertOperation); }
public void AddObject(ITableEntity tableEntity) { var method = Get_GetTable_Method(tableEntity.GetType()); var table = method.Invoke(LinqToSqlContext, null) as ITable; table.InsertOnSubmit(tableEntity); }
//public async Task DestoryTablesForDevOnlyAsync(string onlyThisTableByName = null) //{ // if (_connectionString != "UseDevelopmentStorage=true;") // throw new Exception("this will delete all your datas. only for dev/test purposes. Connection string must = \"UseDevelopmentStorage=true;\""); // var tables = await _tableClient.ListTablesSegmentedAsync() // foreach (var table in _tableClient.ListTables().Where(x => x.Name == onlyThisTableByName || onlyThisTableByName == null)) // await table.DeleteAsync(); //} public async Task InsertOrReplaceAsync(ITableEntity e, string overrideTableName = null) { var table = _tableClient.GetTableReference(overrideTableName ?? e.GetType().Name); await table.CreateIfNotExistsAsync(); var insertOperation = TableOperation.InsertOrReplace(e); await table.ExecuteAsync(insertOperation); }
public void AttachObject(ITableEntity tableEntity, ITableEntity original) { var method = Get_GetTable_Method(tableEntity.GetType()); var table = method.Invoke(LinqToSqlContext, null) as ITable; table.Attach(tableEntity, original); }
//public static void DecryptBytes(this Blob document) //{ // var cryptoProperties = document.GetType().GetProperties() // .Where(prop => prop.GetCustomAttributes(true).Length > 0); //typeof(CryptoAttribute), true)); // foreach (var cryptoProperty in cryptoProperties) // { // var encryptedValue = cryptoProperty.GetValue(document); // var decryptedValue = Decrypt(encryptedValue); // cryptoProperty.SetValue(document, decryptedValue); // } //} public static void EncryptFields(this ITableEntity document) { var cryptoProperties = document.GetType().GetProperties() .Where(prop => prop.GetCustomAttributes(true).Length > 0); //typeof(CryptoAttribute), true)); foreach (var cryptoProperty in cryptoProperties) { var decryptedValue = cryptoProperty.GetValue(document); var encryptedValue = Encrypt(decryptedValue); cryptoProperty.SetValue(document, encryptedValue); } }
public IDictionary <string, EntityProperty> WriteEntity(ITableEntity entity) { Dictionary <string, EntityProperty> retVals = new Dictionary <string, EntityProperty>(); var objectProperties = entity.GetType(). GetProperties() .Union(EntityProperties); foreach (var metadata in Metadatas) { if (retVals.ContainsKey(metadata.Key)) { continue; } retVals.Add(metadata.Key, CreateEntityPropertyFromObject(metadata.Value)); } foreach (PropertyInfo property in objectProperties) { // reserved properties if (property.Name == TableConstants.PartitionKey || property.Name == TableConstants.RowKey || property.Name == TableConstants.Timestamp || property.Name == TableConstants.Etag || property.Name == nameof(Entity)) { continue; } // Enforce public getter / setter if (property.GetSetMethod() == null || !property.GetSetMethod().IsPublic || property.GetGetMethod() == null || !property.GetGetMethod().IsPublic) { continue; } var newProperty = CreateEntityPropertyFromObject(property.GetValue(Entity, null), property); // property will be null for unknown type if (newProperty != null && !retVals.ContainsKey(property.Name)) { retVals.Add(property.Name, newProperty); } } return(retVals); }
public ITableEntity BeforeSave(ITableEntity entity) { if (this.BeforeQuery(entity) == null) { return(entity); } var type = entity.GetType(); foreach (var p in type.GetProperties()) { var jsonData = p.GetCustomAttributes(typeof(JsonDataAttribute), true).Cast <JsonDataAttribute>().FirstOrDefault(); if (jsonData == null) { continue; } type.GetProperty(jsonData.PropertyName).SetValue(entity, ToJson(p.GetValue(entity))); } return(entity); }
public ITableEntity BeforeQuery(ITableEntity entity) { if (entity == null) { return(entity); } var type = entity.GetType(); var rowKey = this.RowKey(type); var partitionKey = this.PartitionKey(type); if (!string.IsNullOrWhiteSpace(partitionKey) && type.GetProperty(partitionKey).CanRead) { entity.PartitionKey = (string)type.GetProperty(partitionKey).GetValue(entity); } if (!string.IsNullOrWhiteSpace(rowKey) && type.GetProperty(rowKey).CanRead) { entity.RowKey = (string)type.GetProperty(rowKey).GetValue(entity); } return(entity); }
internal static void ReadAndUpdateTableEntityWithEdmTypeResolver(ITableEntity entity, Dictionary <string, string> entityAttributes, EntityReadFlags flags, Func <string, string, string, string, EdmType> propertyResolver, OperationContext ctx) { Dictionary <string, EntityProperty> dictionary = ((flags & EntityReadFlags.Properties) > (EntityReadFlags)0) ? new Dictionary <string, EntityProperty>() : null; Dictionary <string, EdmType> dictionary2 = null; if (entity.GetType() != typeof(DynamicTableEntity)) { if (!TableEntity.DisablePropertyResolverCache) { dictionary2 = TableEntity.PropertyResolverCache.GetOrAdd(entity.GetType(), CreatePropertyResolverDictionary); } else { Logger.LogVerbose(ctx, "Property resolver cache is disabled."); dictionary2 = CreatePropertyResolverDictionary(entity.GetType()); } } if (flags > (EntityReadFlags)0) { foreach (KeyValuePair <string, string> entityAttribute in entityAttributes) { if (entityAttribute.Key == "PartitionKey") { entity.PartitionKey = entityAttribute.Value; } else if (entityAttribute.Key == "RowKey") { entity.RowKey = entityAttribute.Value; } else if (entityAttribute.Key == "Timestamp") { if ((flags & EntityReadFlags.Timestamp) != 0) { entity.Timestamp = DateTime.Parse(entityAttribute.Value, CultureInfo.InvariantCulture); } } else if ((flags & EntityReadFlags.Properties) > (EntityReadFlags)0) { if (propertyResolver != null) { Logger.LogVerbose(ctx, "Using the property resolver provided via TableRequestOptions to deserialize the entity."); try { EdmType edmType = propertyResolver(entity.PartitionKey, entity.RowKey, entityAttribute.Key, entityAttribute.Value); Logger.LogVerbose(ctx, "Attempting to deserialize '{0}' as type '{1}'", entityAttribute.Key, edmType.GetType().ToString()); try { dictionary.Add(entityAttribute.Key, EntityProperty.CreateEntityPropertyFromObject(entityAttribute.Value, edmType.GetType())); } catch (FormatException innerException) { throw new StorageException(string.Format(CultureInfo.InvariantCulture, "Failed to parse property '{0}' with value '{1}' as type '{2}'", entityAttribute.Key, entityAttribute.Value, edmType.ToString()), innerException) { IsRetryable = false }; } } catch (StorageException) { throw; } catch (Exception innerException2) { throw new StorageException("The custom property resolver delegate threw an exception. Check the inner exception for more details.", innerException2) { IsRetryable = false }; } } else if (entity.GetType() != typeof(DynamicTableEntity)) { Logger.LogVerbose(ctx, "Using the default property resolver to deserialize the entity."); if (dictionary2 != null) { dictionary2.TryGetValue(entityAttribute.Key, out EdmType value); Logger.LogVerbose(ctx, "Attempting to deserialize '{0}' as type '{1}'", entityAttribute.Key, value); dictionary.Add(entityAttribute.Key, EntityProperty.CreateEntityPropertyFromObject(entityAttribute.Value, value)); } } else { Logger.LogVerbose(ctx, "No property resolver available. Deserializing the entity properties as strings."); dictionary.Add(entityAttribute.Key, EntityProperty.CreateEntityPropertyFromObject(entityAttribute.Value, typeof(string))); } } } if ((flags & EntityReadFlags.Properties) > (EntityReadFlags)0) { entity.ReadEntity(dictionary, ctx); } } }
internal static void ReadAndUpdateTableEntityWithEdmTypeResolver(ITableEntity entity, Dictionary <string, string> entityAttributes, EntityReadFlags flags, Func <string, string, string, string, EdmType> propertyResolver, OperationContext ctx) { Dictionary <string, EntityProperty> entityProperties = (flags & EntityReadFlags.Properties) > 0 ? new Dictionary <string, EntityProperty>() : null; Dictionary <string, EdmType> propertyResolverDictionary = null; // Try to add the dictionary to the cache only if it is not a DynamicTableEntity. If DisablePropertyResolverCache is true, then just use reflection and generate dictionaries for each entity. if (entity.GetType() != typeof(DynamicTableEntity)) { #if WINDOWS_DESKTOP && !WINDOWS_PHONE if (!TableEntity.DisablePropertyResolverCache) { propertyResolverDictionary = TableEntity.PropertyResolverCache.GetOrAdd(entity.GetType(), TableOperationHttpResponseParsers.CreatePropertyResolverDictionary); } else { Logger.LogVerbose(ctx, SR.PropertyResolverCacheDisabled); propertyResolverDictionary = TableOperationHttpResponseParsers.CreatePropertyResolverDictionary(entity.GetType()); } #else propertyResolverDictionary = TableOperationHttpResponseParsers.CreatePropertyResolverDictionary(entity.GetType()); #endif } if (flags > 0) { foreach (KeyValuePair <string, string> prop in entityAttributes) { if (prop.Key == TableConstants.PartitionKey) { entity.PartitionKey = (string)prop.Value; } else if (prop.Key == TableConstants.RowKey) { entity.RowKey = (string)prop.Value; } else if (prop.Key == TableConstants.Timestamp) { if ((flags & EntityReadFlags.Timestamp) == 0) { continue; } entity.Timestamp = DateTime.Parse(prop.Value, CultureInfo.InvariantCulture); } else if ((flags & EntityReadFlags.Properties) > 0) { if (propertyResolver != null) { Logger.LogVerbose(ctx, SR.UsingUserProvidedPropertyResolver); try { EdmType type = propertyResolver(entity.PartitionKey, entity.RowKey, prop.Key, prop.Value); Logger.LogVerbose(ctx, SR.AttemptedEdmTypeForTheProperty, prop.Key, type.GetType().ToString()); try { entityProperties.Add(prop.Key, EntityProperty.CreateEntityPropertyFromObject(prop.Value, type.GetType())); } catch (FormatException ex) { throw new StorageException(string.Format(CultureInfo.InvariantCulture, SR.FailParseProperty, prop.Key, prop.Value, type.ToString()), ex) { IsRetryable = false }; } } catch (StorageException) { throw; } catch (Exception ex) { throw new StorageException(SR.PropertyResolverThrewError, ex) { IsRetryable = false }; } } else if (entity.GetType() != typeof(DynamicTableEntity)) { EdmType edmType; Logger.LogVerbose(ctx, SR.UsingDefaultPropertyResolver); if (propertyResolverDictionary != null) { propertyResolverDictionary.TryGetValue(prop.Key, out edmType); Logger.LogVerbose(ctx, SR.AttemptedEdmTypeForTheProperty, prop.Key, edmType); entityProperties.Add(prop.Key, EntityProperty.CreateEntityPropertyFromObject(prop.Value, edmType)); } } else { Logger.LogVerbose(ctx, SR.NoPropertyResolverAvailable); entityProperties.Add(prop.Key, EntityProperty.CreateEntityPropertyFromObject(prop.Value, typeof(string))); } } } if ((flags & EntityReadFlags.Properties) > 0) { entity.ReadEntity(entityProperties, ctx); } } }
private static string BuildMessage(ITableEntity entity) { return($"Entity was changed by someone else.\r\n- Entity type: {entity.GetType().FullName}\r\n- PK: {entity.PartitionKey}\r\n- RK: {entity.RowKey}\r\n- ETag: {entity.ETag}\r\n- Timestamp: {entity.Timestamp}"); }