.Net Core使用AutoMapper做对象关系映射

我想很多后端开发者,纠结于如何在Dto及表实体中做属性关系映射,因为真的太繁琐了。,

⒈如何使用?

1             Mapper.Initialize(cfg => cfg.CreateMap<UsersInputDto, Users>());
2             UsersInputDto input = new UsersInputDto()
3             {
4                 id = 1, firstname = "fan", lastname = "qi", uname = "fanqisoft", pwd = "admin", enabled = 1
5             };
6             Users user = Mapper.Map<Users>(input);

⒉映射前或映射后进行操作

  首先附上实体类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace AutoMapperTest.Entities
 6 {
 7     public class Users
 8     {
 9         public int id { get; set; }
10         public string fullname { get; set; }
11         public int? age { get; set; }
12         public string username { get; set; }
13 
14         public string password { get; set; }
15         public int? enabled { get; set; }
16 
17         public override string ToString()
18         {
19             return $"用户ID:{this.id} \n用户姓名:{this.fullname}\n用户名:{this.username} \n用户密码:{this.password} \n用户是否启用:{(this.enabled==1?‘是‘:‘否‘)}";
20         }
21     }
22 }

  InputDto

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace AutoMapperTest.Entities
 6 {
 7     public class UsersInputDto
 8     {
 9         public int id { get; set; }
10         public string firstname { get; set; }
11         public string lastname { get; set; }
12         public int? age { get; set; }
13         public string uname { get; set; }
14 
15         public string pwd { get; set; }
16         public int? enabled { get; set; }
17     }
18 }

  

  当前端InputDto传到后端时,我需要将Dto中的firstname及lastname合并转换为数据表中的fullname

1             Mapper.Initialize(cfg => 
2             {
3                 cfg.CreateMap<UsersInputDto, Users>().BeforeMap((dto, ent) => ent.fullname = dto.firstname + "_" + dto.lastname);
4             });

⒊条件映射,必须必要的条件后才会映射属性。

1             Mapper.Initialize(cfg => 
2             {
3                 cfg.CreateMap<UsersInputDto, Users>().ForMember(d => d.age, u => u.Condition(s => s.age >= 0 && s.age <= 120));
4             });

⒋属性对应映射,Dto中属性名  != 数据表属性名

1             Mapper.Initialize(cfg => 
2             {
3                 cfg.CreateMap<UsersInputDto, Users>().ForMember(d => d.username, u => u.MapFrom(s => s.uname))
4                                                      .ForMember(d => d.password, u => u.MapFrom(s => s.pwd)); 
5             });

⒌使用配置文件?实现Profile类并在构造器中初始化你的配置。

 1 using AutoMapper;
 2 using AutoMapperTest.Entities;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Text;
 6 
 7 namespace AutoMapperTest.AutoMapper
 8 {
 9     public class AutoMapperConfig:Profile
10     {
11         public AutoMapperConfig()
12         {
13             CreateMap<UsersInputDto, Users>();
14         }
15     }
16 }
1             Mapper.Initialize(cfg => 
2             {
3                 cfg.CreateMap<UsersInputDto, Users>().ForMember(d => d.username, u => u.MapFrom(s => s.uname))
4                                                      .ForMember(d => d.password, u => u.MapFrom(s => s.pwd));
5                 cfg.AddProfile<AutoMapperConfig>();   //添加一个配置文件
6             });

⒍Dto中数据类型和数据表不一致那就自定义转换器吧。

 1 using AutoMapper;
 2 using AutoMapperTest.Entities;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Text;
 6 
 7 namespace AutoMapperTest.AutoMapper
 8 {
 9     public class UsersConverter:ITypeConverter<Users,UsersOutputDto>
10     {
11         public UsersOutputDto Convert(Users source, UsersOutputDto destination, ResolutionContext context)
12         {
13             string[] names = source.fullname.Split("_");
14             return new UsersOutputDto()
15             {
16                 id = source.id,
17                 firstname = names[0],
18                 lastname = names[1]
19             };
20         }
21     }
22 }
1             Mapper.Initialize(cfg =>
2             {
3                 cfg.CreateMap<Users, UsersOutputDto>().ConvertUsing<UsersConverter>();
4             });
5             Users users = new Users()
6             {
7                 id = 1, fullname = "fan_qi",age = 25,username = "fanqisoft",password ="admin",enabled = 1
8             };
9             UsersOutputDto output = Mapper.Map<UsersOutputDto>(users);

 

 

 

  

.Net Core使用AutoMapper做对象关系映射

上一篇:C#实现手机发送验证码


下一篇:机器学习---用python实现感知机算法和口袋算法(Machine Learning PLA Pocket Algorithm Application)