Friday, 16 November 2012

Some Useful DataReader Extension Methods and Generic Data type converter


public static class DataReaderExtensions
    {
        #region "-- Public -- "

        #region "-- Methods --"

        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static List<T> MapToList<T>(this DbDataReader dr) where T : new()
        {
            List<T> RetVal = null;
            var Entity = typeof(T);
            var PropDict = new Dictionary<string, PropertyInfo>();

            try
            {
                if (dr != null && dr.HasRows)
                {
                    RetVal = new List<T>();
                    var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);

                    while (dr.Read())
                    {
                        T newObject = new T();
                        for (int Index = 0; Index < dr.FieldCount; Index++)
                        {
                            if (PropDict.ContainsKey(dr.GetName(Index).ToUpper()))
                            {
                                var Info = PropDict[dr.GetName(Index).ToUpper()];
                                if ((Info != null) && Info.CanWrite)
                                {
                                    var Val = dr.GetValue(Index);
                                    Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val, null);
                                }
                            }
                        }
                        RetVal.Add(newObject);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return RetVal;
        }

        public static List<T> MapToPrimitive<T>(this DbDataReader dr)
        {
            List<T> RetVal = null;
            try
            {
                if (dr != null && dr.HasRows)
                {
                    RetVal = new List<T>();

                    while (dr.Read())
                    {
                        RetVal.Add(TConverter.ChangeType<T>(Convert.ToString(dr.GetValue(0))));
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return RetVal;
        }

        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static T MapToSingle<T>(this DbDataReader dr) where T : new()
        {
            T RetVal = new T();
            var Entity = typeof(T);
            var PropDict = new Dictionary<string, PropertyInfo>();

            try
            {
                if (dr != null && dr.HasRows)
                {
                    var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);

                    dr.Read();
                    for (int Index = 0; Index < dr.FieldCount; Index++)
                    {
                        if (PropDict.ContainsKey(dr.GetName(Index).ToUpper()))
                        {
                            var Info = PropDict[dr.GetName(Index).ToUpper()];
                            if ((Info != null) && Info.CanWrite)
                            {
                                var Val = dr.GetValue(Index);
                                Info.SetValue(RetVal, (Val == DBNull.Value) ? null : Val, null);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return RetVal;
        }

        #endregion "-- Methods --"

        #endregion "-- Public -- "
    }

-----------------------------------------------------------


/// <summary>
/// Generic Data type converter
/// </summary>


public static class TConverter
    {
        public static T ChangeType<T>(object value)
        {
            return (T)ChangeType(typeof(T), value);
        }
        public static object ChangeType(Type t, object value)
        {
            TypeConverter tc = TypeDescriptor.GetConverter(t);
            return tc.ConvertFrom(value);
        }
    }

-----------------------------------------------------------------------------


Ex.

DbDataReader DbDR;
-----
-----
-----
ClassA ObjA = DbDR.MapToSingle<ClassA>();
List<ClassB> ObjBList = DbDR.MapToList<ClassB>();
List<int> IntValList = DbDR.MapToPrimitive<int>();

No comments:

Post a Comment