标签:Profile opt 映射 ABP 如何 CreateMap AutoMapper
一、ABP的Profile有什么用?
Profile用来编写AutoMapper的映射规则,在Profile类构造方法中编写的规则最后会配置到AutoMapper中,实现实体到DTO的自动映射。
二、代码浅析
1.ABP模板中,应用层的Module类有一个Initialize方法,该方法中用反射找到所有继承于Profile的类,并添加到映射配置。
public override void Initialize()
{
var thisAssembly = typeof(EpiApplicationModule).GetAssembly();
IocManager.RegisterAssemblyByConvention(thisAssembly);
Configuration.Modules.AbpAutoMapper().Configurators.Add(
// Scan the assembly for classes which inherit from AutoMapper.Profile
cfg => cfg.AddMaps(thisAssembly)
);
}
截图中的注释为ABP模板自带的注释,大意为:扫描该程序集中所有继承于Profile的类。(由于只扫描该程序集,这也是DTO写在应用层的原因之一)
2.继承Profile的例子
public class UserMapProfile : Profile
{
public UserMapProfile()
{
CreateMap<UserDto, User>();
CreateMap<UserDto, User>()
.ForMember(x => x.Roles, opt => opt.Ignore())
.ForMember(x => x.CreationTime, opt => opt.Ignore());
CreateMap<CreateUserDto, User>();
CreateMap<CreateUserDto, User>()
.ForMember(x => x.Roles, opt => opt.Ignore());
}
}
构造方法中配置AutoMapper的映射规则,具体配置方法很多博客都有,就不在本文中介绍了。
标签:Profile,opt,映射,ABP,如何,CreateMap,AutoMapper 来源: https://blog.csdn.net/ZZHSourceCode/article/details/120421752
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。