private static void Transform <T>(T objectToTransform, string key, TransformValueManager valueManager) { var typeOfTClass = typeof(T); if (key == null) { key = typeOfTClass.Name; } var properties = typeOfTClass.GetProperties(); foreach (var propertyInfo in properties) { var transformationAttribute = propertyInfo.GetCustomAttributes(true).OfType <TransformValueAttribute>().FirstOrDefault(); if (transformationAttribute == null) { continue; } var fieldType = ResolveFieldType(transformationAttribute.FieldType, propertyInfo.PropertyType); var newValue = RandomValueGenerator.Generate(fieldType); var propertyKey = GeneratePropertyKey(key, propertyInfo.Name); var property = new TransformedProperty(propertyKey, propertyInfo.GetValue(objectToTransform), newValue); valueManager.Add(property); propertyInfo.SetValue(objectToTransform, newValue); } }
private void GuardAgainstDuplicatedEntity(TransformedProperty property) { if (EntityWithKeyHasBeenAddedBefore(property)) { throw new Exception($"Duplicate key detected : {property.Key}"); } }
public void Add(TransformedProperty property) { GuardAgainstDuplicatedEntity(property); _entities.Add(property); }
private bool EntityWithKeyHasBeenAddedBefore(TransformedProperty property) { return(_entities.Any(a => a.SameKeyAs(property))); }
public bool SameKeyAs(TransformedProperty property) { return(this.Key == property.Key); }