c# automapper 使用(一)
一、最简单的用法
有两个类User和UserDto
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
} public class UserDto
{
public string Name { get; set; }
public int Age { get; set; }
}
将User转换成UserDto也和简单
Mapper.Initialize(x => x.CreateMap<User, UserDto>());
User user = new User()
{
Id = ,
Name = "caoyc",
Age =
};
var dto = Mapper.Map<UserDto>(user);
这是一种最简单的使用,AutoMapper会更加字段名称去自动对于,忽略大小写。
二、如果属性名称不同
将UserDto的Name属性改成Name2
Mapper.Initialize(x =>
x.CreateMap<User, UserDto>()
.ForMember(d =>d.Name2, opt => {
opt.MapFrom(s => s.Name);
})
); User user = new User()
{
Id = ,
Name = "caoyc",
Age =
}; var dto = Mapper.Map<UserDto>(user);

三、使用Profile配置
自定义一个UserProfile类继承Profile,并重写Configure方法
public class UserProfile : Profile
{
protected override void Configure()
{
CreateMap<User, UserDto>()
.ForMember(d => d.Name2, opt =>
{
opt.MapFrom(s => s.Name);
});
}
}
使用时就这样
Mapper.Initialize(x => x.AddProfile<UserProfile>());
User user = new User()
{
Id = ,
Name = "caoyc",
Age =
};
var dto = Mapper.Map<UserDto>(user);
四、空值替换NullSubstitute
空值替换允许我们将Source对象中的空值在转换为Destination的值的时候,使用指定的值来替换空值。
public class UserProfile : Profile
{
protected override void Configure()
{
CreateMap<User, UserDto>()
.ForMember(d => d.Name2, opt => opt.MapFrom(s => s.Name))
.ForMember(d => d.Name2, opt => opt.NullSubstitute("值为空")); }
}
Mapper.Initialize(x => x.AddProfile<UserProfile>());
User user = new User()
{
Id = ,
Age =
};
var dto = Mapper.Map<UserDto>(user);
结果为:

五、忽略属性Ignore
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
} public class UserDto
{
public string Name { get; set; }
public int Age { get; set; } } public class UserProfile : Profile
{
protected override void Configure()
{
CreateMap<User, UserDto>().ForMember("Name", opt => opt.Ignore());
}
}
使用
Mapper.Initialize(x => x.AddProfile<UserProfile>());
User user = new User()
{
Id = ,
Name="caoyc",
Age =
};
var dto = Mapper.Map<UserDto>(user);
结果:

六、预设值
如果目标属性多于源属性,可以进行预设值
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
} public class UserDto
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; } } public class UserProfile : Profile
{
protected override void Configure()
{
CreateMap<User, UserDto>();
}
}
使用
Mapper.Initialize(x => x.AddProfile<UserProfile>());
User user = new User()
{
Id = ,
Name="caoyc",
Age =
};
UserDto dto = new UserDto() {Gender = "男"};
Mapper.Map(user, dto);

七、类型转换ITypeConverter
如果数据中Gender存储的int类型,而DTO中Gender是String类型
public class User
{
public int Gender { get; set; }
} public class UserDto
{
public string Gender { get; set; }
}
类型转换类,需要实现接口ITypeConverter
public class GenderTypeConvertert : ITypeConverter<int, string>
{
public string Convert(int source, string destination, ResolutionContext context)
{
switch (source)
{
case :
destination = "男";
break;
case :
destination = "女";
break;
default:
destination = "未知";
break;
}
return destination;
}
}
配置规则
public class UserProfile : Profile
{
protected override void Configure()
{
CreateMap<User, UserDto>(); CreateMap<int, string>().ConvertUsing<GenderTypeConvertert>();
//也可以写这样
//CreateMap<int, string>().ConvertUsing(new GenderTypeConvertert());
}
}
使用
Mapper.Initialize(x => x.AddProfile<UserProfile>());
User user0 = new User() { Gender = };
User user1 = new User() { Gender = };
User user2 = new User() { Gender = };
var dto0= Mapper.Map<UserDto>(user0);
var dto1 = Mapper.Map<UserDto>(user1);
var dto2 = Mapper.Map<UserDto>(user2);
Console.WriteLine("dto0:{0}", dto0.Gender);
Console.WriteLine("dto1:{0}", dto1.Gender);
Console.WriteLine("dto2:{0}", dto2.Gender);
结果

八、条件约束Condition
当满足条件时才进行映射字段,例如人类年龄,假设我们现在人类年龄范围为0-200岁(这只是假设),只有满足在这个条件才进行映射
DTO和Entity
public class User
{
public int Age { get; set; }
} public class UserDto
{
public int Age { get; set; }
}
Profile
public class UserProfile : Profile
{
protected override void Configure()
{
CreateMap<User, UserDto>().ForMember(dest=>dest.Age,opt=>opt.Condition(src=>src.Age>= && src.Age<=));
}
}
使用代码
Mapper.Initialize(x => x.AddProfile<UserProfile>());
User user0 = new User() { Age = };
User user1 = new User() { Age = };
User user2 = new User() { Age = };
var dto0= Mapper.Map<UserDto>(user0);
var dto1 = Mapper.Map<UserDto>(user1);
var dto2 = Mapper.Map<UserDto>(user2);
Console.WriteLine("dto0:{0}", dto0.Age);
Console.WriteLine("dto1:{0}", dto1.Age);
Console.WriteLine("dto2:{0}", dto2.Age);
输出结果

c# automapper 使用(一)的更多相关文章
- 恋爱虽易,相处不易:当EntityFramework爱上AutoMapper
剧情开始 为何相爱? 相处的问题? 女人的伟大? 剧情收尾? 有时候相识即是一种缘分,相爱也不需要太多的理由,一个眼神足矣,当EntityFramework遇上AutoMapper,就是如此,恋爱虽易 ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- AutoMapper
什么是AutoMapper? AutoMapper是一个对象和对象间的映射器.对象与对象的映射是通过转变一种类型的输入对象为一种不同类型的输出对象工作的.让AutoMapper有意思的地方在于它提供了 ...
- AutoMapper随笔记
平台之大势何人能挡? 带着你的Net飞奔吧! http://www.cnblogs.com/dunitian/p/4822808.html#skill 先看效果:(完整Demo:https://git ...
- AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...
- AutoMapper的介绍与使用(一)
软件环境 vs2015 asp.net mvc 5 .NET Framework 4.5.2 AutoMapper 5.2.0.0 AutoMapper安装 新建asp.net mvc 项目 Auto ...
- AutoMapper使用中的问题
指定值只会执行一次 public class MomanBaseProfile : Profile { public MomanBaseProfile() { CreateMap<Request ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(中)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【AutoMapper官方文档】DTO与Domin Model相互转换(下)
写在前面 AutoMapper目录: [AutoMapper官方文档]DTO与Domin Model相互转换(上) [AutoMapper官方文档]DTO与Domin Model相互转换(中) [Au ...
- 【道德经】漫谈实体、对象、DTO及AutoMapper的使用
写在前面 实体和值对象 实体和对象 故常无欲以观其妙,常有欲以观其徼 初始实体和演化实体 代码中的DTO AutoMapper实体转换 后记 实体(Entity).对象(Object).DTO(Dat ...
随机推荐
- js同步、异步、回调的执行顺序以及闭包的理解
首先,记住同步第一.异步第二.回调最末的口诀 公式表达:同步=>异步=>回调 看一道经典的面试题: for (var i = 0; i < 5; i++) { setTimeout( ...
- 【转载】URL编码与两次encodeURI
当使用地址栏提交查询参数时,如果不编码,非英文字符会按照操作系统的字符集进行编码提交到服务器,服务器会按照配置的字符集进行解码,所以如果两者不一致就会导致乱码. encodeURI函数采用UTF-8对 ...
- Font Awesome字体图标的 用法, 很简单
http://fontawesome.dashgame.com/ 上面是 官网, 可下载,也可以CDN. 1... 加载 2... 用法
- 英语发音规则---I字母常见发音组合有哪些
英语发音规则---I字母常见发音组合有哪些 一.总结 一句话总结: I/y在开音节中发/aɪ/,例如:bite /baɪt/ n. 咬 I/y在闭音节中发 /ɪ/,例如:clinic /'klɪnɪk ...
- vxlan中vtep角色,以及通过GRE隧道进行流镜像
1. 交换机上建立gre隧道,对端ip为ip12. 交换机上报gre隧道的OF逻辑端口port id,这里gre tunnel的id实际就是OF逻辑端口id3. 控制器建立流ipflow1的镜像配置, ...
- 版本控制——Version Control
版本控制是指对软件开发过程中各种程序代码.配置文件及说明文档等文件变更的管理,是软件配置管理的核心思想之一. 版本控制最主要的功能就是追踪文件的变更.它将什么时候.什么人更改了文件的什么内容等信息忠实 ...
- Sign Up Account In CloudAMQP
CloudAMQP 有多种账号级别,请参考下面的链接的内容访问你可以注册的级别:https://www.cloudamqp.com/plans.html 作为测试来说,你可以注册免费的的消息. 你可以 ...
- xxx征集系统项目目标文档
分组:每四人一组 主题:xxx征集系统 成果: 讨论结束后,每组提交一份课堂讨论记录(电子版发表到博客上,纸质版小组成员签名,下节课提交). 每人根据课堂讨论结果提交一份系统利益相关者描述案例.撰写项 ...
- 如何 distinct 只对一个字段有用,同时查出其他字段
转至:http://blog.csdn.net/u013402772/article/details/51262524 在使用MySQL时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有d ...
- 数据结构算法之冒泡排序——Java语言实现
今天来谈下冒泡排序算法,这次实现由两种形式如下所示: 1.对于长度为N的数据序列,没有加标签限制,针对一开始就是有序的数据序列,仍然需要排序N-1趟来完成排序. 2.对于长度为N的数据序列,加标了签限 ...