Omu.ValueInjecter检查属性,然后再进行集合



我需要扩展omu.valueinjecter在进行属性分配之前执行检查。给定下面的代码示例,只有在SETA为true时才应发生道具A的分配。我怀疑这里的loopvalueIntive不是这里的正确基类,但是有人可以纠正下面的代码以便在注射过程中检查seta吗?

var source = new Source() { A = 3 };
var dest = new Dest();
dest.InjectFrom<MyInjector>(source);
public class Source
{
    public int A { get; set; }
    public bool SetA { get; set; }
}
public class Dest
{
    public int A { get; set; }
}
public class MyInjector : LoopValueInjection // or some other base class!
{
    protected override bool AllowSetValue(object value)
    {
        // check SetA!!
        //return base.AllowSetValue(value);
    }
}

好吧,我现在可以正常工作。以下是正确的代码。我错过了uSourceprop的超载,这完全可以解决我的目的。

我试图解决的问题是在将视图模型发布到操作的情况下,您必须将视图模型数据复制到数据模型中。初始化数据模型时,可能会设置某些默认值。当注入视图模型时,这些默认值将被覆盖。如果设置了视图模型属性,则覆盖这些内容是正确的,但是我的默认值被视图模型值覆盖,而这些值未从后操作中设置。

解决方案是在视图模型中放置一个标志,以指示是否设置了属性。和每个属性的设置器我只是在基类中更新了一个公共列表字符串对象。

在以下代码中,在usesourceprop方法中,您可以看到,如果在setProperties中不存在正在处理的属性名称,则该方法返回false且未设置属性。

var source = new Source() { A = 3 };
var dest = new Dest();
dest.InjectFrom<MyInjector>(source);
public class Source
{
    public int A { get; set; }
    public bool SetA { get; set; }
}
public class Dest
{
    public int A { get; set; }
}
public class MyInjector : LoopValueInjection // or some other base class!
{
    protected override void Inject(object source, object target)
    {
        if (source is BaseEntityViewModel) _baseEntityViewModel = (BaseEntityViewModel)source;
        base.Inject(source, target);
    }
    protected override bool UseSourceProp(string sourcePropName)
    {
        if (_baseEntityViewModel is BaseEntityViewModel)
            return _baseEntityViewModel.SetProperties.Contains(sourcePropName);
        else
            return base.UseSourceProp(sourcePropName);
    }
}

我认为覆盖SetValue方法可能是您需要的。这是对文档的稍作修改:http://valueinjecter.codeplex.com/wikipage?title=getting started&amp; referringtitle = documentation and http://valueinignter.codeplex.com/discussions/discussions/3555101

>>
public class MyInjector : LoopValueInjection
{       
    //by default is return sourcePropertyValue; override to change behaviour 
    protected override object SetValue(ConventionInfo c)
    {
        // this is just a sample, but you could write anything here
        return new Dest 
        { 
            //Check if source value is true and only then set property
            if(c.SourceProp.Name == "SetA")
            {
              var setASourceVal = c.TargetProp.Value As bool;
              if(setASourceVal)
              {
                A = sourcePropertyValue;
              }
            }
        }
    }
}

取决于您使用哪种注入,对于惯例,您在匹配方法中具有价值https://valueinjecter.codeplex.com/wikipage?title=step By By Step Explanation&amp; referringTitle = home

对于loopvalueInctive,您可以覆盖AllowSetValue

最新的(最快)注射是:https://valueinjecter.codeplex.com/wikipage?title=smartConcentenventionInjection&amp; referferringTitle = home

与"约定提示"相比,它具有一个限制,您没有匹配方法中的源和目标属性的值,但是在setValue方法中,它们可以取消该值的设置,如果您将false设置为ref参数setValue

相关内容

最新更新