随手写的一个对DataRow和DataSqlReader的扩展方法

因为觉得一般使用DataRow获取行数据时使用字符串 会有太多的不确定和类型判断所以想自己扩展一下

最后成果

    public class Model
    {
        public int objUserID { get; set; }
        public string FirstName { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            DataRow r = null;
            var contact= r.GetContact<Model>();//声明一个DataRow的链接
            //获取值类型
            var id=contact.GetValue(s => s.objUserID,s=>int.Parse(s));
            //获取引用类型
            var name = contact.GetClassValue(s => s.FirstName);
            //直接获取int类型
            id = contact.GetValueByDBNull(s => s.objUserID);

            System.Data.SqlClient.SqlDataReader reader = null;
            contact = reader.GetContact<Model>();//声明一个SqlDataReader的链接
            //获取值类型
            id = contact.GetValue(s => s.objUserID);
            //直接获取int类型
            id = contact.GetValueByDBNull(s => s.objUserID);
        }
    }

开始定义一部分接口,公开外部可访问的方法


    public  interface ContactBase<T>
    {
        /// <summary>
        /// 获取值类型
        /// </summary>
        TResult GetValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp, Func<string, TResult> convert=null);
        /// <summary>
        /// 获取引用类型
        /// </summary>
        TResult GetClassValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp,Func<object,TResult> convert=null)
            where TResult :class;
        /*获取指定类型*/
        int GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, int>> exp);
        int GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, double>> exp);
        int GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, string>> exp);
    }

然后写根据接口定义的抽象类,里面写一些工具方法和公共属性

    public abstract class ContactTools<T,TData>:ContactBase<T>
    {
        /// <summary>
        /// 存放row或reader的值
        /// </summary>
        private TData _data;
        protected ContactTools(TData data)
        {
            this._data = data;
        }
        /// <summary>
        /// 通过lambda获取row或reader值
        /// </summary>
        private object GetDBValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp, Func<string, object> getFunc)
        {
            if (exp == null || getFunc == null)
                return default(TResult);
            var member = exp.Body as System.Linq.Expressions.MemberExpression;
            var name = member.Member.Name;
            return getFunc(name);
        }
        /// <summary>
        /// 需要继承的改变
        /// </summary>
        public abstract virtual object GetData(TData data,string name);
        public TResult GetValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp, Func<string, TResult> convert = null)
        {
            var value = this.GetDBValue(exp,s=>this.GetData(this._data,s));
            if (value == DBNull.Value)
                return default(TResult);
            if (string.IsNullOrEmpty(value.ToString()))
                return default(TResult);
            if (convert == null)
                return (TResult)value;
            else
                return convert(value.ToString());
        }
        public TResult GetClassValue<TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> exp, Func<object, TResult> convert = null)
            where TResult : class
        {
            var value = this.GetDBValue(exp, s => this.GetData(this._data, s));
            if (value == DBNull.Value)
                return null;
            if (convert == null)
                return value as TResult;
            else
                return convert(value);
        }
        public int GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, int>> exp)
        {
            return this.GetValue(exp, s => int.Parse(s));
        }
        public double GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, double>> exp)
        {
            return this.GetValue(exp, s => double.Parse(s));
        }
        public string GetValueByDBNull(System.Linq.Expressions.Expression<Func<T, string>> exp)
        {
            var str =this.GetClassValue(exp);
            return str == null ? string.Empty : str;
        }
    }


然后实现DataRow

    public class DataRowContact<T> : ContactTools<T, DataRow>
    {
        public DataRowContact(DataRow row):base(row){}
        public override object GetData(DataRow data, string name)
        {
            return data[name];
        }
    }

实现DataSqlReader
    public class DataReaderContact<T> : ContactTools<T, System.Data.SqlClient.SqlDataReader>
    {
        public DataReaderContact(System.Data.SqlClient.SqlDataReader reader) : base(reader) { }
        public override object GetData(System.Data.SqlClient.SqlDataReader data, string name)
        {
            return data[name];
        }
    }

最后写扩展方法,扩展到DataRow和DataSqlReader中

    public static class MyLinq
    {
        public static ContactBase<T> GetContact<T>(this DataRow row)
        {
            return new DataRowContact<T>(row);
        }
        public static ContactBase<T> GetContact<T>(this System.Data.SqlClient.SqlDataReader reader)
        {
            return new DataReaderContact<T>(reader);
        }
    }




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。