That is, using ADO. NET also requires lightweight object ing, and ado.net object ing.
For whatever reason, sometimes framework staff abandon NH or EF and use native databases to access objects.
For beautiful programming, use the lightweight ing extension method I wrote.
Objective: To automatically convert SqlDataReader to the T type
The Code is as follows:
Public static class SqlDataReaderEx {// <summary> // cache key of attribute reflection Information: Type hashCode, value Attribute Information /// </summary> private static Dictionary <int, dictionary <string, PropertyInfo> propInfoCache = new Dictionary <int, Dictionary <string, PropertyInfo> (); /// <summary> /// convert SqlDataReader to the T type /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "reader"> </param> // <returns> </returns> public st Atic 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 defaultV Alue = null; // default value of the reference type or null type if (IsValueType) {if ((! Prop. PropertyType. IsGenericType) | (prop. PropertyType. IsGenericType &&! Prop. propertyType. getGenericTypeDefinition (). equals (typeof (Nullable <>) {defavalue value = 0; // default value of non-null type} 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> /// obtain the field information of the corresponding data table of a type. // </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> /// convert SqlDataReader to List <T> type /// </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> /// obtain the field name corresponding to this attribute in the data table. /// </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 datafieldattriach) {fieldname = (a as datafieldattrites ). name; break;} return fieldname. toLower ();}}
Reader ["fieldname"] is no longer needed in the project, such:
In other words, you only need to write as follows:
(x) => { res = x.To<StaffModel>(); }
X is the SqlDataReader type.
Of course, reflection is essential to the basic principle. The attributes of an object can be marked with the field name in the data table using the DataField feature. Otherwise, the attribute name is the same as the attribute name, and the field name is case insensitive.
The DataField feature is self-written and has only one Name attribute.
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; } } }
Is it very convenient? Although the wheel is repeated, it makes it easy to convert the entity by using the native database to access the object. The convenience brought by this can make up for everything.
Remember to click [recommended]