private static object ToSagaData(Type sagaDataType, DictionaryTableEntity entity) { var toCreate = Activator.CreateInstance(sagaDataType); foreach (var accessor in GetPropertyAccessors(sagaDataType)) { if (!entity.ContainsKey(accessor.Name)) { continue; } var value = entity[accessor.Name]; var type = accessor.PropertyType; if (type == typeof(byte[])) { accessor.Setter(toCreate, value.BinaryValue); } else if (TrySetNullable(value, toCreate, accessor)) { } else if (type == typeof(string)) { accessor.Setter(toCreate, value.StringValue); } else { if (value.PropertyType == EdmType.String) { // possibly serialized JSON.NET value try { using (var stringReader = new StringReader(value.StringValue)) { var deserialized = jsonSerializer.Deserialize(stringReader, type); accessor.Setter(toCreate, deserialized); } } catch (Exception) { throw new NotSupportedException($"The property type '{type.Name}' is not supported in Azure Table Storage and it cannot be deserialized with JSON.NET."); } } else { throw new NotSupportedException($"The property type '{type.Name}' is not supported in Azure Table Storage"); } } } return(toCreate); }
public static object ToEntity(Type entityType, DictionaryTableEntity entity) { var toCreate = Activator.CreateInstance(entityType); foreach (var propertyInfo in entityType.GetProperties()) { if (entity.ContainsKey(propertyInfo.Name)) { var value = entity[propertyInfo.Name]; var type = propertyInfo.PropertyType; if (type == typeof(byte[])) { propertyInfo.SetValue(toCreate, value.BinaryValue, null); } else if (TrySetNullable(value, toCreate, propertyInfo)) { } else if (type == typeof(string)) { propertyInfo.SetValue(toCreate, value.StringValue, null); } else { if (value.PropertyType == EdmType.String) { // possibly serialized JSON.NET value try { using (var stringReader = new StringReader(value.StringValue)) { var deserialized = jsonSerializer.Deserialize(stringReader, type); propertyInfo.SetValue(toCreate, deserialized, null); } } catch (Exception) { throw new NotSupportedException($"The property type '{type.Name}' is not supported in Azure Table Storage and it cannot be deserialized with JSON.NET."); } } else { throw new NotSupportedException($"The property type '{type.Name}' is not supported in Azure Table Storage"); } } } } return toCreate; }
public static DictionaryTableEntity ToDictionaryTableEntity(object sagaData, DictionaryTableEntity toPersist) { foreach (var accessor in GetPropertyAccessors(sagaData.GetType())) { var name = accessor.Name; var type = accessor.PropertyType; var value = accessor.Getter(sagaData); if (type == typeof(byte[])) { toPersist[name] = new EntityProperty((byte[])value); } else if (TryGetNullable(type, value, out bool? @bool)) { toPersist[name] = new EntityProperty(@bool); } else if (TryGetNullable(type, value, out DateTime? dateTime)) { if (!dateTime.HasValue || dateTime.Value < StorageTableMinDateTime) { throw new Exception($"Saga data of type '{sagaData.GetType().FullName}' with DateTime property '{name}' has an invalid value '{dateTime}'. Value cannot be null and must be equal to or greater than '{StorageTableMinDateTime}'."); } toPersist[name] = new EntityProperty(dateTime); } else if (TryGetNullable(type, value, out Guid? guid)) { toPersist[name] = new EntityProperty(guid); } else if (TryGetNullable(type, value, out int? @int)) { toPersist[name] = new EntityProperty(@int); } else if (TryGetNullable(type, value, out long? @long)) { toPersist[name] = new EntityProperty(@long); } else if (TryGetNullable(type, value, out double? @double)) { toPersist[name] = new EntityProperty(@double); } else if (type == typeof(string)) { toPersist[name] = new EntityProperty((string)value); } else { using (var sw = new StringWriter()) { try { jsonSerializerWithNonAbstractDefaultContractResolver.Serialize(sw, value, type); } catch (Exception) { throw new NotSupportedException($"The property type '{type.Name}' is not supported in Azure Table Storage and it cannot be serialized with JSON.NET."); } toPersist[name] = new EntityProperty(sw.ToString()); } } } return(toPersist); }
public static TEntity ToSagaDataNew <TEntity>(DictionaryTableEntity entity) { return((TEntity)ToSagaData(typeof(TEntity), entity)); }
public static TEntity ToEntity<TEntity>(DictionaryTableEntity entity) { return (TEntity)ToEntity(typeof(TEntity), entity); }