/// <summary> /// Fill the specified property with the result of the specified function /// </summary> /// <typeparam name="T1">The target object type</typeparam> /// <typeparam name="T2">The target property type</typeparam> /// <param name="expression">The target property</param> /// <param name="filler">A function that will return a property value</param> /// <returns>A configurator for the target object type</returns> public GenFuConfigurator Fill <T1, T2>(Expression <Func <T1, T2> > expression, Func <T2> filler) { PropertyInfo propertyInfo = (expression.Body as MemberExpression).Member as PropertyInfo; CustomFiller <T2> customFiller = new CustomFiller <T2>(_genfu, propertyInfo.Name, typeof(T1), filler); _fillerManager.RegisterFiller(customFiller); return(this); }
/// <summary> /// Populate the specified property with a date in the future /// </summary> /// <typeparam name="T">The target object type</typeparam> /// <param name="configurator"></param> /// <returns>A configurator for the specified object type</returns> public static GenFuConfigurator <T> AsFutureDate <T>(this GenFuDateTimeConfigurator <T> configurator) where T : new () { CustomFiller <DateTime> filler = new CustomFiller <DateTime>(configurator.PropertyInfo.Name, typeof(T), () => CalendarDate.Date(DateRules.FutureDates)); configurator.Maggie.RegisterFiller(filler); return(configurator); }
public IPropertyFiller GetFiller(PropertyInfo propertyInfo) { IPropertyFiller result = null; Type objectType = propertyInfo.DeclaringType; while (objectType != null && result == null) { //First try to get a specific filler based on a full type name (including namespace) string fullTypeName = objectType.FullName.ToLowerInvariant(); if (_specificPropertyFillersByObjectType.ContainsKey(fullTypeName)) { IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[fullTypeName]; result = GetMatchingPropertyFiller(propertyInfo, propertyFillers); } //Second try to get a more generic filler based on only the class name (no namespace) if (result == null) { string classTypeName = objectType.Name.ToLowerInvariant(); if (_specificPropertyFillersByObjectType.ContainsKey(classTypeName)) { IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[classTypeName]; result = GetMatchingPropertyFiller(propertyInfo, propertyFillers); } } objectType = objectType.GetTypeInfo().BaseType; } if (result == null) { //Finally, grab a generic property filler for that property type if (_genericPropertyFillersByPropertyType.ContainsKey(propertyInfo.PropertyType)) { result = _genericPropertyFillersByPropertyType[propertyInfo.PropertyType]; } else if (propertyInfo.PropertyType.GetTypeInfo().BaseType == typeof(System.Enum)) { result = new EnumFiller(propertyInfo.PropertyType); } else { //TODO: Can we build a custom filler here for other value types that we have not explicitly implemented (eg. long, decimal, etc.) result = new CustomFiller <object>("*", typeof(object), true, () => null); _genericPropertyFillersByPropertyType.Add(propertyInfo.PropertyType, result); } } return(result); }
public static GenFuConfigurator <T> AsPlaceholderImage <T>(this GenFuStringConfigurator <T> configurator, int width, int height, string text = null, string backgroundColor = null, string textColor = null, ImgFormat format = ImgFormat.GIF) where T : new() { CustomFiller <string> filler = new CustomFiller <string>(configurator.PropertyInfo.Name, typeof(T), () => PlaceholditUrlBuilder.UrlFor(width, height, text, backgroundColor, textColor, format)); configurator.Maggie.RegisterFiller(filler); return(configurator); }
public static GenFuConfigurator <T> AsPlaceholderImage <T>(this GenFuStringConfigurator <T> configurator, int width, int height, string text = null, string backgroundColor = null, string textColor = null, object htmlAttributes = null, ImgFormat format = ImgFormat.GIF) where T : new() { CustomFiller <string> filler = new CustomFiller <string>(configurator.PropertyInfo.Name, typeof(T), () => new StringBuilder() .Append("http://placehold.it/") .Append($"{width}x{height}") .AppendWhen($".{format}", format != ImgFormat.GIF) .AppendWhen($"/{backgroundColor}", !string.IsNullOrWhiteSpace(backgroundColor)) .AppendWhen($"/{textColor}", !string.IsNullOrWhiteSpace(textColor)) .AppendWhen($"?text={text}", !string.IsNullOrWhiteSpace(text)) .ToString()); configurator.Maggie.RegisterFiller(filler); return(configurator); }
public IPropertyFiller GetFiller(PropertyInfo propertyInfo) { rwl.EnterReadLock(); var newRegistrations = new Dictionary <Type, IPropertyFiller>(); IPropertyFiller result = null; try { Type objectType = propertyInfo.DeclaringType; while (objectType != null && result == null) { //First try to get a specific filler based on a full type name (including namespace) string fullTypeName = objectType.FullName.ToLowerInvariant(); if (_specificPropertyFillersByObjectType.ContainsKey(fullTypeName)) { IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[fullTypeName]; result = GetMatchingPropertyFiller(propertyInfo, propertyFillers); } //Second try to get a more generic filler based on only the class name (no namespace) if (result == null) { string classTypeName = objectType.Name.ToLowerInvariant(); if (_specificPropertyFillersByObjectType.ContainsKey(classTypeName)) { IDictionary <string, IPropertyFiller> propertyFillers = _specificPropertyFillersByObjectType[classTypeName]; result = GetMatchingPropertyFiller(propertyInfo, propertyFillers); } } objectType = objectType.GetTypeInfo().BaseType; } if (result == null) { //Finally, grab a generic property filler for that property type if (_genericPropertyFillersByPropertyType.ContainsKey(propertyInfo.PropertyType)) { result = _genericPropertyFillersByPropertyType[propertyInfo.PropertyType]; } else if (propertyInfo.PropertyType.GetTypeInfo().BaseType == typeof(System.Enum)) { result = new EnumFiller(this.GenFu, propertyInfo.PropertyType); } else { //TODO: Can we build a custom filler here for other value types that we have not explicitly implemented (eg. long, decimal, etc.) result = new CustomFiller <object>(GenFu, "*", typeof(object), true, () => null); newRegistrations[propertyInfo.PropertyType] = result; } } } finally { rwl.ExitReadLock(); } if (newRegistrations.Any()) { rwl.EnterWriteLock(); foreach (var newRegistration in newRegistrations) { _genericPropertyFillersByPropertyType[newRegistration.Key] = newRegistration.Value; } rwl.ExitWriteLock(); } return(result); }