zoukankan      html  css  js  c++  java
  • 即使用ADO.NET,也要轻量级实体映射,比Dapper和Ormlite均快

    不管出于什么原因,有时候框架人员摒弃了NH或EF,而使用原生数据库访问对象。

    为了优美的编程,用上我写的轻量级映射扩展方法吧

    目的:将SqlDataReader自动转换成T类型

    代码如下:

        /// <summary>
        /// 提供将SqlDataReader转成T类型的扩展方法
        /// </summary>
        public static class SqlDataReaderEx
        {
            private static object _obj = new object();
            /// <summary>
            /// 属性反射信息缓存 key:类型的hashCode,value属性信息
            /// </summary>
            private static Dictionary<int, Dictionary<string, PropertyInfo>> propInfoCache = new Dictionary<int, Dictionary<string, PropertyInfo>>();
    
            /// <summary>
            /// 将SqlDataReader转成T类型
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="reader"></param>
            /// <returns></returns>
            public static T To<T>(this SqlDataReader reader)
              where T : new()
            {
                if (reader == null || reader.HasRows == false) return default(T);
    
                var res = new T();
                var propInfos = GetFieldnameFromCache<T>();
    
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    var n = reader.GetName(i).ToLower();
                    if (propInfos.ContainsKey(n))
                    {
                        PropertyInfo prop = propInfos[n];
                        var IsValueType = prop.PropertyType.IsValueType;
                        object defaultValue = null;//引用类型或可空值类型的默认值
                        if (IsValueType) {
                            if ((!prop.PropertyType.IsGenericType)
                                ||(prop.PropertyType.IsGenericType&&!prop.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))))
                            {
                                defaultValue = 0;//非空值类型的默认值
                            }
                        }
                        var v = reader.GetValue(i);
                        prop.SetValue(res, (Convert.IsDBNull(v) ? defaultValue : v), null);
                    }
                }
    
                return res;
            }
    
            private static Dictionary<string, PropertyInfo> GetFieldnameFromCache<T>()
            {
                Dictionary<string, PropertyInfo> res = null;
                var hashCode = typeof(T).GetHashCode();
                var filedNames = GetFieldname<T>();
                lock (_obj)
                {
                    if (!propInfoCache.ContainsKey(hashCode))
                    {
                        propInfoCache.Add(hashCode, filedNames);
                    }
                }
                res = propInfoCache[hashCode];
                return res;
            }
    
            /// <summary>
            /// 获取一个类型的对应数据表的字段信息
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            private static Dictionary<string, PropertyInfo> GetFieldname<T>()
            {
                var res = new Dictionary<string, PropertyInfo>();
                var props = typeof(T).GetProperties();
                foreach (PropertyInfo item in props)
                {                
                    res.Add(item.GetFieldName(), item);
                }
                return res;
            }
    
         
    
            /// <summary>
            /// 将SqlDataReader转成List<T>类型
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="reader"></param>
            /// <returns></returns>
            public static List<T> ToList<T>(this SqlDataReader reader)
                where T : new()
            {
                if (reader == null || reader.HasRows == false) return null;
                var res = new List<T>();
                while (reader.Read())
                {
                    res.Add(reader.To<T>());
                }
                return res;
            }  
            
            /// <summary>
            /// 获取该属性对应到数据表中的字段名称
            /// </summary>
            /// <param name="propInfo"></param>
            /// <returns></returns>
            public static string GetFieldName(this PropertyInfo propInfo)
            {
                var fieldname = propInfo.Name;
                var attr = propInfo.GetCustomAttributes(false);
                foreach (var a in attr)
                {
                    if (a is DataFieldAttribute)
                    {
                        fieldname = (a as DataFieldAttribute).Name;
                        break;
                    }
                }
                return fieldname.ToLower();
            }
        }

    在项目中再也需要reader["fieldname"]这样的惹人厌的写法了

    换言之,只需要这样写:

     Model model=SqlDataReader.To<Model>(); 

    基本原理当然少不了反射,实体的属性可以用DataField特性标记在数据表中的字段名称,否则与属性同名,字段名称不区分大小写。

    DataField特性是自己写的,只有一个Name属性。

        public class DataFieldAttribute : Attribute
        {
            public DataFieldAttribute()
            {
    
            }
            public DataFieldAttribute(string name)
            {
                m_name = name;
            }
            private string m_name = null;
    
            public string Name { get { return m_name; } set { m_name = value; } }
        }

    是不是很方便,虽然重复造轮子了,但是即使用了原生数据库访问对象,又轻松了转换了实体,带来的方便性可以弥补一切。

    记得点【推荐】

    性能测试:

    与OrmLite对比:

    从图中看出,在测试1000次的时候,本文的扩展方法比OrmLite的方法快1263ms,测试多次效率均与该结果基本相符。

    Test测试源码如下:

  • 相关阅读:
    一日一技:微信开发-自定义菜单
    Redis五种数据结构
    .NET 5 部署在docker上运行
    一日一技:微信开发-发送模板消息
    Redis快速入门及应用
    面试官扎心一问:防止重复请求提交,有什么方案?
    在Windows上安装Docker
    上班摸鱼神器—VSCode 里也可以看股票 & 基金实时数据
    C# Nuget程序集StackExchange.Redis操作Redis 及 Redis 视频资源 及 相关入门指令 牛逼不,全都有
    Mongodb 更新某一条记录 C#
  • 原文地址:https://www.cnblogs.com/langu/p/4572350.html
Copyright © 2011-2022 走看看