我正在考虑在即将到来的项目中使用 AutoMapper 并尝试找到可能的“瓶颈”。目前我能想象的最复杂的情况如下:
一个域类,由数据库中的 3 个(例如)表表示(我的数据访问层是 LINQ to SQL)。要构建该类的实例,我需要执行 3 个查询:
- 按 ID 从表 A 中选择(1 行,直接进入
Class
属性)
- 按 ID 从表 B 中选择(0..1 行,转到可选的
Class.Code
属性)
- 从表 C 中按 ID 选择(0..N 行,转到
Class.Parameters
集合)
我不知道如何配置映射。以下是我考虑的选项:
- 执行 3 个查询并映射
Tuple
-> Class
- 使用外连接合并查询 1 和 2(更有效)。但是我该如何处理匿名类型呢?
- 将 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:
- Select by ID from table A (1 row, goes directly to
Class
properties)
- Select by ID from table B (0..1 rows, goes to optional
Class.Code
property)
- 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:
- Perform 3 queries and map
Tuple<A,B,C>
-> Class
- Combine queries 1 and 2 using outer join (more effective). But what do i do with anonymous type?
- 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.
发布评论
评论(2)
您的问题很难回答,因为域对象如何以及在哪里获取其数据不是 AutoMapper 关心的。来自 AutoMapper 文档:
但是,这并不意味着您对 AutoMapper 的使用不应考虑到域对象背后的思考过程。作为一个例子,延迟加载浮现在脑海中。例如,在您的情况下,如果您在 Class.Parameters 属性上使用延迟加载,但随后通过 AutoMapper 运行该属性,则数据将始终被加载。这就是为什么坚持 每次观看一个模型。
AutoMapper 的创建者之一 Jimmy Bogard 在 AutoMapper:对象-对象映射器。我希望我可以直接链接到它,但在上述博加德帖子的评论回复中指出:
如前所述,您可以在 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:
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:
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.
这应该是根据您的对象模型做出的判断性决定。
优点:
缺点:
It should be a judgemental decision looking at your object model.
Pros:
Cons: