自动映射器:是否映射< A,B>给出< B,A> ;?
问题描述:
使用 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>();