首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >AutoMapper vs ValueInjecter

AutoMapper vs ValueInjecter
EN

Stack Overflow用户
提问于 2011-01-12 06:49:46
回答 2查看 53.7K关注 0票数 209

每次我在StackOverflow上寻找AutoMapper的东西时,我都会读到一些关于ValueInjecter的东西。

谁能告诉我它们之间的优缺点(性能、功能、API使用、可扩展性、测试)?

EN

回答 2

Stack Overflow用户

发布于 2011-01-13 05:07:04

我都试过了,而且更喜欢ValueInjecter,因为它太简单了:

myObject.InjectFrom(otherObject);

对于我的绝大多数注入需求,这就是我要知道的全部。它不可能比这更简单和优雅了。

票数 55
EN

Stack Overflow用户

发布于 2011-04-15 11:31:24

这也是我一直在研究的一个问题,对于我的用例来说,这似乎是一个很简单的赋值注入器。它不需要预先设置就可以使用(我猜可能会影响性能,尽管如果实现得当,它可以缓存映射以供将来调用,而不是每次都反映),所以在使用它们之前不需要预定义任何映射。

然而,最重要的是,它允许反向映射。现在我可能遗漏了一些东西,因为吉米提到他认为没有必要的用例,所以可能我的模式是错误的,但我的用例是从我的对象关系管理创建一个ViewModel对象。然后我在我的网页上显示这个。用户完成后,我将ViewModel作为httppost返回,如何将其转换回原始的ORM类?我很想知道automapper的模式。对于ValueInjector,它是微不足道的,甚至会变得不扁平化。例如,创建一个新实体

entityframework创建的模型(model first):

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; }
}

ViewController:

    //
    // 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();
        }
    }

在我看来,没有比这更简单的了吗?

(所以这就回避了一个问题,我遇到的这个模式有什么问题(似乎很多人都是这样做的),它对AutoMapper没有价值吗?)

然而,如果这个模式是你想要使用的,那么我的投票是valueinjecter by country英里。

票数 27
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4663577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档