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;
                }
            }

  • 相关阅读:
    Atcoder Regular Contest 123 题解
    Atcoder Grand Contest 015 F Kenus the Ancient Greek(找性质+乱搞)
    Solution 「CF 575G」Run for beer
    Solution 「CF 510E」Fox And Dinner
    Solution 「洛谷 P4389」付公主的背包
    Solution 「CF 555E」Case of Computer Network
    Solution 「CF 802C」Heidi and Library (hard)
    Solution 「JOISC 2020」「UOJ #509」迷路的猫
    Div弹出框
    JS对话框
  • 原文地址:https://www.cnblogs.com/XJNB/p/13144592.html
Copyright © 2011-2022 走看看