最近在項(xiàng)目中用到了AutoMapper,在我項(xiàng)目中只用到了AutoMapper的一些簡(jiǎn)單功能。如對(duì)象轉(zhuǎn)換、將sql查詢出來的Datatable轉(zhuǎn)換為實(shí)體集合。將實(shí)體集合轉(zhuǎn)換為Datatable,我看了半天,好像AutoMapper不支持...,于是就自己實(shí)現(xiàn)了一個(gè)功能。
下面是我將用到的部分功能做了擴(kuò)展,主要還是用的擴(kuò)展方法,方便調(diào)用。
<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using System.Collections;
using System.Data;
using System.Reflection;
namespace LY.AutoMapper
{
public static class AutoMapperExtension
{
/// <summary>
/// 實(shí)體對(duì)象轉(zhuǎn)換
/// </summary>
/// <typeparam name="TDestination"></typeparam>
/// <param name="o"></param>
/// <returns></returns>
public static TDestination MapTo<TDestination>(this object o)
{
if (o == null)
throw new ArgumentNullException();
Mapper.CreateMap(o.GetType(), typeof(TDestination));
return Mapper.Map<TDestination>(o); ;
}
/// <summary>
/// 集合轉(zhuǎn)換
/// </summary>
/// <typeparam name="TDestination"></typeparam>
/// <param name="o"></param>
/// <returns></returns>
public static List<TDestination> MapTo<TDestination>(this IEnumerable o)
{
if (o == null)
throw new ArgumentNullException();
foreach (var item in o)
{
Mapper.CreateMap(item.GetType(), typeof(TDestination));
break;
}
return Mapper.Map<List<TDestination>>(o);
}
/// <summary>
/// 將 DataTable 轉(zhuǎn)為實(shí)體對(duì)象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> MapTo<T>(this DataTable dt)
{
if (dt == null || dt.Rows.Count == 0)
return default(List<T>);
Mapper.CreateMap<IDataReader, T>();
return Mapper.Map<List<T>>(dt.CreateDataReader());
}
/// <summary>
/// 將List轉(zhuǎn)換為Datatable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable MapToTable<T>(this IEnumerable list)
{
if (list == null)
return default(DataTable);
//創(chuàng)建屬性的集合
List<PropertyInfo> pList = new List<PropertyInfo>();
//獲得反射的入口
Type type = typeof(T);
DataTable dt = new DataTable();
//把所有的public屬性加入到集合 并添加DataTable的列
Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
foreach (var item in list)
{
//創(chuàng)建一個(gè)DataRow實(shí)例
DataRow row = dt.NewRow();
//給row 賦值
pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
//加入到DataTable
dt.Rows.Add(row);
}
return dt;
}
}
}
</pre>
將List轉(zhuǎn)換為DataTable,因?yàn)槲覜]看到AutoMapper支持(或許研究的不深...),所以這里用了反射的實(shí)現(xiàn)。這樣的話,這個(gè)擴(kuò)展類就支持從Datatable to List,List to Datatable了。
下面我們進(jìn)行測(cè)試。
新建兩個(gè)類,模擬Entity與Model。
<pre>
namespace LY.AutoMapper.Test
{
public class StudentEntity
{
public string Name { get; set; }
public int Age { get; set; }
}
}
</pre>
<pre>
namespace LY.AutoMapper.Test
{
public class StudentModel
{
public string Name { get; set; }
public int Age { get; set; }
}
}
</pre>
下面,是使用方法。
<pre>
private void btnTest_Click(object sender, EventArgs e)
{
StudentEntity stuEntity = new StudentEntity() { Name = "ly", Age = 29 };
//實(shí)體轉(zhuǎn)換
StudentModel stuModel = stuEntity.MapTo<StudentModel>();
//集合轉(zhuǎn)換
List<StudentEntity> strEntityList = new List<StudentEntity>()
{
new StudentEntity(){Name="aa",Age=15}
};
List<StudentModel> stuModelList = strEntityList.MapTo<StudentModel>();
#region Get DataTable
DataTable tb = new DataTable();
tb.Columns.Add("Name");
tb.Columns.Add("Age", typeof(Int32));
DataRow row1 = tb.NewRow();
row1[0] = "ly";
row1[1] = 27;
DataRow row2 = tb.NewRow();
row2[0] = "yy";
row2[1] = 26;
tb.Rows.Add(row1);
tb.Rows.Add(row2);
#endregion
//Datatable轉(zhuǎn)換為L(zhǎng)ist
List<StudentEntity> stuEntitys = tb.MapTo<StudentEntity>();
//List轉(zhuǎn)換為Datatable
DataTable tbTest = stuEntitys.MapToTable<StudentEntity>();
}
</pre>
當(dāng)然,如果項(xiàng)目中使用了EF,利用AutoMapper可以很方便的將數(shù)據(jù)持久層轉(zhuǎn)換為我們定義的Model,節(jié)省了很多代碼量。