1
我正在使用Value Injector來管理ASP.NET MVC項目中的映射,迄今爲止它一直很棒。該域具有長度度量的概念,在數據庫中存儲爲標準度量單位,並以小數值形式顯示到服務層。如何使用Value Injector檢查目標屬性的屬性?
在UI上下文特定的情況下呈現長度,具體取決於作爲度量的對象,用戶區域等。關於由視圖模型類型的屬性上的屬性表示的上下文提示。使用值注入器,我想在注入時檢查這些屬性,並顯示一個適當格式的字符串來顯示,當源屬性是一個小數,目標屬性是用上述屬性之一裝飾的字符串。
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
}
}