c# 数据请求方式提供

 营销平台数据请求介绍

项目介绍:

      前端使用 WPF,采用MVVM模式  后端数据库采用的sqlite 依靠本地化运行   后期可能会采用WebApi   因为WPF都是自学的 所以 代码方面写的可能不够规范 有问题 可以指出 大家互相学习

     

      代码已经上传至github

      https://github.com/YC946586/YC.Marketing

后端:

     通过DAL类 对应的自定义特性 找到对应的实现方法

  接口类  定义DAL所需要的方法

  /// <summary>
    /// 接口层 为什么不用抽象类 因为我用动软生成数据访问层 懒得去改。
    /// 等业务需要了 再改
    /// </summary>
    public  interface  IUsDataDal<T> where T : class, new()
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        bool Exists(string zj);
        /// <summary>
        /// 增加一条数据
        /// </summary>
        void Add(T model);
        /// <summary>
        /// 更新一条数据
        /// </summary>
        bool Update(T model);
        /// <summary>
        /// 删除数据
        /// </summary>
       bool Delete(string zj);
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        T GetModel(string zj);

        /// <summary>
        /// 附加方法 用于本身生成代码业务不支持
        /// </summary>
        DataSet Addition(T model);
        /// <summary>
        /// 获得数据列表
        /// </summary>
        DataSet GetList(string strWhere);
        /// <summary>
        /// 获得前几行数据
        /// </summary>
       DataSet GetList(int top, string strWhere, string filedOrder);
    }
View Code

      方法请求

/// <summary>
    /// 数据请求
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class DataRequest<T> where T : class, new()
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(dynamic zj)
        {
            try
            {
                Bridge<T> bll = new Bridge<T>();
                return bll.Exists(zj);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(T model)
        {
            Bridge<T> bll = new Bridge<T>();
            bll.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static bool Update(T model)
        {
            Bridge<T> bll = new Bridge<T>();
            return bll.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static bool Delete(string zj)
        {
            Bridge<T> bll = new Bridge<T>();
            return bll.Delete(zj);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static T GetModel(string zj)
        {
            Bridge<T> bll = new Bridge<T>();
            return bll.GetModel(zj);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static  Task<List<T>> GetModelList(string strWhere="")
        {
            try
            {
                Bridge<T> bll = new Bridge<T>();
                return Task.FromResult(bll.GetModelList(strWhere));
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public static List<T> GetList(int Top, string strWhere, string filedOrder)
        {
            try
            {
                Bridge<T> bll = new Bridge<T>();
                return bll.GetList(Top, strWhere, filedOrder);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 附加方法 实现自己对应的逻辑
        /// </summary>
        public static List<T> Addition(T entity)
        {
            try
            {
                Bridge<T> bll = new Bridge<T>();
                return bll.Addition(entity);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        
    }
View Code

     定义请求方法转接到DAL的不同实现

 public class Bridge<T> where T : class, new()
    {
        /// <summary>
        /// 获取对应的数据访问层
        /// </summary>
        private readonly IUsDataDal<T> _dal = DataAccess.CreateusData<T>();

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string zj)
        {
            return _dal.Exists(zj);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(T model)
        {
            _dal.Add(model);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(T model)
        {
            return _dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string zj)
        {
            return _dal.Delete(zj);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public T GetModel(string zj)
        {
            return _dal.GetModel(zj);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<T> GetList(int top, string strWhere, string filedOrder)
        {
            DataSet ds = _dal.GetList(top, strWhere, filedOrder);
            return DataTableToList(ds);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<T> GetModelList(string strWhere)
        {
            DataSet ds = _dal.GetList(strWhere);
            return DataTableToList(ds);
        }

        /// <summary>
        /// 附加方法 实现自己对应的逻辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<T> Addition(T model)
        {
            DataSet ds = _dal.Addition(model);
            return DataTableToList(ds);
        }
        #region Private methods

        /// <summary>
        /// 获得数据列表
        /// </summary>
        private List<T> DataTableToList(DataSet dt)
        {
            var data = DataSetToEntityList<T>(dt);
            List<T> modelList = data.ToList();
            return modelList;
        }


        /// <summary>
        /// DataSet转换为实体列表
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pDataSet">DataSet</param>
        /// <param name="pTableIndex">待转换数据表索引</param>
        /// <returns>实体类列表</returns>
        private static IList<T> DataSetToEntityList<T>(DataSet pDataSet, int pTableIndex = 0)
        {
            try
            {
                if (pDataSet == null || pDataSet.Tables.Count < 0)
                    return default(IList<T>);
                if (pTableIndex > pDataSet.Tables.Count - 1)
                    return default(IList<T>);
                if (pTableIndex < 0)
                    pTableIndex = 0;
                if (pDataSet.Tables[pTableIndex].Rows.Count <= 0)
                    return default(IList<T>);

                DataTable p_Data = pDataSet.Tables[pTableIndex];
                // 返回值初始化
                IList<T> result = new List<T>();
                for (int j = 0; j < p_Data.Rows.Count; j++)
                {
                    T _t = (T)Activator.CreateInstance(typeof(T));
                    PropertyInfo[] propertys = _t.GetType().GetProperties();
                    foreach (PropertyInfo pi in propertys)
                    {
                        if (p_Data.Columns.IndexOf(pi.Name.ToUpper()) != -1 && p_Data.Rows[j][pi.Name.ToUpper()] != DBNull.Value)
                        {
                            object value = p_Data.Rows[j][pi.Name.ToUpper()];
                            if (pi.PropertyType.FullName == "System.Int32")//此处判断下Int32类型,如果是则强转
                                value = Convert.ToInt32(value);
                            pi.SetValue(_t, value, null);
                        }
                        else
                        {
                            pi.SetValue(_t, null, null);
                        }
                    }
                    result.Add(_t);
                }
                return result;
            }
            catch (Exception ex)
            {

                throw;
            }

        }


        #endregion


        #endregion

    }
View Code

    通过传递的实体来找到对应的DAL接口实现

  private static readonly Dictionary<string, string> DicAttribute = new Dictionary<string, string>();
        #region Createus
        /// <summary>
        /// 获取对应的数据访问层
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IUsDataDal<T> CreateusData<T>() where T : class, new()
        {
            try
            {
                string assemblyPath= string.Empty;
                //如果已经查找到对应的Dal类就不继续找了
                var fullName = typeof(T).FullName;
                if (fullName != null && DicAttribute.ContainsKey(fullName))
                {
                    assemblyPath = DicAttribute[fullName];
                }
                else
                {
                    var propertys = Assembly.GetExecutingAssembly().GetTypes();
                    foreach (var item in propertys)
                    {
                        object[] objAttrs = item.GetCustomAttributes(typeof(DataAttribute), true); //获取自定义特性
                        if (objAttrs.Length == 0)
                            continue;
                        DataAttribute curData = objAttrs.First() as DataAttribute;
                        if (curData != null && curData.Data.Equals(typeof(T).FullName))
                        {
                            assemblyPath = item.FullName;
                             DicAttribute.Add(fullName, item.FullName);
                        }

                    }
                } 
                //通过反射获取到DAL类 
                var classNamespace = Assembly.Load(Assembly.GetCallingAssembly().FullName).CreateInstance(assemblyPath);
                return classNamespace as IUsDataDal<T>;

                //方式2             
                //string classNamespace = AssemblyPath + ".us_gngl";
                //object objType = CreateObject(AssemblyPath, classNamespace);
                //return (Ius_gnglDal)objType;

            }
            catch (Exception ex)
            {

                throw;
            }

        }

        #endregion
View Code

 DAL 定义特性

请求示例:

   LoginResultData.TheMainConfig = DataRequest<UcGnglEntity>.Addition(new UcGnglEntity());

  有问题可以联系QQ 29579895  或者留言

  PS:为什么做这种无聊的东西  因为我最近挺闲

 

 

  

  

  

原文地址:https://www.cnblogs.com/Yangrx/p/11958643.html