问题标签 [automapper]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
1349 浏览

c# - 可以使用 WCF + DTO's + Automapper 吗?

我想将 automapper 与使用 WCF + DTO 的结构一起使用,但我想知道使用实体框架 4.0 进行急切加载的问题如何与 automap 的映射一起工作。

0 投票
3 回答
2958 浏览

asp.net-mvc - 如何处理具有多个聚合根的视图模型?

目前,我得到了非常糟糕的视图模型。

类看起来像这样=>

问题是 Reader 类型来自域模型(违反 SRP)。

基本上,我正在寻找设计技巧(即,将视图模型拆分为输入/输出是个好主意吗?)如何使我的视图模型无摩擦且对开发人员友好(即 - 映射应该使用控制器基类自动工作) ?

我知道 AutoMapper 框架,我很可能会使用它。

那么,再一次 - 尝试创建正确的视图模型时常见的问题是什么?如何构造它?当需要多域对象输入时如何进行映射?


当视图需要来自多个聚合根的数据时,我感到困惑。我正在创建具有图书馆、阅读器、书目记录等实体的应用程序。

在我的情况下 - 在域级别,将所有这 3 种类型分组LibraryReaderThatHasOrderedSomeBooks或诸如此类是没有意义的,但是应该显示关于特定图书馆中特定读者的订购书籍列表的视图需要它们全部。

OrderedBooksList所以 -用OrderedBooksListModel下面的视图模型和LibraryOutput视图模型创建视图似乎很好。甚至更好的视图模型,它利用展平技术并具有诸如等的道具。ReaderOutputBibliographicRecordOutputOrderedBooksListModelReaderFirstNameLibraryName

但这会导致映射问题,因为输入不止一个。
这不再是 1:1 的关系,我只踢一个聚合根。
这是否意味着我的域模型有点错误?

那么纯粹存在于 UI 层的视图模型字段(即表示选中选项卡的枚举)呢?

在这种情况下,每个人都会这样做吗?

我不愿意这样做=>

好像写了很多。:)


此刻正在阅读这篇文章。而这个


行。我对这个问题想了很多,是的 - 添加另一个抽象层似乎是一个解决方案 =>

替代文字

所以 - 在我看来,这已经奏效了,现在是玩弄的时候了。

泰吉米

0 投票
2 回答
1251 浏览

asp.net-mvc - AutoMapper 自定义类型转换器依赖项

我正在尝试实现 Automapper 以将 ViewModel 映射到实体,其中实体的属性之一也是实体。

我希望我的转换器使用 NHibernate 的 ISession.Load<> 方法来加载它。

所以问题是将 ISession 注入我的 ITypeConverter 实现的最佳方法是什么?还有一件事要记住的是,被注入的 ISession 将被处理掉,所以每次需要进行映射时我都需要注入一个新的 ISession 吗?

0 投票
2 回答
805 浏览

automapper - 关于列表属性的 Automapper 新手问题

作为 AutoMapper 的新粉丝,我将如何使用它来执行以下操作:

给定以下类,我想从 Group 创建 FlattenedGroup,其中项目字符串列表映射到 Item 的 title 属性。

谢谢

约瑟夫

0 投票
2 回答
2569 浏览

c# - 实体框架(Entities Classes)序列化

在阅读了 Enitity Framework 之后,我有一些问题:

1]在层之间转移实体的最佳方式是什么? a]我是否必须为此创建更轻的 DTO,或者我可以有效地序列化实体并传输它?

b]如果我必须为效率创建轻量级 DTO,并且在我看到 Automapper 的良好用法之后,我不太明白它如何节省额外的编码(如果这是它的目标),我的意思是,我们需要编写 flattern 类( DTO) 属性,所以它只保存 ctor。

2]DTO 那里建立实体类有什么意义吗?

谢谢。

0 投票
1 回答
2188 浏览

c# - 自动映射器约定

可以使用Automapper设置约定,以便在映射到的实体仅附加了“ViewModel”的情况下不必手动创建映射。

例如,我宁愿不必设置以下地图:

我知道如果需要投影,我需要创建一个自定义地图,但是有一个创建地图的约定会很好。

0 投票
3 回答
17314 浏览

c# - Automapper Custom Mapping Exception

Update 1-13-10 I've been able to find some success using the code below for mapping. I am essentially ignoring any of the properties that do not have a mapping and mapping them afterwards. I would appreciate feedback as to whether or not I am going about this in the best way possible. In addition, I am not sure how to go about unit testing this mapping. It was my impression that using the AutoMapper should help alleviate tediousness of checking each property.

Here is my new code:

Original Post:

I'm attempting to use AutoMapper for the first time in order to map from a Bussiness Object to a DTO. I am running into issues that I do not know how to troubleshoot, including the following exception:

AutoMapper.AutoMapperMappingException: Trying to map Graebel.SP.BO.MoveEntity to Graebel.SOA.Contracts.DataContracts.SP.MoveEntity. Exception of type 'AutoMapper.AutoMapperMappingException' was thrown

Here is the AutoMapper Code that I am running:

Here is the DTO (MoveEntityDto) that I am attempting to map:

Here is the Business Object (MoveEntity) that I am trying to map from

The properties within each class almost match up perfectly by name, but their types are different. Therefore I have attempted to perform a custom mapping using the "MapFrom" expression. However, AutoMapper doesn't seem to be able to allow me to point from one object type to another without complain.

I've also tried mapping property-to-property, with no luck. It looked something like this:

However, when attempting this, I receive the following exeception:

must resolve to top-level member. Parameter name: lambdaExpression.

I have been finding the documentation available for AutoMapper difficult to follow. Can someone please point me in the right direction as to how to use this utility correctly?

Thanks in advance for any help!

Adam

0 投票
5 回答
2167 浏览

asp.net-mvc - 在自定义 ViewModel 中重用验证属性

当我开始使用xVal进行客户端验证时,我只实现了将域模型对象用作视图模型或视图模型中这些对象的嵌入实例的操作方法。

这种方法在大多数情况下都可以正常工作,但是在某些情况下,视图只需要显示和回发模型属性的子集(例如,当用户想要更新他的密码,而不是他的其余配置文件数据时) .

一个(丑陋的)解决方法是在表单上为表单上不存在的每个属性设置一个隐藏的输入字段。

显然这里的最佳实践是创建一个自定义视图模型,它只包含与视图相关的属性并通过Automapper填充视图模型。因为我只传输与视图相关的数据,所以它更干净,但它远非完美,因为我必须重复域模型对象上已经存在的相同验证属性。

理想情况下,我想通过 MetaData 属性将域模型对象指定为元类(这通常也称为“伙伴类”),但这不起作用,因为当元数据类具有以下属性时 xVal 抛出不存在于视图模型上。

有什么优雅的解决方法吗?我一直在考虑破解 xVal 源代码,但也许到目前为止我还忽略了其他一些方法。

谢谢,

阿德里安

编辑:随着 ASP.NET MVC 2 的到来,这不仅是一个与验证属性相关的问题,而且它也适用于编辑器和显示属性。

0 投票
1 回答
555 浏览

.net - AutoMapper AssertConfiguration 在编译时?

有没有办法在编译时验证映射?例如,我有一个包含许多子实体的实体。如果我在尝试将 ParentX 映射到 ParentY 时忘记为其中一个孩子添加 CreateMap,则映射将失败。

我想不出在编译期间验证这一点的方法,但我很想找到一种方法。

0 投票
7 回答
9099 浏览

c# - Automapper 可以映射分页列表吗?

我想使用这样的方法将业务对象的分页列表映射到视图模型对象的分页列表:

分页列表实现类似于 Rob Conery 的实现:http: //blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/

你如何设置 Automapper 来做到这一点?