复杂的 DAL ->使用 AutoMapper 进行 BL 映射

发布于 2024-09-27 01:03:04 字数 809 浏览 0 评论 0 原文

我正在考虑在即将到来的项目中使用 AutoMapper 并尝试找到可能的“瓶颈”。目前我能想象的最复杂的情​​况如下: 一个域类,由数据库中的 3 个(例如)表表示(我的数据访问层是 LINQ to SQL)。要构建该类的实例,我需要执行 3 个查询:

  1. 按 ID 从表 A 中选择(1 行,直接进入 Class 属性)
  2. 按 ID 从表 B 中选择(0..1 行,转到可选的 Class.Code 属性)
  3. 从表 C 中按 ID 选择(0..N 行,转到 Class.Parameters 集合)

我不知道如何配置映射。以下是我考虑的选项:

  1. 执行 3 个查询并映射 Tuple -> Class
  2. 使用外连接合并查询 1 和 2(更有效)。但是我该如何处理匿名类型呢?
  3. 将 datacontext 注入到映射中,定义 A -> Class 映射并让类型转换器完成这项工作?

没有一个看起来像胜利。你有什么建议?

编辑:嗯,这种复杂的情况很少见(10-20%),我可以手动完成,其余 80-90% 使用 AutoMapper 就可以了。但我想知道 AutoMapper 是否不是为此类策略而设计的,或者我错过了一些重要的东西。

I'm considering to use AutoMapper in the upcoming project and trying to find possible "bottlenecks". At the moment the most complex case i can imagine is the following:
A domain class which is represented by 3 (for example) tables in the database (my data access layer is LINQ to SQL). To build an instance of the class i need to perform 3 queries:

  1. Select by ID from table A (1 row, goes directly to Class properties)
  2. Select by ID from table B (0..1 rows, goes to optional Class.Code property)
  3. Select by ID from table C (0..N rows, goes to Class.Parameters collection)

And i'm not sure how to configure the mapping. Here are the options i considered:

  1. Perform 3 queries and map Tuple<A,B,C> -> Class
  2. Combine queries 1 and 2 using outer join (more effective). But what do i do with anonymous type?
  3. Inject datacontext into the mapping, define A -> Class mapping and let the type converters do the job?

None looks like a win. What would you suggest?

Edit: Well, such complex cases is quite rare (10-20%) and i can do them manually and the rest 80-90% with AutoMapper just fine. But i'd like to know if AutoMapper is not designed for such strategies or i'm missing something important.

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

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

发布评论

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

评论(2

没︽人懂的悲伤 2024-10-04 01:03:04

您的问题很难回答,因为域对象如何以及在哪里获取其数据不是 AutoMapper 关心的。来自 AutoMapper 文档

AutoMapper 面向模型
投影场景扁平化
DTO 的复杂对象模型和
其他简单的物体,其设计是
更适合序列化,
通信、消息传递或简单的
之间的反腐败层
域和应用层。

但是,这并不意味着您对 AutoMapper 的使用不应考虑到域对象背后的思考过程。作为一个例子,延迟加载浮现在脑海中。例如,在您的情况下,如果您在 Class.Parameters 属性上使用延迟加载,但随后通过 AutoMapper 运行该属性,则数据将始终被加载。这就是为什么坚持 每次观看一个模型

AutoMapper 的创建者之一 Jimmy Bogard 在 AutoMapper:对象-对象映射器。我希望我可以直接链接到它,但在上述博加德帖子的评论回复中指出:

这是我们一直在寻找的东西
while(双向绑定),但是在
最后,我们发现还有太多
更新中进行了大量业务验证。

相反,我们形成了其他模式
围绕更新模型
消息/表格。请参阅 Code Camp 服务器
来源了解更多详细信息。

如前所述,您可以在 CodeCampServer 的源代码中找到 AutoMapper 的大量使用。 CodeCampServer 代码的简化版本可以在 源代码 中找到, href="https://rads.stackoverflow.com/amzn/click/com/193518279X" rel="nofollow noreferrer">ASP.NET MVC 2 实际操作。

Your question is difficult to answer because how and where a domain object get its data is not the concern of AutoMapper. From the AutoMapper documentation:

AutoMapper is geared towards model
projection scenarios to flatten
complex object models to DTOs and
other simple objects, whose design is
better suited for serialization,
communication, messaging, or simply an
anti-corruption layer between the
domain and application layer.

However, this doesn't mean that your use of AutoMapper shouldn't factor into the thought process behind your domain objects. As an example lazy loading springs to mind. For instance in your case if you used lazy loading on the Class.Parameters property, but then ran that property through AutoMapper the data would always be loaded. That's why it's important to stick to the rule of one model per view.

Jimmy Bogard one of the creators of AutoMapper discusses his vision for what AutoMapper is in AutoMapper: the Object-Object Mapper. I wish I could link directly to it but in a comment reply on the aforementioned post Bogard states:

That's something we've looked at for a
while (two-way binding), but in the
end, we found that there was just too
much business validation in an Update.

Instead, we formed other patterns
around updating a model from a
message/form. See Code Camp Server
source for more details.

As indicated you can find heavy use of AutoMapper in the source for CodeCampServer. A simplified version of the CodeCampServer code can be found in the source code provided with ASP.NET MVC 2 in Action.

-残月青衣踏尘吟 2024-10-04 01:03:04

这应该是根据您的对象模型做出的判断性决定。

优点:

  • 如果类直接映射到实体并且类属性名称与 table.columnname 匹配,则 Automapper 会很好。
  • Automapper 无缝映射集合。
  • 您可以创建 Automapper 配置文件并验证您的配置。
  • 您可以在应用程序级别拥有一个用于日期时间、金钱等的格式化程序。
  • 您可以选择用空白或任何您想要的字符填充 NULL。
  • Ignore() 在很多情况下都很有用。

缺点:

  • 映射中的异常有时很难调试。
  • 如果有很多解析器,您的代码看起来会很混乱。
  • 在复杂的对象结构中,您可能会决定直接映射少数类。因此不会有一致性。

It should be a judgemental decision looking at your object model.

Pros:

  • Automapper is good if the class directly maps to entities and class property names matches to table.columnname.
  • Automapper seamlessly maps collections.
  • You can create a Automapper profile and validate your configuration.
  • You can have one formatter for datetime, money etc at application level.
  • You have option to fill NULLs with blank or any character you want.
  • Ignore() comes in handy in many situations.

Cons:

  • Exceptions in mapping is difficult to debug at times.
  • Your code looks cluttered if there are many resolvers.
  • In a complex object structure you may resolve to direct mapping for few classes. Hence there wont be consistency.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文