AutoMapper如何映射对象A对象B不同,这取决于上下文 [英] AutoMapper How To Map Object A To Object B Differently Depending On Context

查看:200
本文介绍了AutoMapper如何映射对象A对象B不同,这取决于上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用所有AutoMapper大师!

Calling all AutoMapper gurus!

我希望能够映射对象A B不同,具体取决于上下文在运行时对象。特别是,我想在一个映射的情况下忽略某些属性,并在另一起案件中映射的所有属性。

I'd like to be able to map object A to object B differently depending on context at runtime. In particular, I'd like to ignore certain properties in one mapping case, and have all properties mapped in another case.

我正在经历的是Mapper.CreateMap可以在不同的映射的情况下调用成功,但是,一旦CreateMap被调用,地图某一对类型的设置,而不是随后被成功CreateMap通话可能不同描述映射的改变。

What I'm experiencing is that Mapper.CreateMap can be called successfully in the different mapping cases however, once CreateMap is called, the map for a particular pair of types is set and is not subsequently changed by succeeding CreateMap calls which might describe the mapping differently.

我发现了一个博客帖子主张Mapper.Reset()来获取圆的问题,然而,映射器类的静态性质意味着发生冲突和碰撞之前这只是一个时间问题。

I found a blog post which advocates Mapper.Reset() to get round the problem, however, the static nature of the Mapper class means that it is only a matter of time before a collision and crash occur.

有没有办法做到这一点?

Is there a way to do this?

我想我需要的是调用一次Mapper.CreateMap每个AppDomain,后来,可以调用Mapper.Map有关哪些属性应包含/排除的提示。

What I think I need is to call Mapper.CreateMap once per appdomain, and later, be able to call Mapper.Map with hints about which properties should be included / excluded.

现在,我正在考虑更改源通过编写一个基于持有的映射配置实例的非静态映射类的代码。业绩不佳,但线程安全的。

Right now, I'm thinking about changing the source code by writing a non-static mapping class that holds the mapping config instance based. Poor performance, but thread safe.

什么是我的选择。可以做些什么? Automapper看起来很有希望。

What are my options. What can be done? Automapper seems so promising.

推荐答案

的映射类是纯粹根据配置和引擎(MappingEngine)物体的顶部薄薄的包装。您可以创建配置/引擎(MappingEngine)对象(仍采用单件)的单独实例,并使用你选择的IoC容器装载必要的正确的。

The Mapper class is merely a thin wrapper on top of a Configuration and MappingEngine objects. You can create separate instances of Configuration/MappingEngine objects (still using singletons), and use your IoC container of choice to load the correct one as necessary.

仍是最好的选择是使用不同的目标类型。真正左右支持此功能非常艰难的部分是类型映射的固有的层次性。顶层对象可能具有一个映射轮廓,而较低水平的人不知道。有些在两者之间可能有与否,等等。

The best option still is to use different destination types. The really tough part about truly supporting this feature is the inherent hierarchical nature of type maps. The top-level object might have a mapping profile, while lower level ones do not. Some in between might have it or not, etc.

这篇关于AutoMapper如何映射对象A对象B不同,这取决于上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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