星空网 > 软件开发 > ASP.net

使用 AutoMapper 映射 IDataReader、DataSet、DataTable 到实体类

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使用 AutoMapper 映射 IDataReader、DataSet、DataTable 到实体类使用 AutoMapper 映射 IDataReader、DataSet、DataTable 到实体类


  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 到实体类

关键词:DataTable

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流