当我应该使用AutoMapper,什么时候不 [英] When should I be using AutoMapper and when not

查看:230
本文介绍了当我应该使用AutoMapper,什么时候不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对我的EF6 DbFirst EDMX,仓库和AutoMappings数据层。

I have a Data layer which holds my EF6 DbFirst edmx, repositories, and AutoMappings.

我也有一个波科模型层在我的数据层每个自动生成的实体。属性pretty多严丝合缝除了几个名称更改。

I also have a Model layer with a Poco for each auto generated entity in my Data layer. The properties pretty much match exactly except for a few name changes.

AutoMapper被安装到只有我的数据层,这就是我把所有映射在配置文件中。在这一点上我有一个从每个数据层实体每个ModelLayer实体的映射和每个ModelLayer实体每个数据层实体。任何名称的变化是在映射指定。

AutoMapper is installed to my DataLayer only and this is where I set all of my mappings in a config file. At this point I have a mapping from each DataLayer entity to each ModelLayer entity and each ModelLayer entity to each DataLayer entity. Any name changes are specified in the mappings.

由于它是建立这种方式在我的仓库保存方法函数需要从ModelLayer对象但随后被映射到数据层对象,这样我就可以将其发送到的DbContext。当我拉着库信息,我使用的DbContext检索,然后AutoMapper功能映射到模型,以便该函数可以返回作为Model.entity。

Since it is setup this way in my repository save methods the function takes in an object from the ModelLayer but then is mapped to a DataLayer object so I can send it to the DbContext. When pulling information in my repository I use the DbContext to retrieve and then the AutoMapper function to map to Model so the function can return as Model.entity.

我的业务层和Web应用程序只能使用模型实体。如果有什么事情似乎是错误的资讯,请让我知道。

My business layer and web app only use the model entities. If anything seems wrong about this please let me know.

另一件事是从映射到ModelLayer视图模型,反之亦然期间GET和在我的控制器POST操作。它是正常的双向映射在这里呢?我需要AutoMapper在这点上安装到我的web应用程序?

The other thing is mapping from ModelLayer to ViewModel and vice versa during GET and POST Actions in my controller. Is it normal to map both ways here too? Do I need to install AutoMapper to my web app at this point?

推荐答案

我使用AutoMapper时,我想摆脱枯燥的左手侧右手边code的。如果逻辑不是复制数据完全明显,我恢复到手动映射。

I use AutoMapper when I want to get rid of boring left-hand-side right-hand-side code. If the logic isn't completely obvious for copying data, I revert back to manual mapping.

这些天,这意味着我使用LINQ的预测从AutoMapper所有gets和谨慎的帖子。

These days this means I use LINQ projections from AutoMapper on all GETs, and sparingly on POSTs.

这篇关于当我应该使用AutoMapper,什么时候不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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