我从一无所知中来,向着无限未知中去

闲时写的一个DataRow TO Entity类,静态扩展方法的形式,直接.使用

废话不多说,直接上代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Linq;

namespace Mapper
{
    public static class DataRowToEntity
    {
        /// <summary>
        /// 将一个DataRow转换成指定的对象
        /// </summary>
        /// <typeparam name="T">返回的对象类型</typeparam>
        /// <param name="row">需要转换的数据行</param>
        /// <returns>当row==null则返回null</returns>
        public static T ToEntity<T>(this DataRow row) where T : new()
        {
            if (row == null)
            {
                return default(T);
            }
            T item = new T();

            item = Activator.CreateInstance<T>();
            row.CopyToEntity(item);

            return item;
        }

        public static IList<T> ToEntity<T>(this IEnumerable<DataRow> rows) where T : new()
        {
            if (rows == null)
            {
                return new List<T>();
            }
            return rows.Select(r => r.ToEntity<T>()).ToList();
        }

        /// <summary>
        /// 将DataRow的数据赋值到指定的对象上
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="row"></param>
        public static void CopyToEntity(this DataRow row, object entity)
        {
            if (entity == null || row == null)
            {
                return;
            }
            PropertyInfo[] propertyInfos = entity.GetType().GetProperties();

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (!CanSetPropertyValue(propertyInfo, row))
                {
                    continue;
                }

                try
                {
                    if (row[propertyInfo.Name] is DBNull)
                    {
                        propertyInfo.SetValue(entity, null, null);
                        continue;
                    }
                    SetPropertyValue(entity, row, propertyInfo);
                }
                finally
                {

                }
            }
        }

        private static bool CanSetPropertyValue(PropertyInfo propertyInfo, DataRow adaptedRow)
        {
            if (!propertyInfo.CanWrite)
            {
                return false;
            }

            if (!adaptedRow.Table.Columns.Contains(propertyInfo.Name))
            {
                return false;
            }

            return true;
        }

        private static void SetPropertyValue(object entity, DataRow adaptedRow, PropertyInfo propertyInfo)
        {
            if (propertyInfo.PropertyType == typeof(DateTime?) ||
                propertyInfo.PropertyType == typeof(DateTime))
            {
                DateTime date = DateTime.MaxValue;
                DateTime.TryParse(adaptedRow[propertyInfo.Name].ToString(),
                    CultureInfo.CurrentCulture, DateTimeStyles.None, out date);

                propertyInfo.SetValue(entity, date, null);
            }
            else
            {
                propertyInfo.SetValue(entity, adaptedRow[propertyInfo.Name], null);
            }
        }
    }
}

  使用方法在需要使用的地方添加Mapper名称空间,在DataRow对象上面可以直接点出ToEntity扩展方法,泛型为需要转为的对象类型

只能DataRowToEntity不能反过来,主要是最近看到很多代码生成简单3层,觉得的不好,所以尝试乱写了一个,后面可能会写一个简单的映射框架,封装生成的代码

注意改类只适用于entity和数据库表结构完全一抹一样的结构才行,后期会推出可以自定义映射属性到列的功能,写的不好大家不要乱踩哈



posted @ 2015-09-14 22:37  何苦来哉  阅读(220)  评论(0)    收藏  举报

知识才让我觉得世界如此美好