在ValueInjecter的ConventionInjection(s)中指定多个规则

时间:2013-05-15 10:21:49

标签: c# .net valueinjecter

我是ValueInjecter的新手。我知道如何匹配具有相同名称但具有不同上限的属性:

public class IgnoreCaseInjection : ConventionInjection
{
     protected override bool Match(ConventionInfo c)
     {
         return String.Compare(c.SourceProp.Name, c.TargetProp.Name, 
                               StringComparison.OrdinalIgnoreCase) == 0;
     }
}

var foo = new Foo() { ID = 1};
var bar = new Bar();
bar.InjectFrom<IgnoreCaseInjection>(foo);

这会将foo.ID映射到bar.Id。如果我有另一个使用不同规则映射的属性怎么办?例如,我还有foo.MyProp(这是类型为FooEnum的可枚举),我想要映射到bar.MyProp这是一个字符串(我的意思是它存储.ToString()表示枚举)。

如何向转换器添加其他规则?代码将如何?

2 个答案:

答案 0 :(得分:0)

到目前为止,我只在ValueInjecter's documentation中找到了解决方案:

viewModel.InjectFrom(entity)
                .InjectFrom<CountryToLookup>(entity)
                .InjectFrom<AnythingElseYouMightImagine>(entity)
                .InjectFrom(new StuffInjection(stuffRepository), anotherEntity); 

您必须对每个类进行编码并确保它们不重叠

答案 1 :(得分:0)

你可以使用|| &安培;&安培;在Match方法中添加其他规则,但在这种特定情况下,我会创建另一个注入,如下所示:

https://valueinjecter.codeplex.com/wikipage?title=Useful%20injections&referringTitle=Home

在本页^您可以看到EnumToInt和IntToEnum,您可以修改它并执行EnumToStr