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

测试结果如下:

image

开放泛型

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

测试结果如下:

image

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

测试结果如下:

image

 

好的,关于AutoMapper的系列教程就先告一段落了,当时开这系列教程的初衷是为了顺利地进行项目地开发,现在会用了,关于AutoMapper的话题就暂时先放放,不过以后肯定还会有关于AutoMapper的博客的。

posted @ 2015-11-07 18:09  tkbSimplest  阅读(4771)  评论(2编辑  收藏  举报