自动映射器:是否映射< A,B>给出< B,A&gt ;? [英] AutoMapper: Does Map <A,B> give <B,A>?

查看:74
本文介绍了自动映射器:是否映射< A,B>给出< B,A&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Automapper 创建一个简单的地图:

Using Automapper I create a simple map:

Mapper.CreateMap<MyCustomerDTO, YourCustomerDTO>()

我经常也需要采用其他方式.我是否还需要以其他方式创建映射,还是Automapper会根据上述映射来推断它?

I often need to go the other way too. Do I need to also create the mapping the other way, or will Automapper infer it based on the above mapping?

Mapper.CreateMap<YourCustomerDTO, MyCustomerDTO>()   //Needed?

推荐答案

否.您必须创建两种方式的映射.双向映射的好帮手方法可能是:

No. you must create two way mapping. A good helper method for two way mapping could be :

 protected virtual void ViceVersa<T1, T2>()
        {
            Mapper.CreateMap<T1, T2>();
            Mapper.CreateMap<T2, T1>();
        }

然后像这样使用它:

ViceVersa<T1, T2>();

这篇关于自动映射器:是否映射&lt; A,B&gt;给出&lt; B,A&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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