博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AutoMapper 5.0-升级指南
阅读量:6587 次
发布时间:2019-06-24

本文共 3431 字,大约阅读时间需要 11 分钟。

Initialization

You now must use either Mapper.Initialize or new MapperConfiguration() to initialize AutoMapper. If you prefer to keep the static usage, use Mapper.Initialize.

If you have a lot of Mapper.CreateMap calls everywhere, move those to a Profile, or into Mapper.Initialize, called once at startup.

Profiles

Instead of overriding a Configure method, you configure directly via the constructor:

public class MappingProfile : Profile {    public MappingProfile() {        CreateMap
(); RecognizePrefix("m_"); }}

Resolution Context things

ResolutionContext used to capture a lot of information, source and destination values, along with a hierarchical parent model. For source/destination values, all of the interfaces (value resolvers and type converters) along with config options now include the source/destination values, and if applicable, source/destination members.

If you're trying to access some parent object in your model, you will need to add those relationships to your models and access them through those relationships, and not through AutoMapper's hierarchy. The ResolutionContext was pared down for both performance and sanity reasons.

Value resolvers

The signature of a value resolver has changed to allow access to the source/destination models. Additionally, the base class is gone in favor of interfaces. For value resolvers that do not have a member redirection, the interface is now:

public interface IValueResolver
{ TDestMember Resolve(TSource source, TDestination destination, TDestMember destMember, ResolutionContext context);}

You have access now to the source model, destination model, and destination member this resolver is configured against.

If you are using a ResolveUsing and passing in the FromMember configuration, this is now a new resolver interface:

public interface IMemberValueResolver
{ TDestMember Resolve(TSource source, TDestination destination, TSourceMember sourceMember, TDestMember destMember, ResolutionContext context);}

This is now configured directly as ForMember(dest => dest.Foo, opt => opt.ResolveUsing<MyCustomResolver, string>(src => src.Bar)

Type converters

The base class for a type converter is now gone in favor of a single interface that accepts the source and destination objects and returns the destination object:

public interface ITypeConverter
{ TDestination Convert(TSource source, TDestination destination, ResolutionContext context);}

Circular references

Previously, AutoMapper could handle circular references by keeping track of what was mapped, and on every mapping, check a local hashtable of source/destination objects to see if the item was already mapped. It turns out this tracking is very expensive, and you need to opt-in using PreserveReferences for circular maps to work. Alternatively, you can configure MaxDepth:

// Self-referential mappingcfg.CreateMap
().MaxDepth(3);// Circular references between users and groupscfg.CreateMap
().PreserveReferences();

UseDestinationValue

UseDestinationValue tells AutoMapper not to create a new object for some member, but to use the existing property of the destination object. It used to be true by default. Consider whether this applies to your case. Check .

cfg.CreateMap
() .ForMember(d => d.Child, opt => opt.UseDestinationValue());

转载于:https://www.cnblogs.com/Leman/p/5774000.html

你可能感兴趣的文章
Linux Python详细安装、升级指南
查看>>
无法修复ie使用代理服务器
查看>>
教你给IDEA安装插件
查看>>
隐蔽可扩展PHP Webshell – Weevely 1.0
查看>>
如何让Yii框架支持多个数据库
查看>>
用函数指针读取并调用虚函数表指向的每个函数
查看>>
办公小贴士之:在Outlook 2010中添加农历生日
查看>>
我的友情链接
查看>>
ActionScript 3.0游戏编程——创建简单的ActionScript程序
查看>>
函数const
查看>>
关于“Return empty arrays or collections, not nulls”的思考
查看>>
CodeForces-1167E-Range Deleting
查看>>
兼容多个版本程序集的web.config配置
查看>>
java finally块执行时机分析
查看>>
day6 字符串
查看>>
JMeter5.0 边界提取器使用
查看>>
Windows Azure 上的 Symfony,适用于 PHP 开发者的强大组合
查看>>
堆和栈的区别 (转贴)
查看>>
通过包名获取该包下的所有类
查看>>
【JavaScript学习笔记】画图
查看>>