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

首頁 > 編程 > .NET > 正文

五步掌握OOM框架AutoMapper基本使用

2024-07-10 13:31:58
字體:
來源:轉載
供稿:網友

寫在前面

OOM顧名思義,Object-Object-Mapping實體間相互轉換,AutoMapper也是個老生常談了,其意義在于幫助你無需手動的轉換簡單而又麻煩的實體間關系,比如ViewModel和entity的轉換,SearchModel和Entity的轉換,我這篇分享的意義在于,網上大多數的分享都是幾年前的,很多方法已經被廢棄,到了編譯器里會告訴你該方法已經過時,廢棄的,不建議使用的,比如Mapper.CreateMap等方法,當然老司機大多數直接就去github看文檔了,或者google一下就了解了,但是中文資料關于方法廢棄后,并沒有什么說明了。本篇的五個實例可以幫你解決常見的基本問題.

預備

首先我們預備一些ViewModel和TModel。ViewModel就是你和用戶交互的實體。TModel就是你與數據庫打交道的實體。

實體展示如下:

TModel有如下三個簡單的實體,他們有獨立的實體,也有一對多的實體。

public class TAddress{ public string Country { get; set; } public string City { get; set; } public string Street { get; set; } public string PostCode { get; set; } public string CreateTime { get; set; } public int CreateUserId { get; set; }}
public class TAuthor {   public string Name { get; set; }   public string Description { get; set; }   public List<TContactInfo> ContactInfo { get; set; } }  public class TContactInfo {  public int Id { get; set; }  public string Email { get; set; }  public string Blog { get; set; }  public string Twitter { get; set; } }

ViewModel如下三個:

public class VM_Address {  public string Country { get; set; }  public string City { get; set; }  public string City2 { get; set; } } public class VM_Author {  public string Name { get; set; }  public string Description { get; set; }  public List<VM_ContactInfo> ContactInfo { get; set; } } public class VM_ContactInfo {  public int Id { get; set; }  public string Email { get; set; }  public string Blog { get; set; }  public string Twitter { get; set; } }

單個實體轉換

單個實體轉換的時候,在屬性字段名稱完全匹配的情況下,你只需指定兩個實體間的轉換規則,指定source源實體和destination目標實體。那么你應該參照如下實例:

VM_Address dto = new VM_Address   {    Country = "China",    City = "Beijing"   };   Mapper.Initialize(m => m.CreateMap<VM_Address, TAddress>());   TAddress address = Mapper.Map<VM_Address, TAddress>(dto);

請注意在AutoMapper5.x當中,Initialize來初始化你的規則是首選的。

在你指定轉換規則后,請使用Map方法,進行轉換并輸出你的目標實體。還有第一個參數代表SourceModel,第二個參數是DestinationModel.

單個實體不同名屬性轉換

當你需要對不同名稱的字段來進行映射的時候,請注意使用ForMember方法,第一個參數需要你制定所需特殊配置的目標字段,第二個參數你則需要制定你對該字段屬性的操作,我選擇了它提供的MapFrom方法,意義在于告訴AutoMapper,我需要講目標實體的City來源 指定為 源實體的City2屬性值。

VM_Address dto = new VM_Address   {    Country = "China",    City2 = "Beijing"   };   Mapper.Initialize(m => m.CreateMap<VM_Address, TAddress>().ForMember(x => x.City, opt => opt.MapFrom(o => o.City2)));   TAddress address = Mapper.Map<VM_Address, TAddress>(dto);

集合轉換

在集合間轉換的時候,你不需要配置目標List與源List對象中的匹配,而只需要配置你泛型對象的映射匹配關系。

   TAddress address = new TAddress { Country = "China", City = "Beijing" };   TAddress address2 = new TAddress() { Country = "USA", City = "New York" };   List<TAddress> addressList = new List<TAddress>() { address2, address };   Mapper.Initialize(m => m.CreateMap<TAddress, VM_Address>());//這里僅需配置實體間的轉換,而不是實體集合的轉換   List<VM_Address> res = Mapper.Map<List<TAddress>, List<VM_Address>>(addressList);

實體包含不同類型屬性轉換(忽略屬性)

在實體包含不同類型屬性的時候,比如TModel1中包含了一個List<TModel>,而你的ViewModel1中包含了一個List<ViewModel>.這個時候你可以選擇忽略這個屬性

 var contacts = new List<TContactInfo>() { new TContactInfo()           { Blog = "myblog", Email = "ws@qq.com" }, new TContactInfo() { Blog = "myblog", Email = "ll@qq.com" } };   TAuthor author = new TAuthor() { Description = "描述", Name = "吳雙", ContactInfo = contacts };   Mapper.Initialize(m => { m.CreateMap<TAuthor, VM_Author>().ForMember(x => x.ContactInfo, opt => opt.Ignore()); });       VM_Author dto = Mapper.Map<TAuthor, VM_Author>(author);//這里的Ignore代表配置ContractInfo該屬性的操作 為 忽略Ignore,映射時將忽略該屬性 由于List<TContactInfo>()和List<VM_ContactInfo>() 是不同類型,所以需要配置忽略或者是特殊映射,特殊映射例子看下方

實體包含不同類型屬性轉換(指定屬性Mapfrom)

當然你需要這個屬性的時候,你可以不忽略他,而是使用MapFrom來進行特殊的指定,并且在類型不相同的時候,你要指定你兩個類型間的映射匹配關系。正如下面實例中的

m.CreateMap<TContactInfo, VM_ContactInfo>();和
m.CreateMap<TAuthor, VM_Author>().ForMember(x => x.ContactInfo, opt => opt.MapFrom(o => o.ContactInfo));

var contacts = new List<TContactInfo>()   {    new TContactInfo() { Blog = "myblog", Email = "ws@qq.com" },    new TContactInfo() { Blog = "myblog", Email = "ll@qq.com" }   };   TAuthor author = new TAuthor() { Description = "描述", Name = "吳雙", ContactInfo = contacts };   Mapper.Initialize(m =>   {    m.CreateMap<TContactInfo, VM_ContactInfo>();//注意 內部不同類型實體轉換時必要的    m.CreateMap<TAuthor, VM_Author>().ForMember(x => x.ContactInfo, opt => opt.MapFrom(o => o.ContactInfo));//注意 制定MapFrom是必要的   });   VM_Author dto = Mapper.Map<TAuthor, VM_Author>(author);

寫在最后

在實體轉換中,AutoMapper的必要性和實用性已經被你一覽無余。

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網!

 

注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定远县| 闽侯县| 延安市| 三台县| 出国| 元阳县| 东莞市| 塔城市| 来凤县| 明光市| 阿坝| 增城市| 崇信县| 甘洛县| 宝清县| 南安市| 华池县| 杭州市| 合作市| 衡山县| 乾安县| 四子王旗| 高安市| 鸡东县| 宣汉县| 定南县| 上饶县| 大厂| 福建省| 阜南县| 宝丰县| 平和县| 巫山县| 丁青县| 邢台市| 乌苏市| 宜昌市| 临沂市| 鲁山县| 巴塘县| 木里|