其他分享
首页 > 其他分享> > AutoMapper学习笔记

AutoMapper学习笔记

作者:互联网

AutoMapper

基本:
var config=new MapperConfiguration(cfg=>{ cfg.CreateMap<Foo,FooTo> });
var mapper=config.CreateMapper();
var result=mapper.Map<FooTo>(foo);

单例注册:
Mapper.Initialize()

Profile:

Public Class MyProfile:Profile{
public MyProfile(){

CreateMap<From,TO>();
}
}
var config=new MapperConfiguration(cfg=>{
cfg.addProfile<MyProfile>();
//自动加载方法
cfg.addMaps( system.appdomain.currentDomain.GetAssemblies )
cfg.addMaps("xxx.dll");
})

命名约定:

cfg.CreateMap<Foo,Foo2>();
cfg.SourceMemberNamingConvention=new PascalCaseNamingConvention();大驼峰,帕斯卡命名法
可见性:
cfg.ShouldMapProperty=p=>p.GetMethod.IsPublic||p.SetMethod.IsPrivate
cfg.ShouldMapProperty=p=>false
识别前后缀:
cfg.RecognizePrefixs("My");
cfg.RecognizePostFixes("MY");
替换字符:
cfg.ReplaceMemberName("AA","A");

禁用构造函数: cfg.DisableConstuctorMapping();

数组映射:mapper.Map<source[],IEnumerable<dest>>(source);
集合多态: cfg.CreateMap<E1,E2>.Include<E,E>,E是E1的子类扩展
自定义映射:cfg.CreateMap<E1,E2>().ForMember("ID",opt=>opt.MapFrom(src=>src.ID))
.ForMember(dst=>dst.Name,opt=>opt.MapFrom(src=>src.Name));
IncludeMember: cfg.CreateMap<E1,E2>().IncludeMembers(e=>e.Xx);
cfg.CrateeMap<Xx,E2>();

 

标签:opt,src,cfg,笔记,学习,var,CreateMap,AutoMapper,new
来源: https://www.cnblogs.com/congqiandehoulai/p/15255663.html