轉載 c# automapper 使用(一) https://www.cnblogs.com/caoyc/p/6367828.html
阿新 • • 發佈:2019-04-16
type 代碼 clas 約束 orm html efault () com
一、最簡單的用法
有兩個類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 使用(一) https://www.cnblogs.com/caoyc/p/6367828.html