AutoMapper介紹(未完待續、部分沒實現)

來源:互聯網
上載者:User

標籤:json   custom   官網   www.   orm   編譯   ret   問題   失敗   

實體間轉換工具。其實也可以用Json來實現同名屬性、異名屬性(用JsonProperty指明)的自動轉換

 

最新版本6.11

需要使用vs2013以上。vs2012下載新版 nuget會遇到問題、只能舊版。

 1 using AutoMapper; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Mvc; 7  8 namespace AutoMapperVs2015.Controllers 9 {10     public class HomeController : Controller11     {12         public ActionResult Index()13         {14             return View();15         }16 17         public ActionResult About()18         {19             ViewBag.Message = "Your application description page.";20 21             return View();22         }23 24         public ActionResult Contact()25         {26             ViewBag.Message = "Your contact page.";27 28             return View();29         }30 31         public JsonResult TestAutoMaper()32         {33             //Table=>DTO。屬性相同自動匹配;且組件支援命名規範的屬性名稱自動匹配34             Mapper.Initialize(cfg =>    //https://github.com/AutoMapper/AutoMapper/wiki/Getting-started35             {36                 cfg.CreateMap<TB_MENU, DTO_MENU>();37                 //cfg.CreateMap<DTO_MENU, TB_MENU>();38             });39             var tbMenu = new TB_MENU() { MENU_NAME = "許可權管理", MENU_LEVEL = "1" };40             var dto = Mapper.Map<DTO_MENU>(tbMenu);41             return Json(dto, JsonRequestBehavior.AllowGet);42 43             //DTO=>Table。屬性相同自動匹配;且組件支援命名規範的屬性名稱自動匹配44             //Mapper.Initialize(cfg => {45             //    //cfg.CreateMap<TB_MENU, DTO_MENU>();46             //    cfg.CreateMap<DTO_MENU, TB_MENU>();47             //});48             //var dtoMenu = new DTO_MENU() { MENU_NAME = "許可權管理2", MENU_LEVEL = "2" };49             //var tbMenu = Mapper.Map<TB_MENU>(dtoMenu);50             //return Json(tbMenu, JsonRequestBehavior.AllowGet);51 52 53             //Table=>DTO。屬性相同自動匹配;不相同且不符合命名規範的屬性,需要人工定義對應匹配關係54             //TODO:人工定義方法未知、沒查到相關文檔,舊文檔已失效(http://www.cnblogs.com/xishuai/p/3700052.html)。55             //Mapper.Initialize(cfg =>56             //{57             //    cfg.CreateMap<TB_MENU, DTO_MENU>().ForMember(),58             //});59             //var dtoMenu = new DTO_MENU() { MENU_NAME = "許可權管理2", MENU_LEVEL = "2" };60             //var tbMenu = Mapper.Map<TB_MENU>(dtoMenu);61             //return Json(tbMenu, JsonRequestBehavior.AllowGet);62 63         }64     }65 66     public class TB_MENU67     {68         public TB_MENU()69         {70         }71 72         public string MENU_LEVEL { get; set; }73         public string MENU_NAME { get; set; }74     }75 76     public class DTO_MENU77     {78         public DTO_MENU()79         {80         }81 82         public string MENU_LEVEL { get; set; }83         public string MENU_NAME { get; set; }84     }85 }

未完待續://TODO:人工定義方法未知、沒查到相關文檔,舊文檔已失效(http://www.cnblogs.com/xishuai/p/3700052.html)。

新文檔:https://github.com/AutoMapper/AutoMapper/wiki/Getting-started(官網)

舊文檔參考(好多方法在新版中已經不正確、棄用、vs編譯失敗)

http://www.cnblogs.com/xishuai/p/3700052.html

論點:屬性相同自動匹配;且組件支援命名規範的屬性名稱自動匹配。                      這點也可以用json實現,本文開頭已提到。

可以看到配置相當的簡單,只要設定下Order和OrderDto之間的類型映射就可以了,我們看OrderDto中的CustomerName和Total屬性在領域模型Order中並沒有與之相對性,沒什麼可以轉換呢,感覺好神奇的樣子,其實仔細發現這些屬性的命名都有一定的規則,AutoMapper在做解析的時候會按照PascalCase(帕斯卡命名法),就是一種變數命名法,除了PascalCase還有Hungarian(匈牙利命名法)和camelCase(駱駝命名法),PascalCase就是指混合使用大小寫字母來構成變數和函數的名字,首字母要大寫,camelCase首字母小寫,我們C#命名中,一般使用的是camelCase和PascalCase,比較進階的是PascalCase。

  但是為什麼AutoMapper會解析Total呢?因為在領域模型Order中有個GetTotal()方法,AutoMapper會解析“Get”之後的單詞,所以會與Total相對應,如果你把OrderDto的屬性“Total”改為“Totals”,就會發現得到的“Totals”為0。理解了AutoMapper的解析方式,我們就要注意在編寫變數、屬性或是方法名稱的時候一定要規範,這也是一種好的習慣。

http://blog.csdn.net/csethcrm/article/details/52934325

 

AutoMapper介紹(未完待續、部分沒實現)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.