Automapper、Valuinjector 还是手动映射哪个更快?每个速度快到什么程度?

发布于 2024-12-15 09:48:03 字数 1395 浏览 1 评论 0原文

假设我的 DAL(ORM 等)中有这个对象

public class Student
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string Phone {get;set;}
   public Parent Parent {get;set;}
}
public class Parent
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string Phone {get;set;}
}

并且我有一个看起来像这样的 ViewModel

public class StudentDetailVM
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string Phone {get;set;}

   public string ParentName {get;set;}
   public string ParentPhone {get;set;}
}

在这种情况下我需要展平这些对象。我可以使用 Automapper、ValueInjector 等工具来完成此操作,也可以手动完成此操作。如果有很多这样的类需要处理,那么这将是一项乏味的工作,但是这三种方法之间似乎存在性能/开发人员效率的权衡。

我正在寻找有关何时使用 AutomapperValueinjector 与手动映射。我确信手动映射是最快的,但是快多少呢?

  1. 某些场景是否比其他场景慢/快得多(例如扁平化等)?

  2. 采用混合方法在层之间映射对象是否有意义?

我之所以问这个问题,是因为创建了一个名为 emitmapper 的 Codeplex 项目来解决 automapper 中的性能问题,我记得看到一条评论说 automapper 可能需要 0.5 毫秒来映射一个大类。 (需要参考)

我还记得看过一篇文章,描述了如果加载时间在 70 毫秒内(而不是 90 毫秒或更长),用户将有更高的机会留在您的网站上。 (我也在寻找这个链接)。如果自动映射器消耗了我的大部分页面加载时间,再加上网络延迟,那么我认为有可能不使用自动映射器并为我的大容量页面创建手动类并坚持使用混合方法。

底线:我会自己运行测试,但我对 .NET 内部结构了解不够,无法创建可用作可重用指南的准确结果。

Suppose I have this object in my DAL (ORM etc)

public class Student
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string Phone {get;set;}
   public Parent Parent {get;set;}
}
public class Parent
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string Phone {get;set;}
}

And I have a ViewModel that looks like this

public class StudentDetailVM
{
   public string Name {get;set;}
   public string Address {get;set;}
   public string Phone {get;set;}

   public string ParentName {get;set;}
   public string ParentPhone {get;set;}
}

In that case I need to flatten the objects. I can do this with a tool like Automapper, ValueInjector, or I could do it manually. This is tedious work if there are many such classes to handle, but there appears to be a performance / developer efficiency tradeoff between all three approaches.

I'm looking for guidance on when to use Automapper vs Valueinjector vs a manual mapping. I'm sure manual mapping is the fastest one, but by how much?

  1. Are some scenarios much slower/faster than others (e.g. flattening, etc)?

  2. Would it make sense to do a hybrid approach to mapping objects between layers?

The reason I ask is because a Codeplex project called emitmapper was created to address performance issues in automapper, and I remember seeing a comment that said automapper may take up to .5ms to map a large class. (reference needed)

I also remember seeing an article that describes how users have a higher chance of staying on your site if it loads within 70ms, as opposed to 90ms or more. (I'm looking for this link too). If automapper is consuming most of my page-load time, combined with network latency, then I see potential to not use automapper and create manual classes for my high volume pages and stick with a hybrid approach.

Bottom line: I would run the tests myself, but I don't know enough about .NET internals to create accurate results that can be used as a reusable guideline.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2024-12-22 09:48:03

底线:我会自己运行测试,但我对 .NET 内部结构了解不够,无法创建可用作可重用指南的准确结果。

您不需要了解 .NET 内部结构。您只需要知道您的性能要求是什么以及您的典型用法是什么样的。以各种方式在典型使用场景下分析代码,并选择满足您的性能要求且最容易维护的代码(即不一定选择性能最高的;还有其他标准)。

Bottom line: I would run the tests myself, but I don't know enough about .NET internals to create accurate results that can be used as a reusable guideline.

You don't need to know .NET internals. You just need to know what your performance requirements are and what your typical usage is going to look like. Profile the code under a typical usage scenario in the all the variety of ways, and choose that which meets your performance requirements and is easiest to maintain (i.e., don't necessarily choose the most performant; there are other criteria).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文