zoukankan      html  css  js  c++  java
  • ASP.Net 反射简单工厂模式

    SqlDal中


    public int AddUser<T>(T model) where T : class, new()
            {
              return  connection.Execute(GetSQLStr<T>(model));
            }

            public List<T> GetList<T>() where T : class, new()
            {
                return connection.Query<T>("").AsList();
            }
    /// <summary>
            /// 反射
            /// </summary>
            /// <typeparam name="T"><peparam>
            /// <param name="model"></param>
            /// <returns></returns>
            public string GetSQLStr<T>(T model) where T:class,new()
            {
                Type type = model.GetType();

                string tableName = type.Name.Replace("Model","");

                string _fields = "";

                string _values = "";
                //获取属性

                PropertyInfo[] properties = type.GetProperties();

                for(int i=0;i<properties.Length;i++)
                {
                    if(properties[i].Name.ToLower().Contains("id"))
                    {
                        continue;
                    }

                    if(i+1==properties.Length)
                    {
                        _fields += properties[i].Name;
                        _values += "'" + properties[i].GetValue(model) + "'";
                    }
                    else
                    {
                        _fields += properties[i].Name + ",";
                        _values += "'" + properties[i].GetValue(model) + "',";
                    }
                }
                string sql = $"insert into TB_{tableName}({_fields}) values({_values})";

                return sql;
            }


    MySqlDal中

     public int AddUser<T>(T model) where T : class, new()
            {
                return connection.Execute("");
            }

            public List<T> GetList<T>() where T : class, new()
            {
               return connection.Query<T>("select *from user").AsList();
            }


     public static IUserDAL CreateInstance(string typeName)
            {
                switch (typeName.ToLower())
                {
                    case "sql":
                        return new SqlUserDAL();
                    case "mysql":
                        return new MySqlUserDAL();
                    default:
                        return null;
                }
            }


    Factory中


     public static IUserDAL CreateInstance(string typeName)
            {
                switch (typeName.ToLower())
                {
                    case "sql":
                        return new SqlUserDAL();
                    case "mysql":
                        return new MySqlUserDAL();
                    default:
                        return null;
                }
            }

  • 相关阅读:
    matlab学习(1)——sparse函数和full函数处理稀疏矩阵
    MFC学习(6)——以数组矩阵形式表示读取出来的BMP图像||将数组矩阵数据转成BMP图像
    opencv学习(5)——HOG算子
    图像处理MFC学习(7)——实现8*8数组的DCT、IDCT
    MFC学习(3)——WIDTHBYTES()每行象素所占的字节数目
    【iOS】The differences between Class Extension and Header File 类扩展与头文件的区别
    PAT算法题学习笔记
    【前端】require函数实现原理
    【前端】从输入URL到页面加载完成的过程中都发生了什么事情
    Photoshop学习笔记(待续)
  • 原文地址:https://www.cnblogs.com/XJNB/p/13144592.html
Copyright © 2011-2022 走看看