AutoMapper——Map之實體的橋樑(二)

來源:互聯網
上載者:User

標籤:總結   orm   map   實體轉換   實際應用   

       我們在之前的文章中提到了ORM,然後用到了EF,今天我們再介紹一種實體轉換關係的模型AutoMapper。

一、是什麼

        AutoMapper是一個.NET的對象映射工具。主要作用是進行領域對象與貧血模型之間的轉換、資料庫查詢結果映射至實體物件。

         什麼是貧血模型?貧血模型(DTO,Data Transfer Object,就是說只包含屬性,只能儲存必須的資料,木有其它任何的多餘的方法資料,專門用於資料傳遞用的類型對象)。

        那這是什麼意思呢,比如在ORM中,與資料庫互動用的Model模型是具有很多屬性變數方法的實體。而當我們與其它系統(或系統中的其它結構)進行資料互動時,出於耦合性考慮或者安全性考慮或者效能考慮(總之就是各種考慮),我們不希望直接將這個Model模型傳遞給它們,這時我們會建立一個貧血模型儲存資料並傳遞。

       也正是這個原因,我們才需要在Model模型和DTO實體之間做相互的轉換。

二、怎麼用    1、兩個實體的屬性完全一樣

         在應用它之前我們先要在vs中引用它。點擊工具下的庫封裝管理員,選擇程式包管理主控台!然後輸入Install-PackageAutoMapper。或者右擊解決方案,在nuget管理中找到AutoMapper,安裝, 然後在引入AutoMapper,就可以放心的使用了

         Model實體

<span style="font-size:18px;">namespace testautomapper  {      //學生源實體類      public class studentSource      {          public string name { get; set; }          public int age {get;set;}          public string sex { get; set; }      }  }</span>  
         DTO實體
<span style="font-size:18px;">namespace testautomapper  {      //學生目標實體類      public class studentPurpost      {          public string name { get; set; }          public string age { get; set; }          public string sex { get; set; }      }  }</span>  
          單個實體轉換過程

<span style="font-size:18px;"><span style="font-size:18px;">namespace testautomapper  {      class Program      {          static void Main(string[] args)          {              //建立映射規則  ,第一個參數為源實體(Model),第二個為目標實體(DTO)            Mapper.CreateMap<studentSource, studentPurpost>();              //建立一個源實體              studentSource sSource = new studentSource              {                  name = "崔曉光",                  age = 23,                  sex = "男"              };              //進行轉換  ,得到目標實體(DTO)            var sPurpost = Mapper.Map<studentPurpost>(sSource);              Console.WriteLine(sPurpost.name + sPurpost.sex + sPurpost.age);  //輸出目標實體的屬性        }      }  </span>}</span>  

         多個實體轉換過程(實體集)

<span style="font-size:18px;"><span style="font-size:18px;">namespace testautomapper  {      class Program      {          static void Main(string[] args)          {              //建立一個轉換關係規則              Mapper.CreateMap<studentSource, studentPurpost>();              //建立一個實體集合              List<studentSource> list = new List<studentSource>();              studentSource s = new studentSource              {                  name = "cuixiaoguang",                  age = 13,                  sex = "nan"              };              list.Add(s);                //進行轉換              List<studentPurpost> sPurpostList = Mapper.Map<List<studentSource>, List<studentPurpost>>(list);              Console.WriteLine(sPurpostList[0].age + sPurpostList[0].name + sPurpostList[0].sex);          }      }  }</span></span>  

    2、兩個實體的屬性不完全一樣

         Model實體

<span style="font-size:18px;">namespace testautomapper  {      //學生源實體類      public class studentSource      {          public string Sname { get; set; }          public int age {get;set;}          public string sex { get; set; }      }  }</span> 

         DTO實體

<span style="font-size:18px;">namespace testautomapper  {      //學生目標實體類      public class studentPurpost      {          public string Pname { get; set; }          public string age { get; set; }          public string sex { get; set; }      }  }</span>  

         實體轉換(實體集合約理)

       AutoMapper使用ForMember來指定每一個欄位的映射規則: 
       引用:The each custom member configuration uses an action delegate to configure each member.
      還好有強大的lambda運算式,規則的定義簡單明了。 

<span style="font-size:18px;"><span style="font-size:18px;">namespace testautomapper  {      class Program      {          static void Main(string[] args)          {              //建立映射規則  ,第一個參數為源實體(Model),第二個為目標實體(DTO)            Mapper.CreateMap<studentSource, studentPurpost>();              //定義實體中不同的屬性,使之對應            map.ForMember(d => d.Sname, opt => opt.MapFrom(s => s.Pname));             //建立一個源實體              studentSource sSource = new studentSource              {                  Sname = "崔曉光",                  age = 23,                  sex = "男"              };              //進行轉換  ,得到目標實體(DTO)            var sPurpost = Mapper.Map<studentPurpost>(sSource);              Console.WriteLine(sPurpost.name + sPurpost.sex + sPurpost.age);  //輸出目標實體的屬性        }      }  </span>}</span> 

    3、兩個實體的屬性完全不一樣

         Model實體

<span style="font-size:18px;">namespace testautomapper  {      //學生源實體類      public class studentSource      {          public string Sname { get; set; }          public int Sage {get;set;}          public string Ssex { get; set; }      }  }</span> 

         DTO實體

<span style="font-size:18px;">namespace testautomapper  {      //學生目標實體類      public class studentPurpost      {          public string Pname { get; set; }          public string Page { get; set; }          public string Psex { get; set; }      }  }</span>  

         實體轉換

         AutoMapper使用ConstructUsing來指定每一個欄位的映射規則

<span style="font-size:18px;"><span style="font-size:18px;">namespace testautomapper  {      class Program      {          static void Main(string[] args)          {              //建立映射規則  ,第一個參數為源實體(Model),第二個為目標實體(DTO)            Mapper.CreateMap<studentSource, studentPurpost>();              //定義實體中所有不同的屬性,使之對應                        map.ConstructUsing(s => new studentPurpost                                          {                                                Pname= s.Sname,                                                Page= s.Sage,                                                Psex= s.Ssex                                          });             //建立一個源實體              studentSource sSource = new studentSource              {                  Sname = "崔曉光",                  age = 23,                  sex = "男"              };              //進行轉換  ,得到目標實體(DTO)            var sPurpost = Mapper.Map<studentPurpost>(sSource);              Console.WriteLine(sPurpost.name + sPurpost.sex + sPurpost.age);  //輸出目標實體的屬性        }      }  </span>}</span> 

         這裡還可以用ForMember,一個個的將實體屬性對應,不過感覺這樣比較麻煩所以就不寫了


     小編寄語:通過本次講述,我們知道了AutoMapper是實體轉換模型,他可以將兩個相同或者不相同的實體進行相互轉化,還可以轉化兩個實體集,當然AutoMapper的功能遠不止這些,它更多的功能還需要我們進一步瞭解。其實我們在實際工作中沒必要非用AutoMapper來轉化,只是我們用了之後事情會變的很簡單,所以我們不僅要完成任務,更要提高完成任務的效率,這就要求我們多聽多看,博覽群書,見多識廣,這樣才能夠充分的利用已有的知識,提高我們的生活品質。




AutoMapper——Map之實體的橋樑(二)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.