How can I inspect the attributes of a target property using Value Injector?
-
26-05-2021 - |
Question
I'm using Value Injector to manage my mappings in an ASP.NET MVC project and it's been great so far. The domain has the concept of a length measurement stored as standard metric units in the db and exposed as a decimal value up to the service layer.
Rendering of lengths in the UI context-specific, depending on object being measure, user culture, etc. Hints about context denoted by attributes on the properties of view model types. Using Value Injector, I want to inspect these attributes at injection time and show an appropriately formatted string to display, when the source property is a decimal and the target property is a string decorated with one of the above attributes.
namespace TargetValueAttributes
{
public class Person
{
public decimal Height { get; set; }
public decimal Waist { get; set; }
}
public class PersonViewModel
{
[LengthLocalizationHint(LengthType.ImperialFeetAndInches)]
[LengthLocalizationHint(LengthType.MetricMeters)]
public string Height { get; set; }
[LengthLocalizationHint(LengthType.ImperialInches)]
[LengthLocalizationHint(LengthType.MetricCentimeters)]
public string Waist { get; set; }
}
public enum LengthType
{
MetricMeters,
MetricCentimeters,
ImperialFeetAndInches,
ImperialInches
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class LengthLocalizationHintAttribute : Attribute
{
public LengthType SuggestedLengthType { get; set; }
public LengthLocalizationHintAttribute(LengthType suggestedLengthType)
{
SuggestedLengthType = suggestedLengthType;
}
}
public class LengthLocalizationInjection : FlatLoopValueInjection<decimal, string>
{
protected override void Inject(object source, object target)
{
base.Inject(source, target);//I want to be able to inspect the attributes on the target value here
}
protected override string SetValue(decimal sourceValues)
{
var derivedLengthType = LengthType.MetricMeters;//here would be even better
return sourceValues.ToLength(derivedLengthType);//this is an extension method that does the conversion to whatever the user needs to see
}
}
La solution
After poking around in the source, I came up with a solution based on the implementation of `FlatLoopValueInjection'.
public abstract class LocalizationStringInjection<TSource, TTarget> : LoopValueInjectionBase
{
public ILocalizationContext LocalizationContext { get; set; }
protected LocalizationStringInjection(ILocalizationContext localizationContext)
{
LocalizationContext = localizationContext;
}
protected virtual bool TypesMatch(Type sourceType, Type targetType)
{
return sourceType == typeof(TSource) && targetType == typeof(TTarget);
}
protected override void Inject(object source, object target)
{
foreach (PropertyDescriptor targetPropertyDescriptor in target.GetProps())
{
var t1 = targetPropertyDescriptor;
var es = UberFlatter.Flat(targetPropertyDescriptor.Name, source, type => TypesMatch(type, t1.PropertyType));
var endpoint = es.FirstOrDefault();
if (endpoint == null) continue;
var sourceValue = endpoint.Property.GetValue(endpoint.Component) is TSource ? (TSource)endpoint.Property.GetValue(endpoint.Component) : default(TSource);
if (AllowSetValue(sourceValue))
targetPropertyDescriptor.SetValue(target, SetValue(sourceValue, targetPropertyDescriptor));
}
}
protected abstract TTarget SetValue(TSource sourcePropertyValue, PropertyDescriptor targetPropertyDescriptor);
}
public class LengthLocalizationStringInjection : LocalizationStringInjection<decimal, string>
{
public LengthLocalizationStringInjection(ILocalizationContext localizationContext) : base(localizationContext) { }
protected override string SetValue(decimal sourceValue, PropertyDescriptor targetPropertyDescriptor)
{
var lengthHints = targetPropertyDescriptor.Attributes.Cast<object>().Where(attribute => attribute.GetType() == typeof(LengthLocalizationAttribute)).Cast<LengthLocalizationAttribute>().ToList();
return lengthHints.Any() ? sourceValue.ToLength(lengthHints.First(l => l.SuggestedLengthType == LocalizationContext.Length).SuggestedLengthType) : sourceValue.ToLength(default(LengthType));
}
}
This has proven good enough for my purposes for now. I've left out some referenced types so as not to obscure the idea.