0

我正在尝试使用我将称为基本 linq-to-sql 的东西创建一个 LINQ-to-SQL 解决方案。它不是通过 EF 或类似的方式提供的完整 ORM 解决方案。现在的重点是利用 LINQ 的强大功能并减少存储过程(以及其他一些好处)。

我以前做过一些 DTO 和实体的工作……但这也是一个自定义实现。在那个实现中使用了自动映射器。当实体属性与 DTO 属性匹配时,我知道自动映射器“效果最佳”。但如果他们不这样做呢?您可以以自定义方式对其进行配置吗?

我想一个更大的问题是......对于您无法在查询中实例化“LINQ 映射”“实体”这一事实,这是一个真正的解决方案。要清楚,您不能使用 linq 并选择一个新的“实体”,您必须选择一个未映射的 DTO。我最终要做的是避免在给定查询中将实体“映射”到 DTO。我想这没什么大不了的,但是像 automapper 这样的东西能解决这个问题吗?

4

1 回答 1

0

I'm not exactly sure what you're asking but:

I know automapper "works best" when entitiy properties match DTO properties. But what if they don't? Can you configure it in a custom way?

If you're asking what I think you're asking you should look into .ForMember and overriding the Convert function. There are examples of both on the wiki: .ForMember: https://github.com/AutoMapper/AutoMapper/wiki/Projection Convert: https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters

Automapper does work "best" when dtos and entities match up because it's super easy to set up a map between the two is their property names and types match. When they don't match you need to manually select properties with .ForMember or manually do the whole conversion by overriding the Convert function.

I hope this is a little helpful. Pop a comment below if it wasn't or you need more info.

于 2013-07-30T02:16:52.437 回答