本篇AutoMapper使用场景:
※ 类型转换,源string类型分别转换成int, DateTime,Type
※ 源和目标都包含复杂类型属性
※ 把源中的一个属性映射到目标中的多个属性
类型转换,源string类型分别转换成int, DateTime,Type
□ Domain model
public class Source2
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
}
□ View model
public class Destination2
{
public int Value1 { get; set; }
public DateTime Value2 { get; set; }
public Type Value3 { get; set; }
}
□ 派生TypeConverter<,>自定义转换
//string转换成DateTime类型
public class DateTimeTypeConverter : TypeConverter<string,DateTime>
{
protected override DateTime ConvertCore(string source)
{
return System.Convert.ToDateTime(source);
}
}
//string转换成Type类型
//当然,这里的string必须是当前项目下的某个类
public class TypeTypeConverter : TypeConverter<string, Type>
{
protected override Type ConvertCore(string source)
{
Type type = Assembly.GetExecutingAssembly().GetType(source);
return type;
}
}
□ 映射配置
Mapper.CreateMap<string, int>().ConvertUsing(Convert.ToInt32);
Mapper.CreateMap<string, DateTime>().ConvertUsing<DateTimeTypeConverter>();
Mapper.CreateMap<string,Type>().ConvertUsing<TypeTypeConverter>();
Mapper.CreateMap<Source2, Destination2>();
□ 要点
类型转换用Convertusing<>()方法
源和目标都包含复杂类型属性
□ Domain model
public class OuterSource
{
public int Value { get; set; }
public InnerSource Inner { get; set; }
}
public class InnerSource
{
public int OtherValue { get; set; }
}
□ View model
public class OuterDest
{
public int Value { get; set; }
public InnerDest Inner { get; set; }
}
public class InnerDest
{
public int OtherValue { get; set; }
}
□ 映射配置
Mapper.CreateMap<OuterSource, OuterDest>();
Mapper.CreateMap<InnerSource, InnerDest>();
□ 使用
public ActionResult Nest()
{
var source = new OuterSource()
{
Value = 5,
Inner = new InnerSource(){OtherValue = 15}
};
var dest = Mapper.Map<OuterSource, OuterDest>(source);
return View(dest);
}
□ 要点
当源和目标都具有复杂属性并对应,复杂属性对应的类也需要配置映射。
把源中的一个属性映射到目标中的多个属性
□ Domain model
public class CalendarEvent
{
public DateTime EventDate { 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; }
}
□ 自定义解析器
//转换成DateTime.Date
public class DateTimeToDateResolver : ValueResolver<CalendarEvent, DateTime>
{
protected override DateTime ResolveCore(CalendarEvent source)
{
return source.EventDate.Date;
}
}
//转换成DateTime.Hour
public class DateTimeToHourResolver : ValueResolver<CalendarEvent, int>
{
protected override int ResolveCore(CalendarEvent source)
{
return source.EventDate.Hour;
}
}
//转换成DateTime.Minute
public class DateTimeToMinuteResolver : ValueResolver<CalendarEvent, int>
{
protected override int ResolveCore(CalendarEvent source)
{
return source.EventDate.Minute;
}
}
□ 映射配置
Mapper.CreateMap<CalendarEvent, CalendarEventForm>()
.ForMember("EventDate", opt => opt.ResolveUsing<DateTimeToDateResolver>())
.ForMember("EventHour", opt => opt.ResolveUsing<DateTimeToHourResolver>())
.ForMember("EventMinute", opt => opt.ResolveUsing<DateTimeToHourResolver>());
□ 使用
public ActionResult Projection()
{
var calendarEvent = new CalendarEvent
{
EventDate = new DateTime(2008, 12, 15, 20, 30, 0),
Title = "Company Holiday Party"
};
CalendarEventForm from = Mapper.Map<CalendarEvent, CalendarEventForm>(calendarEvent);
return View(from);
}