转载 c# automapper 使用(一) https://www.cnblogs.com/caoyc/p/6367828.html

一、最简单的用法

有两个类User和UserDto

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     public class User
 2     {
 3         public int Id { get; set; }
 4         public string Name { get; set; }
 5         public int Age { get; set; }
 6     }
 7 
 8     public class UserDto
 9     {
10         public string Name { get; set; }
11         public int Age { get; set; }
12     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

将User转换成UserDto也和简单

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
1     Mapper.Initialize(x => x.CreateMap<User, UserDto>());
2     User user = new User()
3     {
4         Id = 1,
5         Name = "caoyc",
6         Age = 20
7     };
8     var dto = Mapper.Map<UserDto>(user);
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

 这是一种最简单的使用,AutoMapper会更加字段名称去自动对于,忽略大小写。

 

二、如果属性名称不同

将UserDto的Name属性改成Name2

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     Mapper.Initialize(x => 
 2         x.CreateMap<User, UserDto>()
 3          .ForMember(d =>d.Name2, opt => {
 4             opt.MapFrom(s => s.Name);
 5             })
 6         );
 7 
 8     User user = new User()
 9     {
10         Id = 1,
11         Name = "caoyc",
12         Age = 20
13     };
14 
15     var dto = Mapper.Map<UserDto>(user);
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

 

三、使用Profile配置

自定义一个UserProfile类继承Profile,并重写Configure方法

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     public class UserProfile : Profile
 2     {
 3         protected override void Configure()
 4         {
 5             CreateMap<User, UserDto>()
 6                 .ForMember(d => d.Name2, opt =>
 7                 {
 8                     opt.MapFrom(s => s.Name);
 9                 });
10         }
11     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

使用时就这样

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     Mapper.Initialize(x => x.AddProfile<UserProfile>());
 2 
 3     User user = new User()
 4     {
 5         Id = 1,
 6         Name = "caoyc",
 7         Age = 20
 8     };
 9 
10     var dto = Mapper.Map<UserDto>(user);
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

 

四、空值替换NullSubstitute

空值替换允许我们将Source对象中的空值在转换为Destination的值的时候,使用指定的值来替换空值。

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     public class UserProfile : Profile
 2     {
 3         protected override void Configure()
 4         {
 5             CreateMap<User, UserDto>()
 6                 .ForMember(d => d.Name2, opt => opt.MapFrom(s => s.Name))
 7                 .ForMember(d => d.Name2, opt => opt.NullSubstitute("值为空"));
 8                 
 9         }
10     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
1     Mapper.Initialize(x => x.AddProfile<UserProfile>());
2 
3     User user = new User()
4     {
5         Id = 1,
6         Age = 20
7     };
8 
9     var dto = Mapper.Map<UserDto>(user);
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

结果为:

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

 

五、忽略属性Ignore

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     public class User
 2     {
 3         public int Id { get; set; }
 4         public string Name { get; set; }
 5         public int Age { get; set; }
 6     }
 7 
 8     public class UserDto
 9     {
10         public string Name { get; set; }
11         public int Age { get; set; }
12 
13     }
14 
15     public class UserProfile : Profile
16     {
17         protected override void Configure()
18         {
19             CreateMap<User, UserDto>().ForMember("Name", opt => opt.Ignore());
20         }
21     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

使用

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     Mapper.Initialize(x => x.AddProfile<UserProfile>());
 2 
 3     User user = new User()
 4     {
 5         Id = 1,
 6         Name="caoyc",
 7         Age = 20
 8     };
 9 
10     var dto = Mapper.Map<UserDto>(user);
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

结果:

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

 

六、预设值

如果目标属性多于源属性,可以进行预设值

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     public class User
 2     {
 3         public int Id { get; set; }
 4         public string Name { get; set; }
 5         public int Age { get; set; }
 6     }
 7 
 8     public class UserDto
 9     {
10         public string Name { get; set; }
11         public int Age { get; set; }
12         public string Gender { get; set; }
13 
14     }
15 
16     public class UserProfile : Profile
17     {
18         protected override void Configure()
19         {
20             CreateMap<User, UserDto>();
21         }
22     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

使用

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     Mapper.Initialize(x => x.AddProfile<UserProfile>());
 2 
 3     User user = new User()
 4     {
 5         Id = 1,
 6         Name="caoyc",
 7         Age = 20
 8     };
 9 
10     UserDto dto = new UserDto() {Gender = "男"};
11     Mapper.Map(user, dto);
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

 七、类型转换ITypeConverter

如果数据中Gender存储的int类型,而DTO中Gender是String类型

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
1     public class User
2     {
3         public int Gender { get; set; }
4     }
5 
6     public class UserDto
7     {
8         public string Gender { get; set; }
9     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

类型转换类,需要实现接口ITypeConverter

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     public class GenderTypeConvertert : ITypeConverter<int, string>
 2     {
 3         public string Convert(int source, string destination, ResolutionContext context)
 4         {
 5             switch (source)
 6             {
 7                 case 0:
 8                     destination = "男";
 9                     break;
10                 case 1:
11                     destination = "女";
12                     break;
13                 default:
14                     destination = "未知";
15                     break;
16             }
17             return destination;
18         }
19     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

配置规则

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     public class UserProfile : Profile
 2     {
 3         protected override void Configure()
 4         {
 5             CreateMap<User, UserDto>();
 6 
 7             CreateMap<int, string>().ConvertUsing<GenderTypeConvertert>();
 8             //也可以写这样
 9             //CreateMap<int, string>().ConvertUsing(new GenderTypeConvertert());
10         }
11     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

使用

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     Mapper.Initialize(x => x.AddProfile<UserProfile>());
 2 
 3     User user0 = new User() { Gender = 0 };
 4     User user1 = new User() { Gender = 1 };
 5     User user2 = new User() { Gender = 2 };
 6     var dto0= Mapper.Map<UserDto>(user0);
 7     var dto1 = Mapper.Map<UserDto>(user1);
 8     var dto2 = Mapper.Map<UserDto>(user2);
 9 
10     Console.WriteLine("dto0:{0}", dto0.Gender);
11     Console.WriteLine("dto1:{0}", dto1.Gender);
12     Console.WriteLine("dto2:{0}", dto2.Gender);
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

结果

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

 

八、条件约束Condition

当满足条件时才进行映射字段,例如人类年龄,假设我们现在人类年龄范围为0-200岁(这只是假设),只有满足在这个条件才进行映射

DTO和Entity

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
1     public class User
2     {
3         public int Age { get; set; }
4     }
5 
6     public class UserDto
7     {
8         public int Age { get; set; }
9     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

Profile

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
1     public class UserProfile : Profile
2     {
3         protected override void Configure()
4         {
5             CreateMap<User, UserDto>().ForMember(dest=>dest.Age,opt=>opt.Condition(src=>src.Age>=0 && src.Age<=200));
6         }
7     }
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

使用代码

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html
 1     Mapper.Initialize(x => x.AddProfile<UserProfile>());
 2 
 3     User user0 = new User() { Age = 1 };
 4     User user1 = new User() { Age = 150 };
 5     User user2 = new User() { Age = 201 };
 6     var dto0= Mapper.Map<UserDto>(user0);
 7     var dto1 = Mapper.Map<UserDto>(user1);
 8     var dto2 = Mapper.Map<UserDto>(user2);
 9 
10     Console.WriteLine("dto0:{0}", dto0.Age);
11     Console.WriteLine("dto1:{0}", dto1.Age);
12     Console.WriteLine("dto2:{0}", dto2.Age);
转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

输出结果

转载  c# automapper 使用(一)  https://www.cnblogs.com/caoyc/p/6367828.html

转载 c# automapper 使用(一) https://www.cnblogs.com/caoyc/p/6367828.html

上一篇:关于Android的app权限申请问题


下一篇:apply,call,bind函数作用与用法