如果源類型的成員鏈上的屬性值為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支持開放泛型映射。下面創建兩個泛型類:
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();
測試結果如下:
因為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的話題就暫時先放放,不過以後肯定還會有關於AutoMapper的博客的。