1

私はValueInjectorを使用してASP.NETMVCプロジェクトのマッピングを管理していますが、これまでのところすばらしいです。ドメインには、データベースに標準のメートル単位として保存され、サービスレイヤーまで10進値として公開される長さ測定の概念があります。

測定対象のオブジェクト、ユーザーカルチャなどに応じて、UIコンテキスト固有の長さのレンダリング。ビューモデルタイプのプロパティの属性によって示されるコンテキストに関するヒント。Value Injectorを使用して、インジェクション時にこれらの属性を検査し、適切にフォーマットされた文字列を表示して、ソースプロパティが10進数で、ターゲットプロパティが上記の属性のいずれかで装飾された文字列である場合に表示したいと思います。

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
        }
    }
4

1 に答える 1

1

ソースを調べた後、`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));
    }
}

これは今のところ私の目的には十分であることが証明されています。アイデアを曖昧にしないために、いくつかの参照型を省略しました。

于 2012-03-26T19:40:20.670 に答える