使用AutoMapper的原因 [英] Reasons Using AutoMapper

查看:100
本文介绍了使用AutoMapper的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我们公司开始使用AutoMapper. 我的团队成员没有看到这个问题的任何好处. 主要主张是,当我们可以编写自己的扩展方法时,为什么需要附加抽象.

I want to start using AutoMapper at our firm. The problem the my team members just don’t see any benefit with it. The main claim why we need addition abstraction when we can write our-self extensions method that doing the same.

那么我反对的理由是什么?

So what are the reasons I can raise against ?

推荐答案

我对Automapper的两个最大争论:

My two biggest arguments for Automapper:

  1. 当您可以依靠经过良好测试的,基于约定的库为您服务时,为什么还要编写自定义内容呢?

是的,您可以编写扩展方法以在属性之间移动数据. 但是,为什么我要为运输数据而写无数的映射行 跨类型,例如

Yes, you can write extension methods to move data between properties. But, why would I write countless mapping lines just for shipping data across types, e.g.

Property1 = original.Property1; Property2 = original.Property2;

尤其是,当您执行以下操作时:

especially when you are doing thing like this:

Property1 = (MyEnum)Enum.Parse(typeof(MyEnum), original.Property1);

这是乱七八糟的管道代码,编写起来很耗时,而且您拥有 与您的时间相关的更好的事情,例如构建有用的功能,以 像这样乱七八糟. AutoMapper提供了所有 惯例,您需要免费,简单地完成第一个案例 用于管理您所处的更复杂场景的模式 减少形状或更改类型.

It's messy plumbing code that is time-consuming to write, and you have better things to do with your time, like building useful features, to mess around with things like this. AutoMapper provides all the convention you need to get the first case done for free, and simple patterns for managing the more complex scenarios where you are reducing shapes or changing types.

  1. 地图本身易于测试.

同样,您可以编写自己的方法.但是,如果您正在做 正确的话,您有相应的测试可以涵盖所有情况 您刚编写的自定义代码所固有的. 或者,你可以 加载您的地图,并询问AutoMapper您的地图配置是否 是正确的.

Again, you can write your own methods. However, if you're doing things correctly, you have corresponding tests to cover all the cases inherent with the custom code you just wrote. Or, you could just loads your maps up and ask AutoMapper if your mapping configurations are correct.

我对Automapper的警告:

My caveat argument against Automapper:

  • 如果您开始必须向映射写入足够多的异常,而您几乎已经停止依赖于配置,那么它很可能会成为泄漏抽象. (实际上,问题可能出在您的域的设计中,而AutoMapper只是发出上游问题的第一个约束).

我并不是说这是映射代码的灵丹妙药(它有其古怪之处),但是花费大量时间查看它是否适合您的解决方案是值得的.

I'm not saying it's the panacea for mapping code (it has its quirks) but the time expended spiking with it to see if it fits your solution is worth the investment.

这篇关于使用AutoMapper的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆