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

即使用ADO.NET,也要轻量级实体映射

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

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

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

代码如下:

 public static class SqlDataReaderEx  {    /// <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();      if (!propInfoCache.ContainsKey(hashCode))      {        propInfoCache.Add(hashCode, GetFieldname<T>());      }      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.GetFiledName(), 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 GetFiledName(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"]这样的惹人厌的写法了,如:

即使用ADO.NET,也要轻量级实体映射

换言之,只需要这样写:

 (x) => { res = x.To<StaffModel>(); }

x是SqlDataReader类型。

基本原理当然少不了反射,实体的属性可以用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; } }  }

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

记得点【推荐】




原标题:即使用ADO.NET,也要轻量级实体映射

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

考拉速汇:https://www.ikjzd.com/w/1573201669718974465
Karmaloop:https://www.ikjzd.com/w/1573201673221562370
Karrot:https://www.ikjzd.com/w/1573201675251605506
哈萨克斯坦市场:https://www.ikjzd.com/w/1573201685628313601
Kazpost:https://www.ikjzd.com/w/1573201687851294721
Keenu:https://www.ikjzd.com/w/1573201689348661250
23点聊电商:新质生产力加速数字贸易发展 卓尔智联集团实现营收利润双增长 :https://www.kjdsnews.com/a/1836411.html
南京浦口都有什么好玩的地方 南京浦口都有什么好玩的地方推荐:https://www.vstour.cn/a/363180.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流