ValueInjecter 将 ViewModel 映射到域
我正在使用 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,否则它似乎永远不会进入我的重写方法。
I am playing around with valueinjecter and wondering how do I do viewmodels to domains when the view model has a collection of viewmodels?
Say I have this domain
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;}
}
now how do I do my injecting? Do I need to manually map these with valueinjector?
public ActionResult Method(MyMasterVM vm)
{
MyDomain d = new MyDomain();
d.InjectFrom<UnflatLoopValueInjection>(vm);
}
Edit
After some playing around I got the simulator to work. However mine is different than the one in the tests
// 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);
I have to specify it is a IList otherwise it never seems to go into my overridden method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要实现自己的自定义
IValueInjection
注入器。我的这个答案也是基于您的MyDomain
类中的一个拼写错误。拼写错误的假设:
而不是
所以,客户注入类可能看起来像这样(如果有更好的方法,则不是 100%)
在您的控制器中,您几乎可以像上面指定的那样使用它(只需更改注入器类型)
I think you'd need to implement your own custom
IValueInjection
injector. I am also basing this answer on a typo in yourMyDomain
class.Assumption of typo:
and not
So, the customer injection class could look like this (not 100% if there's a better way to do it)
In your controller, you can use it almost like you specified above (just change the injector type)
一种快速简单的方法是执行此操作:
编辑:
我做了一个新的测试项目,您可以下载(它也使用您的课程)。
在这里获取它: http://valueinjecter.codeplex.com/releases/view/60311 #DownloadId=318259
one quick and easy way would be to do this:
edit:
I did a new test project that you can download (it uses your classes also).
get it here: http://valueinjecter.codeplex.com/releases/view/60311#DownloadId=318259