最佳实践:使用 AutoMapper 或 LINQ (LINQ to Objects) 在域模型和表示模型之间进行映射的优点和缺点

发布于 2024-08-29 00:43:31 字数 1431 浏览 5 评论 0原文

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

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

发布评论

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

评论(3

三生一梦 2024-09-05 00:43:31

使用 AutoMapper 也有一些缺点,其中一些缺点通常适用于按约定进行编程(而不是显式编写代码)。

假设您有两个 C# 类 -

namespace MyDtoNamespace {
    public class MyClass {
        public int Id { get; set; }
}}

namespace MyBusinessLayerNamespace {
    public class MyClass {
        public int Id { get; set; }
}}

AutoMapper 将在这两个类之间很好地映射,几乎不需要显式配置。

但后来,假设开发人员考虑将这些 Id 属性之一重命名为不同的内容,例如,

namespace MyBusinessLayerNamespace {
    public class MyClass {
        public int MyNewIdentifierVariableName { get; set; }
}}

我在 Visual Studio 中仔细搜索引用并考虑重命名对这些引用的影响 - 但因为 MyDtoNamespace.MyClass.Id 没有显式引用 MyBusinessLayerNamespace.MyClass。 id,我从来没见过。

当 Visual Studio 或其他工具自动为我在解决方案中重命名所有出现的变量时,AutoMapper 映射会中断。

我只能在运行时发现这一点,不存在编译时问题。理想情况下,我有单元测试来验证我的映射是否按我希望的方式工作,但即便如此,重构期间潜在的运行时错误是更喜欢编写显式映射代码的一个很好的理由。

我当然并不是主张完全避免使用 AutoMapper,只是指出这是按惯例编程的一个主要问题。

There are also disadvantages in using an AutoMapper and some of these disadvantages apply generally to programming by convention (as opposed to writing code explicitly).

Say you have two C# classes -

namespace MyDtoNamespace {
    public class MyClass {
        public int Id { get; set; }
}}

namespace MyBusinessLayerNamespace {
    public class MyClass {
        public int Id { get; set; }
}}

An AutoMapper will map between these two classes nicely with little explicit configuration required.

But later, say a developer considers renaming one of these Id properties to something different e.g.

namespace MyBusinessLayerNamespace {
    public class MyClass {
        public int MyNewIdentifierVariableName { get; set; }
}}

I carefully search for references in Visual Studio and consider the impact of renaming on these references - but because MyDtoNamespace.MyClass.Id does not explicitly reference MyBusinessLayerNamespace.MyClass.Id, I never see it.

When Visual Studio or another tool automatically renames all occurences of the variable for me in the solution, the AutoMapper mapping breaks.

I can only find this out at run time, there is no compile time problem. Ideally, I have unit tests to verify my mapping works as I hope but even so, the potential for run time errors during refactoring is a good reason to prefer writing explicit mapping code.

I am certainly not arguing to avoid AutoMapper altogether, just noting that a major problem with programming by convention.

酒中人 2024-09-05 00:43:31

引用另一个 Automapper 问题的部分答案:

如果您有一个类型的对象,并且想要使用第一种类型的属性填充另一种类型的对象的属性,您有两种选择:

  1. 手动编写代码来执行这样的操作
    映射。
  2. 使用可以自动为您处理此问题的工具。

AutoMapper 是 2 的一个示例。

to Objects 是 1 的一个示例 - 它只是比编写普通的对象到对象映射代码稍微轻松一些。

就优缺点而言:

  • 与 LINQ 相比,Automapper 应该显着减少您必须编写的代码量,因为它使用约定来确定默认映射。使用 LINQ,必须定义这些默认映射。

  • 使用 LINQ,需要定义两个方向的映射 - Automapper 应该能够在使用约定时自动解决这个问题。

  • 与所有第三方 dll 一样,使用 Automapper 将引入另一个依赖项,并且需要一个小的学习曲线(请注意,对于那些以前没有使用过 LINQ 的开发人员来说,也会有一个学习曲线)。

请注意,Automapper 可以与 LINQ(和 LINQ2SQL)结合使用 - 另一篇 Automapper 帖子解释了一些细节。

Quoting part of an answer from another Automapper question:

If you have an object of one type and you want to populate the properties of an object of another type using properties from the first type, you have two choices:

  1. Manually write code to do such a
    mapping.
  2. Use a tool that will automatically handle this for you.

AutoMapper is an example of 2.

LINQ to Objects is an example of 1 - it just happens to be a bit less painful than writing vanila object-to-object mapping code.

In terms of pros and cons:

  • Automapper should significantly reduce the amount of code you have to write compared to LINQ as it uses conventions to determine default mappings. Using LINQ, these default mappings would have to be defined.

  • Using LINQ it will be necessary to define mappings in both directions - Automapper should be able to work this out automatically when conventions are used.

  • As with all third-party dll's, using Automapper will introduce another dependency and require a small learning curve (note there would be a learning curve for those developers who haven't used LINQ before as well).

Note, Automapper can be used in conjunction with LINQ (and LINQ2SQL) - yet another Automapper post which explains some of the finer points.

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