有人能告诉我它们之间的优缺点吗(性能、功能、API使用、可扩展性、测试)?
作为ValueInjecter的创造者,我可以告诉你我的初衷是想要一个简单且非常灵活的东西。
我真的不喜欢写很多代码或写出很多猴子代码
,比如:
Prop1.Ignore, Prop2.Ignore etc.
CreateMap<Foo,Bar>(); CreateMap<Tomato, Potato>(); etc.
ValueInjecter类似于拥有插件的Mozilla,您可以创建ValueInjections并使用它们。
内置注入用于平铺、反平铺,并且有些注入是用于继承的。
它的工作方式更像是一种方面,您不必指定所有属性1对1,而是执行以下操作:
从源中获取名称以“Id”结尾的所有int属性,转换值并将每个值设置为源对象中具有相同名称但没有Id后缀并且其类型继承自Entity的属性,例如这样的内容。
因此,一个明显的区别是,ValueInjecter甚至在窗体中进行平铺和反平铺操作,这就是它的灵活性所在。
(从对象映射到窗体控件并返回)
Automapper不能在Windows Forms中使用,无法反平铺,但它具有良好的集合映射等功能,因此如果您需要与ValueInjecter一起使用,则只需执行以下操作:
foos.Select(o => new Bar().InjectFrom(o));
您还可以使用ValueInjecter将匿名和动态对象映射到对象。
区别:
target.InjectFrom<FlatLoopValueInjection>(source); also <UnflatLoopValueInjection>
。如果您想要从Foo.Bar.Name的String类型映射到FooBarName的Class1类型,则可以继承FlatLoopValueInjection并指定此内容。由于我从未使用过其他工具,所以我只能谈谈AutoMapper。我在构建AutoMapper时有几个目标:
如果你想做这些事情,AutoMapper非常适合你。AutoMapper做不好的事情是:
原因是我从来没有需要做这些事情。在很大程度上,我们的实体没有设置器,也没有暴露集合等等,这就是为什么它不在那里的原因。我们使用AutoMapper将数据展开到DTO并从UI模型映射到命令消息等等。在这方面,它对我们非常非常好。
我尝试了两者并更喜欢ValueInjecter,因为它非常简单:
myObject.InjectFrom(otherObject);
对于我大部分注入需求而言,这就是需要知道的全部。没有比这更简单和优雅的了。
InjectFrom()
扩展方法。 - jgauffin由entityframework(模型优先)创建的模型:
public partial class Family
{
public int Id { get; set; }
public string FamilyName { get; set; }
public virtual Address Address { get; set; }
}
public partial class Address
{
public int Id { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string TownCity { get; set; }
public string County { get; set; }
public string Postcode { get; set; }
public virtual Family Family { get; set; }
}
ViewModel(我可以用验证器来修饰):
public class FamilyViewModel
{
public int Id { get; set; }
public string FamilyName { get; set; }
public int AddressId { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressTownCity { get; set; }
public string AddressCounty { get; set; }
public string AddressPostcode { get; set; }
}
视图控制器:
//
// GET: /Family/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Family/Create
[HttpPost]
public ActionResult Create(FamilyViewModel familyViewModel)
{
try
{
Family family = new Family();
family.InjectFrom<UnflatLoopValueInjection>(familyViewModel);
db.Families.Add(family);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
在我看来,这再简单不过了?