国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

AutoMapper(六)

2019-11-14 13:48:22
字體:
供稿:網(wǎng)友

返回總目錄


List和數(shù)組

AutoMapper只要求元素類型的配置而不要求可能會用到的任何數(shù)組或者list類型。比如,我們有一個簡單的源和目標(biāo)類型:

public class Source{    public int Value { get; set; }}public class Destination{    public int Value { get; set; }}

支持所有的基本泛型集合,代碼如下:

class PRogram{    static void Main(string[] args)    {        Mapper.CreateMap<Source, Destination>();        var sources = new[]        {            new Source() {Value = 1},            new Source() {Value = 2},            new Source() {Value = 3},        };        IEnumerable<Destination> iEnumerableDests= Mapper.Map<IEnumerable<Destination>>(sources);        ICollection<Destination> iCollectionDests= Mapper.Map<ICollection<Destination>>(sources);        IList<Destination> iListDests= Mapper.Map<IList<Destination>>(sources);        List<Destination> listDests= Mapper.Map<List<Destination>>(sources);        Destination[] destsArr= Mapper.Map<Destination[]>(sources);        //這里只舉兩個例子,其他集合同理        foreach (var dest in iCollectionDests)        {            Console.Write(dest.Value+",");        }        Console.WriteLine();        foreach (var dest in destsArr)        {            Console.Write(dest.Value + ",");        }        Console.Read();    }}

以上代碼是集合和集合之間的映射,但是映射的配置CreateMap方法中只是配置的是類型之間的映射,而沒有設(shè)計任何集合類型。

測試結(jié)果如下,可見集合之間映射成功:

image

具體來說,支持的源集合類型包括:

  • IEnumerable
  • IEnumerable<T>
  • ICollection
  • ICollection<T>
  • IList
  • IList<T>
  • List<T>
  • Arrays

集合中的多態(tài)元素類型

很多時候,在我們的源和目標(biāo)類型中可能有一個類型層次關(guān)系。AutoMapper支持多態(tài)數(shù)組和集合,因此如果發(fā)現(xiàn)派生的源或者目標(biāo)類型,就會使用它們。

public class ParentSource{    public int Value1 { get; set; }}public class ChildSource : ParentSource{    public int Value2 { get; set; }}public class ParentDestination{    public int Value1 { get; set; }}public class ChildDestination : ParentDestination{    public int Value2 { get; set; }}

AutoMapper仍然要求顯示配置孩子映射,因為它不能“猜出”具體使用哪一個孩子目標(biāo)映射。

在Main方法中添加如下代碼:

Mapper.Initialize(c =>{    c.CreateMap<ParentSource, ParentDestination>()        .Include<ChildSource, ChildDestination>();    c.CreateMap<ChildSource, ChildDestination>();});var sources = new[]{    new ParentSource(){Value1 = 11},    new ChildSource(){Value2 = 22},    new ParentSource(),};var dests = Mapper.Map<ParentDestination[]>(sources);Console.WriteLine(dests[0]);Console.WriteLine(dests[1]);Console.WriteLine(dests[2]);

測試結(jié)果如下:

image

上面我們創(chuàng)建了一個源的數(shù)組,其中包含兩個ParentSource和一個ChildSource,所以兩個ParentSource成功地映射到了ParentDestination,而CreateMap配置中,ParentSource到ParentDestination的映射配置包含了ChildSource到ChildDestination的配置,所以執(zhí)行Mapper.Map<ParentDestination[]>(sources)的時候,也可以將ChildSource映射到ChildDestination。

映射繼承

在派生類中標(biāo)明繼承

上面的代碼,是在基類中配置繼承的,除此之外,也可以在派生類中配置繼承:

//在基類中配置繼承 Mapper.Initialize(c => {     c.CreateMap<ParentSource, ParentDestination>()         .Include<ChildSource, ChildDestination>();     c.CreateMap<ChildSource, ChildDestination>(); }); //在派生類中配置繼承 Mapper.Initialize(c => {     c.CreateMap<ParentSource, ParentDestination>();     c.CreateMap<ChildSource, ChildDestination>()         .IncludeBase<ParentSource, ParentDestination>(); });

繼承映射屬性

這里介紹一下額外的復(fù)雜性,因為一個屬性映射時可以有多種方式。下面是這些源的優(yōu)先級:

  • 顯式映射 (使用.MapFrom())
  • 繼承的顯式映射 
  • 慣例映射 (通過慣例匹配的屬性)
  • 忽略的屬性映射

下面來演示一下:

這里還是用上面定義的四個類:Order,OrderDto,PCOrder,MobileOrder:

//領(lǐng)域?qū)ο?/span>public class Order { }//電腦端訂單public class PCOrder : Order{    public string Referrer { get; set; }}//手機訂單public class MobileOrder : Order { }//Dtospublic class OrderDto{    public string Referrer { get; set; }}

配置映射的方法使用的是在父類中配置繼承映射

//在父類中配置繼承映射Mapper.CreateMap<Order, OrderDto>()    .Include<PCOrder,OrderDto>()    .Include<MobileOrder,OrderDto>()    .ForMember(o => o.Referrer, m => m.Ignore());//這里配置了忽略目標(biāo)屬性Referrer的映射Mapper.CreateMap<PCOrder,OrderDto>();Mapper.CreateMap<MobileOrder, OrderDto>();
// 執(zhí)行映射var order = new PCOrder() { Referrer = "天貓" };var mapped = Mapper.Map<OrderDto>(order);Console.WriteLine(mapped.Referrer);

執(zhí)行結(jié)果如下:

image

注意在我們的映射配置中,我們已經(jīng)忽略了Referrer(因為Order基類中不存在這個屬性),但是在基類的映射中,慣例比忽略的屬性有更高的優(yōu)先級,因而屬性仍然得到了映射。


上一篇:初探C#

下一篇:PDF文件分割和合并

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 遂平县| 宿松县| 定兴县| 南雄市| 阳谷县| 鄂伦春自治旗| 淮南市| 和政县| 泗洪县| 宜章县| 博客| 泰和县| 杂多县| 白朗县| 赤城县| 包头市| 南平市| 安阳市| 岐山县| 内丘县| 眉山市| 务川| 定陶县| 石棉县| 汽车| 牙克石市| 汉源县| 南宁市| 大理市| 扶沟县| 罗山县| 满洲里市| 沧州市| 区。| 溆浦县| 辉南县| 师宗县| 紫云| 定兴县| 敖汉旗| 友谊县|