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


    一、最简单的用法

    有两个类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);
    复制代码

    输出结果

  • 相关阅读:
    64码高清网络电视V2.4.2
    无线网络探测V1.22
    居民身份证号码查询系统
    ORA-12505 TNS:listener does not currently know of SID given in connect descriptor
    weblogic项目部署问题--Deployment could not be created. Deployment creator is null.
    ORA-01461: can bind a LONG value only for insert into a LONG column 解决方法
    前端基础知识学习第五节(Vue篇)
    前端基础知识学习第三节
    前端基础知识学习第二节
    你真的会用parseInt方法吗?
  • 原文地址:https://www.cnblogs.com/Jeely/p/10716067.html
Copyright © 2020-2023  润新知