我正在玩 valueinjecter 并想知道当视图模型具有视图模型集合时如何对域进行视图模型?
说我有这个域
public class MyDomain
{
public IList<MyOtherDomain> MyOtherDomains {get; set;}
}
public class MyOtherDomain
{
public string Name {get; set;}
}
public class MyMasterVM
{
public IList<MyOtherVm> MyOtherDomains {get; set;}
}
public class MyOtherVm
{
public string Name {get; set;}
}
现在我该如何注射?我是否需要使用 valueinjector 手动映射这些?
public ActionResult Method(MyMasterVM vm)
{
MyDomain d = new MyDomain();
d.InjectFrom<UnflatLoopValueInjection>(vm);
}
编辑
在玩了一些之后,我让模拟器开始工作。但是我的与测试中的不同
// sample test
public class FooBar : TypeMapper<Foo, Bar>
{
public override Bar Map(Foo source, Bar target)
{
base.Map(source, target);
target.NoConvention = source.Name + source.Xyz + source.Props;
return target;
}
}
[Test]
public void MapShouldMapCollectionPropertiesAndUseFooBarTypeMapper()
{
MapperFactory.AddMapper(new FooBar());
var foo = new Foo
{
Foos = new List<Foo>
{
new Foo{Name = "f1",Props = "v",Xyz = 19},
new Foo{Name = "f2",Props = "i",Xyz = 7},
new Foo{Name = "f3",Props = "v",Xyz = 3},
}
};
var bar = Mapper.Map<Foo, Bar>(foo);
Assert.AreEqual(foo.Foos.Count(), bar.Foos.Count());
var ffoos = foo.Foos.ToArray();
var bfoos = bar.Foos.ToArray();
for (var i = 0; i < ffoos.Count(); i++)
{
Assert.AreEqual(ffoos[i].Name, bfoos[i].Name);
Assert.AreEqual(ffoos[i].Name + ffoos[i].Xyz + ffoos[i].Props, bfoos[i].NoConvention);
}
}
// mine
public class Test : TypeMapper<IList<MyOtherVm>, IList<MyOtherDomain>>
{
public override IList<MyOtherDomain> Map(IList<MyOtherVm> source, IList<MyOtherDomain> target)
{
// not sure if I always have to call base
// mapping would happen here.
return base.Map(source, target);
}
}
MapperFactory.AddMapper(new Test());
IList<MyOtherDomain> otherDomains= new List<MyOtherDomain>();
MapperVj.Map(vm.MyOtherDomains , otherDomains);
我必须指定它是一个 IList 否则它似乎永远不会进入我的重写方法。