使用 AutoMapper 映射 IDataReader、DataSet、DataTable 到實體類

來源:互聯網
上載者:User

標籤:

AutoMapper是一個.NET的對象映射工具。

項目地址:https://github.com/AutoMapper/AutoMapper。

協助文檔:https://github.com/AutoMapper/AutoMapper/wiki

 

主要用途

領域對象與DTO之間的轉換、資料庫查詢結果映射至實體物件。

這裡主要說下使用 AutoMapper 將 IDataReader、DataSet、DataTable 轉為實體的方法。

依賴檔案:AutoMapper.dll、AutoMapper.Net4.dll 兩個 dll 檔案

AutoMapper.Net4.dll 這個檔案可以下載代碼自己編譯,該檔案是封裝了對 IDataReader 的支援

簡單說下 AutoMapper 使用

第一步:宣告對應約定

Mapper.CreateMap<IDataReader, menuModel>();//將 IDataReader 映射為 menuModel 實體

第二步:轉換實體

//IDataReader =>menuModel

using (IDataReader dr = .....) {     var list  = Mapper.Map<List<menuModel>>(dr);     dr.Close(); }

以下是自己封裝的一個 AutoMapper 協助類

使用方法:

第一步:將要轉換的模型在 靜態建構函式中 約定下

/// <summary> /// 註冊 Mapper 轉換規則約定 /// </summary> static void Configure() { Mapper.CreateMap<IDataReader, menuModel>();

...

Mapper.CreateMap<IDataReader, xxxxxModel>(); }

第二步:在項目中應用程式集,使用已經寫好的擴充方法

 

[csharp] view plaincopy
  1. using Utitity.AutoMapper  

 

IDataReader dr = ...; var list1 = dr.GetEntity<List<menuModel>>(); DataSet ds = ...; var list2 = ds.GetEntity<List<menuModel>>(); DataTable dt = ...; var list3 = dt.GetEntity<List<menuModel>>();

MapperHelper 源碼

 

[csharp] view plaincopy
  1. using AutoMapper;  
  2. using System.Data;  
  3.   
  4. namespace Utitity.AutoMapper  
  5. {  
  6.     /// <summary>  
  7.     /// 實體映射協助類  
  8.     /// </summary>  
  9.     public static class MapperHelper  
  10.     {  
  11.         #region 配置映射規則  
  12.         /// <summary>  
  13.         /// 確保映射配置只註冊一次  
  14.         /// </summary>  
  15.         static MapperHelper()  
  16.         {  
  17.             Configure();  
  18.         }  
  19.         /// <summary>  
  20.         /// 註冊 Mapper 轉換規則約定  
  21.         /// </summary>  
  22.         static void Configure()  
  23.         {  
  24.             Mapper.CreateMap<IDataReader, xxxxModel>();//只需要約定基礎類型,不要要寫成List<xxxxModel>這種形式  
  25.         }  
  26.  
  27.         #endregion  
  28.  
  29.  
  30.         #region 實體映射擴充方法  
  31.         /// <summary>  
  32.         /// 將 IDataReader 轉為實體物件  
  33.         /// </summary>  
  34.         /// <typeparam name="T"></typeparam>  
  35.         /// <param name="dr"></param>  
  36.         /// <returns></returns>  
  37.         public static T GetEntity<T>(this IDataReader dr)  
  38.         {  
  39.             return Mapper.Map<T>(dr);  
  40.         }  
  41.         /// <summary>  
  42.         /// 將 DataSet 轉為實體物件  
  43.         /// </summary>  
  44.         /// <typeparam name="T"></typeparam>  
  45.         /// <param name="ds"></param>  
  46.         /// <returns></returns>  
  47.         public static T GetEntity<T>(this DataSet ds)  
  48.         {  
  49.             if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)  
  50.                 return default(T);  
  51.             var dr = ds.Tables[0].CreateDataReader();  
  52.             return Mapper.Map<T>(dr);  
  53.         }  
  54.         /// <summary>  
  55.         /// 將 DataTable 轉為實體物件  
  56.         /// </summary>  
  57.         /// <typeparam name="T"></typeparam>  
  58.         /// <param name="dt"></param>  
  59.         /// <returns></returns>  
  60.         public static T GetEntity<T>(this DataTable dt)  
  61.         {  
  62.             if (dt == null || dt.Rows.Count == 0)  
  63.                 return default(T);  
  64.             var dr = dt.CreateDataReader();  
  65.             return Mapper.Map<T>(dr);  
  66.         }  
  67.  
  68.         #endregion  
  69.     }  

使用 AutoMapper 映射 IDataReader、DataSet、DataTable 到實體類

聯繫我們

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