当我多次调用相同类型的Mapper.CreateMap时会发生什么?
它会改写以前的地图吗?如果是这样,如果我尝试创建已创建的地图,是否可以使其抛出异常?
Art*_*r P 23
当多次为同一组源和目标调用Mapper.CreateMap时,根本不会发生任何事情,因为Mapper.CreateMap<TSource, TDestination>()
它没有为映射配置设置任何扩展.如果为此设置IMappingExpression的覆盖,则为
Mapper.CreateMap<TSource, TDestination>().ConstructUsing(x=>new TDestination(x.SomeField))
yes,此映射的配置将替换为新映射.关于问题的第二部分,我知道验证地图是否已经创建的方法:
public TDestination Resolve<TSource, TDestination>(TSource source)
{
var mapped = Mapper.FindTypeMapFor(typeof(TSource), typeof(TDestination)); //this will give you a reference to existing mapping if it was created or NULL if not
if (mapped == null)
{
var expression = Mapper.CreateMap<TSource, TDestination>();
}
return Mapper.Map<TSource, TDestination>(source);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4489 次 |
最近记录: |