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

來源:互聯網
上載者:User

標籤:c#   .net   dataset   

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>();
}

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

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 源碼

using AutoMapper;using System.Data;namespace Utitity.AutoMapper{    /// <summary>    /// 實體映射協助類    /// </summary>    public static class MapperHelper    {        #region 配置映射規則        /// <summary>        /// 確保映射配置只註冊一次        /// </summary>        static MapperHelper()        {            Configure();        }        /// <summary>        /// 註冊 Mapper 轉換規則約定        /// </summary>        static void Configure()        {            Mapper.CreateMap<IDataReader, xxxxModel>();//只需要約定基礎類型,不要要寫成List<xxxxModel>這種形式        }        #endregion        #region 實體映射擴充方法        /// <summary>        /// 將 IDataReader 轉為實體物件        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="dr"></param>        /// <returns></returns>        public static T GetEntity<T>(this IDataReader dr)        {            return Mapper.Map<T>(dr);        }        /// <summary>        /// 將 DataSet 轉為實體物件        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="ds"></param>        /// <returns></returns>        public static T GetEntity<T>(this DataSet ds)        {            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)                return default(T);            var dr = ds.Tables[0].CreateDataReader();            return Mapper.Map<T>(dr);        }        /// <summary>        /// 將 DataTable 轉為實體物件        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="dt"></param>        /// <returns></returns>        public static T GetEntity<T>(this DataTable dt)        {            if (dt == null || dt.Rows.Count == 0)                return default(T);            var dr = dt.CreateDataReader();            return Mapper.Map<T>(dr);        }        #endregion    }}



使用 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.