简体   繁体   English

如何使用ValueInjecter控制展开的深度

[英]How to control depth of unflattening using ValueInjecter

Im sure the solution is blindingly obvious but any ideas on how i would do the following with value injecter? 我肯定解决方案是显而易见的,但是关于如何使用价值注入器进行以下操作的任何想法?

Say you have the following model: 假设您有以下模型:

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

and a view model that looks like this: 以及如下所示的视图模型:

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

when i call InjectFrom<UnflatLoopValueInjection>() on the Foo object i only want the Foo.BarId property populated, not the Foo.Bar.Id property as well. 当我在Foo对象上调用InjectFrom<UnflatLoopValueInjection>()时,我只希望填充Foo.BarId属性,也不希望填充Foo.Bar.Id属性。 I would like to if possible stop the Unflattening process from recursing through the whole object graph if an exact match to a property name is found at a shallower depth in the graph. 如果在图中较浅的深度发现了与属性名称的精确匹配,我想尽可能地阻止整个对象图的“重新展平”过程。

Ideally i would like to do this without resorting to explicitly ignoring properties and being able to do this by convention. 理想情况下,我希望执行此操作而不必诉诸于显式地忽略属性并能够按照惯例执行此操作。

Having dug into the source a bit more the answer was as i suspected trivial to implement 挖了更多的答案,因为我怀疑实现起来很简单

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));
                }
            }
        }
    }
}

The above injection will only drill down deeper (using the logic from the standard UnflatLoopValueInjection) into the object graph if an exact match for the viewmodels property name is not found. 如果未找到与viewmodels属性名称完全匹配的结果,则上述注入将仅在对象图中更深入(使用标准UnflatLoopValueInjection的逻辑)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM