c# automapper 使用

Posted JustDo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# automapper 使用相关的知识,希望对你有一定的参考价值。

一、最简单的用法

有两个类User和UserDto

 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     }

将User转换成UserDto也和简单

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);

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

 

二、如果属性名称不同

将UserDto的Name属性改成Name2

 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);

 

三、使用Profile配置

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

 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     }

使用时就这样

 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);

 

四、空值替换NullSubstitute

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

 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     }
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);

结果为:

 

五、忽略属性Ignore

 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     }

使用

 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);

结果:

 

六、预设值

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

 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     }

使用

 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);

 七、类型转换ITypeConverter

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

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     }

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

 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     }

配置规则

 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     }

使用

 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);

结果

 

八、条件约束Condition

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

DTO和Entity

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     }

Profile

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     }

使用代码

 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 使用的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C# 中使用 AutoMapper

C# automapper 嵌套对象条件映射

AutoMapper C#实体映射

AutoMapper在C#中的有趣应用

C# AutoMapper6.1.1使用

C#实现对象映射AutoMapper