C# AutoMapper的簡單擴展


AutoMapper可以很方便的將一個實體的屬性值轉化給另一個對象。這個功能在我們日常的編碼中經常會遇到。我將AutoMapper的一些基本映射功能做成擴展方法,在編碼中更方便使用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using System.Collections;
using System.Data;
using System.Reflection;

namespace NFMES.Core.Type
{
    public static class AutoMapperExtension
    {
        /// <summary>
        /// 實體對象轉換
        /// </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>
        /// 集合轉換
        /// </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 轉為實體對象  
        /// </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轉換為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);

            //創建屬性的集合
            List<PropertyInfo> pList = new List<PropertyInfo>();
            //獲得反射的入口
            System.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)
            {
                //創建一個DataRow實例
                DataRow row = dt.NewRow();
                //給row 賦值
                pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
                //加入到DataTable
                dt.Rows.Add(row);
            }
            return dt;
        }

    }
}

這個靜態類中有4個擴展方法,分別對Object類型,IEnumberable類型,DataTable類型添加了MapTo方法,可以方便的將對象映射到對象,集合映射到集合,表映射到集合,集合映射到表(這個功能AutoMapper不支持,我用反射實現的)

單實體轉化使用方式:

集合轉化的使用方式:

     AutoMapper還有很多功能,我這個類只擴展了一些最基本和常用的功能,以后用到會繼續添加。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM