1

解決策は盲目的に明白だと確信していますが、値インジェクターを使用して次のことを行う方法についてのアイデアはありますか?

次のモデルがあるとします。

public class Foo{
    public int BarId{get;set;}
    public Bar Bar{get;set;}
}

public class Bar{
    public int Id{get;set;}
    [Required]
    public string Description{get;set;}
}

そして、次のようなビュー モデル:

public class FooBarViewModel{
    public int BarId{get;set;}
    public bool EditMode{get;set;}
}

オブジェクトを呼び出すInjectFrom<UnflatLoopValueInjection>()と、プロパティではなく、プロパティのみが設定されます。プロパティ名と完全に一致するものがグラフの浅い深さで見つかった場合、オブジェクト グラフ全体を再帰する Unflattening プロセスを、可能であれば停止したいと考えています。FooFoo.BarIdFoo.Bar.Id

理想的には、プロパティを明示的に無視し、慣例によりこれを行うことができるようにすることなく、これを行いたいと考えています。

4

1 に答える 1

0

ソースをもう少し掘り下げた結果、実装するのは簡単だと思ったので、答えは

public class UnflatLoopValueInjectionUseExactMatchIfPresent : UnflatLoopValueInjection {
    protected override void Inject(object source, object target) {
        var targetProperties = target.GetProps().Cast<PropertyDescriptor>().AsQueryable();
        foreach (PropertyDescriptor sourceProp in source.GetProps()) {
            var prop = sourceProp;
            if (targetProperties.Any(p => p.Name == prop.Name)) {
                //Exact match found
                targetProperties.First(p => p.Name == prop.Name).SetValue(target, SetValue(sourceProp.GetValue(source)));
            }
            else {
                //Fall back to UnflatLoopValueInjection
                var endpoints = UberFlatter.Unflat(sourceProp.Name, target, t => TypesMatch(prop.PropertyType, t)).ToList();
                if (!endpoints.Any()) {
                    continue;
                }
                var value = sourceProp.GetValue(source);
                if (!AllowSetValue(value)) {
                    continue;
                }
                foreach (var endpoint in endpoints) {
                    endpoint.Property.SetValue(endpoint.Component, SetValue(value));
                }
            }
        }
    }
}

上記のインジェクションは、viewmodels プロパティ名と完全に一致するものが見つからない場合にのみ、(標準の UnflatLoopValueInjection のロジックを使用して) オブジェクト グラフにドリルダウンします。

于 2013-07-24T15:21:35.380 に答える