星空网 > 软件开发 > ASP.net

AutoMapper(七)

返回总目录


Null值替换

如果源类型的成员链上的属性值为Null,Null值替换允许提供一个可替换的值。下面有两个类Person和PersonInfo类,都有一个属性Title(头衔),从Person映射到PersonInfo,如果Person的属性没有赋值,那么PersonInfo的对应属性值就用“屌丝”来替换。

namespace SeventhAutoMapper{  class Person  {    public string Title { get; set; }   }  class PersonInfo  {    public string Title { get; set; }  }  class Program  {    static void Main(string[] args)    {      //映射      Mapper.CreateMap<Person, PersonInfo>()        .ForMember(dest => dest.Title, opt => opt.NullSubstitute("屌丝"));//源属性如果为null,置为“屌丝”      //执行映射      var personInfo = Mapper.Map<PersonInfo>(new Person());//源属性未赋值,故为null     var personInfo2 = Mapper.Map<PersonInfo>(new Person(){Title = "高富帅"});//源属性有值      //输出结果      Console.WriteLine("personInfo.Title=" + personInfo.Title);     Console.WriteLine("personInfo2.Title=" + personInfo2.Title);     Console.Read();    }  }}

测试结果如下:

AutoMapper(七)

开放泛型

AutoMapper支持开放泛型映射。下面创建两个泛型类:

class Soure<T>{  public T Value { get; set; }}class Destination<T>{  public T Value { get; set; }}

我们不需要创建封闭的泛型类型(也就是不将具体的类型写死在尖括号内),AutoMapper在运行时会将开放泛型的任何配置应用到关闭的映射上去。

//创建开放泛型的映射Mapper.CreateMap(typeof(Source<>),typeof(Destination<>));var src1 = new Source<int> {Value = 22};var dest1= Mapper.Map<Destination<int>>(src1);Console.WriteLine(dest1.Value);var src2 = new Source<string> {Value = "Hello,AutoMapper!"};var dest2 = Mapper.Map<Destination<string>>(src2);Console.WriteLine(dest2.Value);//......依次类推Console.Read();

测试结果如下:

AutoMapper(七)

因为C#只允许关闭的泛型形参,所以必须使用不带泛型参数的CreateMap方法来创建自己的开放泛型参数映射,同时可以使用所有可以利用的映射配置。AutoMapper在配置验证期间会跳过开放泛型类型映射。

也可以创建一个开放泛型转换器:

Mapper.CreateMap(typeof(Source<>), typeof(Destination<>)).ConvertUsing(typeof(Converter<>));

投影

通过扁平化对象模型,将一个源类型转换成一个目标类型。不需要额外的配置,AutoMapper只要求一个扁平的目标类型能匹配源类型的命名结构。当把一个源值投影到一个不精准匹配源结构的目标值时,必须指明成员映射定义。

举个栗子,我们想把一个源结构CalendarEvent转成一个在Web页面上方便用户输入的目标结构CalendarEventForm:

public class CalendarEvent{  public DateTime Date { get; set; }  public string Title { get; set; }}public class CalendarEventForm{  public DateTime EventDate { get; set; }  public int EventHour { get; set; }  public int EventMinute { get; set; }  public string Title { get; set; }}

由于目标属性不是很匹配源属性(CalendarEvent.Date需要成为CalendarEventForm.EventDate),我们需要在类型映射配置中指明成员的映射规则:

var calender = new CalendarEvent(){  Date = DateTime.Now,  Title = "历史性时刻"};//创建映射Mapper.CreateMap<CalendarEvent, CalendarEventForm>()  .ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.Date.Date))  .ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.Date.Hour))  .ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.Date.Minute));//执行映射var calenderForm = Mapper.Map<CalendarEventForm>(calender);//输出映射前的对象Console.WriteLine("calender.Date={0},Title={1}",calender.Date,calender.Title);//输出映射后的对象foreach (PropertyInfo info in calenderForm.GetType().GetProperties()){  Console.WriteLine(info.Name+"="+info.GetValue(calenderForm));}

测试结果如下:

AutoMapper(七)




原标题:AutoMapper(七)

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

【规范申报】如何修改或撤销进出口货物报关单?:https://www.kjdsnews.com/a/777550.html
怎么回事?价格才提高0.1美金,就被亚马逊禁售?:https://www.kjdsnews.com/a/777557.html
适合独立站的12款实用工具(2):https://www.kjdsnews.com/a/777558.html
eBay封号会有什么后果?:https://www.kjdsnews.com/a/777564.html
DTC品牌Kuru Footwear是如何做付费搜索的?:https://www.kjdsnews.com/a/777568.html
外媒:eBay老卖家粘性强,但拉新还需多努力:https://www.kjdsnews.com/a/777569.html
独家丨B站广告位可跳转美团APP B站为电商平台引流再升级 :https://www.kjdsnews.com/a/1836410.html
百崖大峡谷生态旅游景区(探秘中国西南自然风光):https://www.vstour.cn/a/363176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流